Skip to main content


Showing posts from January, 2019

Foldable Collections / Data Structures

I've never been big into functional programming, so my recent work with VAVR, which more and more seems like some kind of java port back from scala or haskell, has been very new and different for me.  Some of the things I have seen have been really exciting -- flatMap and groupBy, for example, are very simple, straight-forward, and useful tools. And then there is folding.  Foldable data structures  ( here is another reference ).  Geometrically, the concept of folding is straight forward, too.  Imaging turning a page in a book -- this is folding the page over.  It is a simple as that. Functionally speaking, however, things get a lot more interesting.  If we start with a list: A B C D And then we fold that list in either direction, we don't visibly reflect, as folding is not quite the same as reflection.  Instead, folding results in this list: D C B A Of course, this is a very simple example of folding because folding can become much more complicated than that.


I have started working with VAVR  for part of a project.  So far, I like how they have approached collections (this library has basically rewritten/released a separate Collections API for Java!) and associated mutability; any mutation made to a collection returns a new collection -- it is not possible to modify an existing collection.  The documentation for the library even goes so far as to call void return type a code smell because it implies that a method has side effects. The push for immutability is a somewhat recent concept in programming, but VAVR is strongly on side of pushing for more immutability.  So far, I tend to like where this is going; if I let you borrow/use an object in real life, I expect you to give me that object back in the same state I left it or to tell me that you've changed it somehow. VAVR also takes a whole new approach to java streams (again, a whole separate API...) and introduces TupleX and FunctionX to support this approach.  I'm not sure