-
Notifications
You must be signed in to change notification settings - Fork 21
map function
Q: In the following code; what does the 2nd term(beyond .map operator) on the left side of equation really do?
val misa = BigInt(0) | isa_string.map(x => 1 << (x - 'A')).reduce(_|_)
A: The map applies the same operation to every element. The reduce then applies an operator to all pairs of outputs from the map. So, this statement does the following:
Take the string "IMAFDC" -- so, isa_string has this value. Now, the map operator applies to each individual position of the string.
I - 'A'
followed by left shift
M - 'A'
followed by left shift
and so on. So there is a separate value generated for each position in the string.
Then, the .reduce(|) is applied. The underscores are place holders, and the vertical bar in the middle is the operation. So, this means apply the vertical bar to each neighboring pair of values generated by the map. Vertical bar is bitwise OR operator.
Here is the result:
1 << ('I' - 'A') | 1 << ('M' - 'A') | 1 << ('A' - 'A') | 1 << ('F' - 'A') | 1 << ('D' - 'A') | 1 << ('C' - 'A') | 1 << ('I' - 'A') | 1 << ('M' - 'A') | 1 << ('A' - 'A') | 1 << ('F' - 'A') | 1 << ('D' - 'A') | 1 << ('C' - 'A')