Last active
April 11, 2017 09:25
-
-
Save EvgenyOrekhov/66bc63c9befb444c4d8fe41194a38e8b to your computer and use it in GitHub Desktop.
My solutions to Functional Javascript Workshop https://github.com/timoxley/functional-javascript-workshop
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function upperCaser(input) { | |
return input.toUpperCase(); | |
} | |
module.exports = upperCaser; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
module.exports = function (namespace) { | |
return console.log.bind(console, namespace); | |
}; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
module.exports = function arrayMap(arr, fn) { | |
return arr.reduce( | |
(acc, currentValue) => [...acc, fn(currentValue)], | |
[] | |
); | |
}; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function Spy(target, method) { | |
const spy = {count: 0}; | |
const original = target[method]; | |
target[method] = function (...args) { | |
spy.count += 1; | |
return original.apply(target, args); | |
}; | |
return spy; | |
} | |
module.exports = Spy; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function repeat(operation, num) { | |
if (num <= 0) { | |
return; | |
} | |
operation(); | |
return setImmediate(() => repeat(operation, num - 1), 0); | |
} | |
module.exports = repeat; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function repeat(operation, num) { | |
if (num <= 0) { | |
return; | |
} | |
operation(); | |
return () => repeat(operation, num - 1); | |
} | |
function trampoline(fn) { | |
let next = fn(); | |
while (next) { | |
next = next(); | |
} | |
} | |
module.exports = function (operation, num) { | |
return trampoline(() => repeat(operation, num)); | |
}; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function loadUsers(userIds, load, done) { | |
const users = []; | |
let numberOfLoadedUsers = 0; | |
userIds.forEach( | |
(userId, index) => load( | |
userId, | |
function (user) { | |
users[index] = user; | |
numberOfLoadedUsers += 1; | |
if (numberOfLoadedUsers === userIds.length) { | |
done(users); | |
} | |
} | |
) | |
); | |
} | |
module.exports = loadUsers; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function getDependencies(tree) { | |
function getUniqueDependencies(subtree = {}) { | |
const {dependencies} = subtree; | |
if (!dependencies) { | |
return {}; | |
} | |
return Object | |
.keys(dependencies) | |
.reduce( | |
function (acc, depName) { | |
acc[`${depName}@${dependencies[depName].version}`] = | |
undefined; | |
return Object.assign( | |
{}, | |
acc, | |
getUniqueDependencies(dependencies[depName]) | |
); | |
}, | |
{} | |
); | |
} | |
const uniqueDependencies = getUniqueDependencies(tree); | |
return Object.keys(uniqueDependencies).sort(); | |
} | |
module.exports = getDependencies; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function getDependencies(tree) { | |
function getDepsArray(subtree = {}) { | |
if (!subtree.dependencies) { | |
return []; | |
} | |
return Object | |
.keys(subtree.dependencies) | |
.map( | |
(packageName) => Object.assign( | |
{name: packageName}, | |
subtree.dependencies[packageName] | |
) | |
); | |
} | |
function getUniqueDependencies(depsArray, depsAcc = {}) { | |
if (depsArray.length === 0) { | |
return depsAcc; | |
} | |
const {newDepsArray, newDepsAcc} = depsArray.reduce( | |
function reduce({newDepsArray, newDepsAcc}, aPackage) { | |
const {name, version} = aPackage; | |
newDepsAcc[`${name}@${version}`] = undefined; | |
return { | |
newDepsArray: newDepsArray.concat( | |
getDepsArray(aPackage) | |
), | |
newDepsAcc | |
}; | |
}, | |
{ | |
newDepsArray: [], | |
newDepsAcc: depsAcc | |
} | |
); | |
return getUniqueDependencies(newDepsArray, newDepsAcc); | |
} | |
const deps = getDepsArray(tree); | |
const uniqueDependencies = getUniqueDependencies(deps); | |
return Object.keys(uniqueDependencies).sort(); | |
} | |
module.exports = getDependencies; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function curryN(fn, n) { | |
const arity = n === undefined | |
? fn.length | |
: n; | |
return function partial(...params) { | |
return params.length < arity | |
? (...rest) => partial(...params, ...rest) | |
: fn(...params); | |
}; | |
} | |
module.exports = curryN; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
module.exports = Function.prototype.call.bind(Array.prototype.slice); |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function repeat(operation, num) { | |
if (num <= 0) { | |
return; | |
} | |
operation(); | |
return repeat(operation, num - 1); | |
} | |
module.exports = repeat; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function doubleAll(numbers) { | |
return numbers.map((number) => number * 2); | |
} | |
module.exports = doubleAll; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function getShortMessages(messages) { | |
return messages | |
.filter(function isShort({message}) { | |
return message.length < 50; | |
}) | |
.map((object) => object.message); | |
} | |
module.exports = getShortMessages; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function checkUsersValid(goodUsers) { | |
return function allUsersValid(submittedUsers) { | |
return submittedUsers.every( | |
(submittedUser) => goodUsers.some( | |
(goodUser) => submittedUser.id === goodUser.id | |
) | |
); | |
}; | |
} | |
module.exports = checkUsersValid; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function countWords(inputWords) { | |
return inputWords.reduce( | |
function countWord(counts, word) { | |
counts[word] = counts[word] | |
? counts[word] + 1 | |
: 1; | |
return counts; | |
}, | |
{} | |
); | |
} | |
module.exports = countWords; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function reduce(arr, fn, initial) { | |
function recur(acc, tail) { | |
if (tail.length === 0) { | |
return acc; | |
} | |
const [car, ...cdr] = tail; | |
return recur( | |
fn(acc, car), | |
cdr | |
); | |
} | |
return recur(initial, arr); | |
} | |
module.exports = reduce; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function duckCount(...ducks) { | |
return ducks | |
.filter((duck) => Object.prototype.hasOwnProperty.call(duck, "quack")) | |
.length; | |
} | |
module.exports = duckCount; |
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
/*jslint node, es6, maxlen: 80 */ | |
"use strict"; | |
function logger(namespace) { | |
return function log(...args) { | |
console.log(...[namespace, ...args]); | |
}; | |
} | |
module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment