最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functional programming - What is the raku equivalent of the JavaScript arrow function? - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 18

The 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).

发布评论

评论列表(0)

  1. 暂无评论