In JavaScript, I can go
const materials = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
console.log(materials.map(material => material.length));
// expected output: Array [8, 6, 7, 9]
I guess that raku has some chops in functional - and I wonder if someone can clarify the equivalent code (see )
In JavaScript, I can go
const materials = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
console.log(materials.map(material => material.length));
// expected output: Array [8, 6, 7, 9]
I guess that raku has some chops in functional - and I wonder if someone can clarify the equivalent code (see https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions )
Share Improve this question edited Jul 7, 2020 at 12:47 raiph 32.5k3 gold badges64 silver badges111 bronze badges asked Jul 6, 2020 at 20:22 librastevelibrasteve 7,62110 silver badges33 bronze badges 2-
What feature of arrow functions are you looking to replicate? Lexical
this
binding? Short-hand syntax where the body of the function is an expression rather than a block? The ability to create a function using the=>
symbols? – Quentin Commented Jul 6, 2020 at 20:26 -
hi @quentin - the latter i.e. ability to create a function with
=>
– librasteve Commented Jul 6, 2020 at 21:08
1 Answer
Reset to default 18The most direct equivalent would be
my @materials = <Hydrogen Helium Lithium Beryllium>;
say @materials.map(-> $material { $material.chars });
but an arrow sub is more explicit than you need in this case, because
say @materials.map: *.chars;
would also be sufficient (method call on a "whatever star" returns a code block that calls that method on its argument), and
say @materials».chars;
would also work (hyper-application applied to the dot operator).