Skip to content

Instantly share code, notes, and snippets.

@manute
Last active December 15, 2015 21:39
Show Gist options
  • Save manute/5327266 to your computer and use it in GitHub Desktop.
Save manute/5327266 to your computer and use it in GitHub Desktop.
like map erlang function
/*
Closure with 2 arguments:
-closure_F is the function F
-list_L is list L
*/
def map = { list_L , closure_F ->
// Delegate is the elment to actue over
delegate.collect(list_L) { element ->
closure_F(element)
}
}
// Add to metaClass of List dynamically with name "map"
List.metaClass.map = map
def list = [1,2,3,4]
def newList = []
list.map( newList ){ it * 2 }
assert newList == [2,4,6,8]
assert list == [1,2,3,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment