Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How Functional can you be without Immutable / Persistent Data Structures?  RSS feed

 
Sean Corfield
Ranch Hand
Posts: 323
16
Clojure Linux Mac OS X Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the features that allows functional programs to perform well in languages like Scala and Clojure is the presence of immutable collections, based on persistent data structures. These allow for a lot of sharing of data and so new, modified versions of collections can be created with very low cost.

Java 8 introduces streams to allow functions to be chained without recomputing the intermediate results - the functions are just composed in sequence onto each element of the stream, as I understand it. That's similar to lazy sequences in Clojure and addresses that one use case of pipelining, but it doesn't address general operations on collections.

In languages with Persistent Data Structures, you can have a large hash map and have a function that alters just one key in a pure way, returning a new version of the hash map that shares nearly all its data with the original. In Clojure:

Questions for Richard:
  • do you feel that the lack of persistent data structures in Java 8 is an obstacle to adopting a functional style?
  • how would you tackle functions that needed to behave like the assoc example above in Java 8?
  •  
    Richard Warburton
    Author
    Greenhorn
    Posts: 22
    7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sean Corfield wrote:One of the features that allows functional programs to perform well in languages like Scala and Clojure is the presence of immutable collections, based on persistent data structures. These allow for a lot of sharing of data and so new, modified versions of collections can be created with very low cost.

    Java 8 introduces streams to allow functions to be chained without recomputing the intermediate results - the functions are just composed in sequence onto each element of the stream, as I understand it. That's similar to lazy sequences in Clojure and addresses that one use case of pipelining, but it doesn't address general operations on collections.

    In languages with Persistent Data Structures, you can have a large hash map and have a function that alters just one key in a pure way, returning a new version of the hash map that shares nearly all its data with the original. In Clojure:

    Questions for Richard:
  • do you feel that the lack of persistent data structures in Java 8 is an obstacle to adopting a functional style?
  • how would you tackle functions that needed to behave like the assoc example above in Java 8?


  • Hi Sean,

    I think that it would be great if Java 9 brought Immutable collections into the core library and let use their benefits in day to day programming. Having said that there's a lot that you can do without needing new collection types. The streams API itself encourages you to avoid mutating the source data structures and return new copies of the data type - for a lot of use cases that's perfectly sufficient.

    I've found that the Goldman Sachs collections library - https://github.com/goldmansachs/gs-collections - offers some pretty good immutable collections and is worth taking a look at. It also encourages a functional style if you code using it. If I've got a Java project where I want immutable collections this is what I would use.

    regards,

    Richard
     
    Sean Corfield
    Ranch Hand
    Posts: 323
    16
    Clojure Linux Mac OS X Monad
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Richard Warburton wrote:I've found that the Goldman Sachs collections library - https://github.com/goldmansachs/gs-collections - offers some pretty good immutable collections and is worth taking a look at. It also encourages a functional style if you code using it. If I've got a Java project where I want immutable collections this is what I would use.

    That looks very interesting - and certainly quite comprehensive with a wide variety of specific collection types, heavily influenced by Smalltalk it seems.

    Do you have any thoughts on choosing between the GS Collections and Google's Guava Collections? I haven't worked with either but I'd heard a fair bit about Guava (but wasn't aware of the GS library).
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!