Skip to content

Instantly share code, notes, and snippets.

@iampeterbanjo
Created August 7, 2019 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iampeterbanjo/b8a8bb5f904209aad09becf5a9b70226 to your computer and use it in GitHub Desktop.
Save iampeterbanjo/b8a8bb5f904209aad09becf5a9b70226 to your computer and use it in GitHub Desktop.
(function() {
console.log('Hello from the snippet');
const user = {
name: 'Cassie Cage'
};
const exampleTarget = {
firstname: 'Cassie',
surname: 'Cage',
};
const mapping = f => reducing => (result, input) =>
reducing(result, f(input));
const filtering = predicate => reducing => (result, input) =>
predicate(input) ? reducing(result, input) : result;
const chain = (xs, x) => xs.concat(x);
const result = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
.reduce(mapping(x => x + 1)(chain), [])
.reduce(filtering(x => x % 2 === 0)(chain), []);
// [2, 4, 6, 8, 10]
console.log(result);
const inspect = x => {
console.log(x);
return x;
}
const doNothing = x => x;
const parseName = x => {
const { name } = x;
const [firstname, surname] = name.split(' ');
const result = { firstname, surname };
return result;
};
[user]
.reduce(mapping(inspect)(chain), [])
.reduce(mapping(parseName)(chain), [])
.reduce(mapping(doNothing)(chain), [])
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment