• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
  •  
    Author
    Posts: 31
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Rancher
    Posts: 379
    22
    Mac OS X Monad Clojure Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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).
    reply
      Bookmark Topic Watch Topic
    • New Topic