Skip to content

Instantly share code, notes, and snippets.

@EvgenyOrekhov
Last active April 11, 2017 09:25
Show Gist options
  • Save EvgenyOrekhov/66bc63c9befb444c4d8fe41194a38e8b to your computer and use it in GitHub Desktop.
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
/*jslint node, es6, maxlen: 80 */
"use strict";
function upperCaser(input) {
return input.toUpperCase();
}
module.exports = upperCaser;
/*jslint node, es6, maxlen: 80 */
"use strict";
module.exports = function (namespace) {
return console.log.bind(console, namespace);
};
/*jslint node, es6, maxlen: 80 */
"use strict";
module.exports = function arrayMap(arr, fn) {
return arr.reduce(
(acc, currentValue) => [...acc, fn(currentValue)],
[]
);
};
/*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;
/*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;
/*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));
};
/*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;
/*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;
/*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;
/*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;
/*jslint node, es6, maxlen: 80 */
"use strict";
module.exports = Function.prototype.call.bind(Array.prototype.slice);
/*jslint node, es6, maxlen: 80 */
"use strict";
function repeat(operation, num) {
if (num <= 0) {
return;
}
operation();
return repeat(operation, num - 1);
}
module.exports = repeat;
/*jslint node, es6, maxlen: 80 */
"use strict";
function doubleAll(numbers) {
return numbers.map((number) => number * 2);
}
module.exports = doubleAll;
/*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;
/*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;
/*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;
/*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;
/*jslint node, es6, maxlen: 80 */
"use strict";
function duckCount(...ducks) {
return ducks
.filter((duck) => Object.prototype.hasOwnProperty.call(duck, "quack"))
.length;
}
module.exports = duckCount;
/*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