-
Notifications
You must be signed in to change notification settings - Fork 3
FunListToGroovyList
Robert Peszek edited this page Sep 17, 2013
·
1 revision
There are several methods to move between Groovy Lists and Funtional Lists implemented by Fpiglet:
- using funlistIn, funlistOut, funlistOutTake functions
- using withFunList
- using FunListToListFunctor
- using shortcut methods defined in fpig.groovylists.functions.FunBaseForGroovy.
fpig.groovylists.functions.FunBaseForGroovy defines shortcuts for functions which automate the back and forth. This allows for using Fpiglet functions on regular Groovy lists as if they were coded for them:
assert 10 = foldL(PLUS) << [1,4,6]
The fact that Fpiglet can simply map-over functions implemented for !FunList structures to Groovy Lists follows from the fact that the Type Mapping
FunList<T> -> List<T>
is a Functor (FunctorPolymorphism).
In fact, writing:
assert [5,10,50] = filter {it % 5==0} << map {1 + it * it} << (1..7)
is translated by Fpiglet to this:
Closure fmap = FunListToListFunctor.statics.fmap
assert [5,10,50] == fmap (filter {it % 5==0}) << fmap (map {1 + it * it} ) << (1..7)
Note this is not performance optimal. It is better to do this:
assert [5,10,50] == fmap(filter {it % 5==0} << map {1 + it * it}) << (1..7)
Which incidentally is one of the defining rules for being a Functor.
It maybe more readable to use withFunList syntax as in this code:
def squaresPlusOneDivisibleBy5 = withFunList(filter {it % 5==0} << map {1 + it*it})
assert [5,10,50] == squaresPlusOneDivisibleBy5 (1..7)
Back to FunList