Last active
June 9, 2023 10:08
-
-
Save suica/54dfe1fb7ac0ee2c61ad33fdc0c5a64c to your computer and use it in GitHub Desktop.
工业聚心爱的A Little JavaScript Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// statement https://lisperator.net/blog/a-little-javascript-problem/ | |
const push = (stack, value) => (first) => first ? stack : value; | |
const range = (left, right) => (left > right ? null : push(range(left, right - 1), right)); | |
const map = (stack, func) => (stack ? push(map(stack(1), func), func(stack(0))) : stack); | |
const foreach = (stack, func) => void map(stack, func); | |
const reverse = (stack, result = null) => stack ? reverse(stack(1), push(result, stack(0))) : result; | |
// You must not use arrays. The square bracket characters, [ and ], are forbidden, as well as Array constructor. | |
// You must not use objects. The curly braces, { and }, and the dot character (.) are forbidden. You may use curly braces for code blocks, but not for creating JavaScript objects. | |
// Should go without saying, these functions must be generic and do what their name implies. They must not be hard-coded for the particular 1..10 example. | |
var numbers = range(1, 10); | |
numbers = map(numbers, function (n) { | |
return n * n; | |
}); | |
numbers = reverse(numbers); | |
foreach(numbers, console.log); | |
/* output: | |
100 | |
81 | |
64 | |
49 | |
36 | |
25 | |
16 | |
9 | |
4 | |
1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment