Ok, I can’t figure this one out even though I have an idea what it’s doing…
let t = ["APE", "MONKEY", "DONKEY"]
Now consider three cases:
map (length.group) t
(map length.group) t
map (map length.group) t
This returns these three answers:
[3,6,6]
[1,1,1]
[[1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1]]
Now, can someone explain to me in details what’s going on. A crucial part of this question is that I assume that map needs a list to work on and I don’t see two maps being passed in the third case for example.
This composes the functions
lengthandgroup. The result is a function that takes a list (string) and returns the number of “groups” in that list (where a group is a sequence of the same character repeating 1 or more times, so “abc” contains 3 groups and so does “aabbcc”).This function is then applied to each string in
tusingmap.Here the function
map length(which takes the length of each sublist in a list of lists) is composed with the functiongroupand the composed function is applied tot. In other words it’s the same asmap length (group t).Here the function
map length . groupis applied to each string int, i.e.map length (group str)is calculated for each stringstrint.