Last active
November 17, 2018 20:22
-
-
Save joaofnds/f9a3b46cf403f2881ca5acff248216a8 to your computer and use it in GitHub Desktop.
Simple js helpers (range, log, logFunc)
This file contains hidden or 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
| const fib = | |
| n => | |
| n < 2 | |
| ? 1 | |
| : fib(n - 1) + fib(n - 2) | |
| const range = | |
| (min, max) => | |
| Array(max - min + 1).fill().map((_, i) => i + min) | |
| const log = value => console.log(value) || value | |
| const logFunc = func => (...args) => log(func(...args)) | |
| const memoize = | |
| (func, mem = {}) => | |
| (...args) => { | |
| const key = JSON.stringify(args); | |
| mem[key] = mem[key] || func(...args) | |
| return mem[key]; | |
| } | |
| const memfib = memoize(fib) | |
| range(0, 10).forEach(logFunc(memfib)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment