Skip to content

Instantly share code, notes, and snippets.

@mariogarcia
Created September 16, 2014 21:16
Show Gist options
  • Save mariogarcia/8e4bbc992d7739bd5135 to your computer and use it in GitHub Desktop.
Save mariogarcia/8e4bbc992d7739bd5135 to your computer and use it in GitHub Desktop.
Ideas on how to implement some list comprehensions
import static groovyfp.categories.Fn.*
import groovyfp.categories.*
/*
a = do
x <- (1..3)
y <- ('a','b')
return (x,y)
a = mdo {
x << (1..3)
y << ('a'..'b')
mreturn { [x, y] }
}
*/
def expr = { x, y -> [x + y] }
def result =
list(1,2,3).bind { x ->
list('a','b').bind { y ->
list([expr(x,y)])
}
}
result.typedRef.value
/*
m = mlet(x:10,y:20) {
x + y
}
*/
m = just(10).bind { x ->
just(20).bind { y ->
just(x + y)
}
}
m.typedRef.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment