What is the reason why the closure for `map` and `...
# nostupidquestions
n
What is the reason why the closure for
map
and
collect
seems to be slightly different? For example, with a 2 item channel
.map {_item1, item2 -> item2}
or
.map { items -> items[1]}
and both are ok, but with
.collect
only
.collect { items -> items[1] }
appears to work, but
.collect { _item1, item2 -> item2 }
throws an error?
p
Are you really testing this with the same data? I don't think there should be any difference and I don't see any in some quick tests. One difference though is that the closure
{ it[1] }
will happily run no matter how long the
it
is (access to a nonexisting index yields
null
), whereas
{ _item1, item2 -> item2 }
will blow up if
it
doesn't have exactly two elements.
n
Hm, maybe it was the second case and i miscounted
p
More common might be that you have extra elements - those still blow up when only two would be allowed.
n
But it is very good to know that using an index on
it
that is not part of it will return
null
p
I'm not sure how lazily things are evaluated, but it's possible that could be a thing (
collect
will need to run through all at once whereas one could allow for having the mapping of an element only done when it's to be consumed with
map
).
n
The error I received was:
Copy code
groovy.lang.MissingMethodException: No signature of method: Script_16f8fb075687b1c5$_runScript_closure1$_closure2$_closure12.doCall() is applicable for argument types: (ArrayList) values: [[]]
Possible solutions: doCall(java.lang.Object, java.lang.Object), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)
in case that is informative for you
p
I think it says you had an item that was a list of an empty list, although I'm not sure why it's presented in that form.
That would differ between the closures but not between
map
and
collect
.
n
Ok, I will assume that it was user error 😉