Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array.reduce #7

Open
sminnee opened this issue Aug 26, 2015 · 0 comments
Open

array.reduce #7

sminnee opened this issue Aug 26, 2015 · 0 comments

Comments

@sminnee
Copy link
Owner

sminnee commented Aug 26, 2015

Let's add a companion to array.map and we can bask in our functional goodness.

main {
  square = (x) => x*x;
  sum = (x) => x+x;
  sumSquares = a => a.map(square).reduce(sum);
  puts(sumSquares([1,2,3])); // 14
}

Ideally, the output of map can be piped into reduce without first creating an intermediary array, so that the compiled code is more of this form:

sumSquares(a) {
  for(i in a) {
    mapped = i*i;
    reduced = reduced+mapped;
  }
  return reduced;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant