admin管理员组文章数量:1400837
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).
本文标签: functional programmingWhat is the raku equivalent of the JavaScript arrow functionStack Overflow
版权声明:本文标题:functional programming - What is the raku equivalent of the JavaScript arrow function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744185991a2594293.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论