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 getCharacterFrequency = string => string.split('').reduce((acc, char) => { | |
const { length } = string.split(char); | |
if (length > acc.frequency) { | |
acc.frequency = length; | |
acc.mostFrequent = char; | |
} | |
return acc; | |
}, { frequency: 0, mostFrequent: '' }).mostFrequent; |
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 findPeaks = arr => arr.reduce((result, curr, i) => { | |
const prev = arr[i - 1]; | |
const next = arr.slice(i).find(item => item !== curr); | |
if(curr > prev && curr > next) { | |
result.pos.push(i); | |
result.peaks.push(curr); | |
} | |
return result; |
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
// manual implementation | |
const sum = ([first, ...rest]) => rest.length ? first + sum(rest) : first; | |
// using reduce | |
const sum = arr => arr.reduce((acc, el) => acc + el, 0); |
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
// imperative | |
function fibonacci(n, first = 0, second = 1) { | |
while (n !== 0) { | |
console.log(first); // side-effect | |
[n, first, second] = [n - 1, second, first + second]; // assignment | |
} | |
} | |
fibonacci(10); | |
// functional |
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
### | |
Original project here: https://github.com/glenjamin/node-fib , | |
this is CoffeeScript rewrite of the app. Nothing more, nothing less. | |
### | |
app = require("express").createServer() | |
port = "3000" | |
fibonacci = (n, callback) -> | |
inner = (n1, n2, i) -> |