zatziky zatziky - 1 year ago 113
Javascript Question

Is Groovy .collect() an equivalent of JS .map()?

Is Groovy .collect() an equivalent of JS .map()? Or is there another purpose?

Answer Source

It can do a few other things too (like initial collection), but otherwise it's pretty much the same:

// Collect without
// initial collection.
assert [1,2,8] == [1,4,64].collect(Math.&sqrt)
assert [0,2,4,6] == (0..3).collect { it * 2 }
assert ['Groovy', 'Grails'] == [lang: 'Groovy', framework: 'Grails'].collect { it.value }
 
// Collect with initial collection argument.
assert [0, 1, 2, 3] == [2, 3].collect([0, 1]) { it }
assert [0, 3, 6, 9] == [2, 3].collect([0, 3], { it * 3})
assert ['Gradle', 'groovy', 'grails'] == ['Groovy', 'Grails'].collect(['Gradle']) { it.toLowerCase() }
assert ['m','r','h','a','k','i'] == [4, -3, 7, 5].collect(['m', 'r']) { (it + 100) as char }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download