Question: How to flatmap and map with index?

Here is an example of map and flat_map in other scripting languages such as NodeJS:


let a = ['a', 'b', 'c'],i)=>[e,i])                    // returns: [['a', 0], ['b', 1], ['c', 2]]
a.flatMap((e,i) => e > 'b' ? [] : [e]) // returns: ['a', 'b']


What I do for map_with_index is:


map_with_index := (f, a) -> zip(f, to_list(a), [`$`(1..nops(to_list(a)))]);


My question is:


1) What is the canonical way to achieve map_with_index?


2) Is there a builtin procedure for flat_map? If not how to achieve it?

Please Wait...