Last active
December 15, 2015 21:39
-
-
Save manute/5327266 to your computer and use it in GitHub Desktop.
like map erlang function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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