Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Created August 28, 2012 15:36
Show Gist options
  • Select an option

  • Save JamieMason/3499183 to your computer and use it in GitHub Desktop.

Select an option

Save JamieMason/3499183 to your computer and use it in GitHub Desktop.
Haskell vs JavaScript: find the sum of all odd squares that are smaller than 10000
sum . takeWhile (<10000) . filter odd . map (^2) $ [1..]
var sum = 0,
i = 0,
n = 0;
while ((n = Math.pow(++i, 2)) < 1e4) {
i % 2 !== 0 && (sum += n);
}
@JamieMason
Copy link
Copy Markdown
Author

For some context, the constraints I put on myself for the above were to write as close to a single expression as possible in JavaScript, since that's what Haskell is able to do using only the language's standard library.

You could extend JavaScript with libraries to achieve some like the below;

take(1, 10000).map(pow(2)).filter(lt(10000), odd);

But the total code used to create that would be much more.

@makeusabrew
Copy link
Copy Markdown

Ah, that makes sense - I was wondering why the JS equivalent looked quite convoluted! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment