Skip to content

Instantly share code, notes, and snippets.

@carlosrberto
Created April 21, 2018 15:29
Show Gist options
  • Save carlosrberto/3ca71455f8f79f7757cf2088cec45c21 to your computer and use it in GitHub Desktop.
Save carlosrberto/3ca71455f8f79f7757cf2088cec45c21 to your computer and use it in GitHub Desktop.
Functional Stuffs 1
const input = document.getElementById('input');
const outPut = document.getElementById('output');
const leftKeys = [
'q', 'w', 'e', 'r', 't',
'a', 's', 'd', 'f', 'g',
'z', 'x', 'c', 'v',
];
const rightKeys = [
'y', 'u', 'i', 'o', 'p',
'h', 'j', 'k', 'l', 'ç',
'b', 'n', 'm',
];
const pipe = function(first, ...others) {
return function() {
return others.reduce((acc, fn) => {
return fn.call(null, acc);
}, first.apply(null, arguments));
}
}
const curry = function(fn) {
const partial = function(prevArgs = []) {
return function() {
const nextArgs = [...prevArgs, ...arguments];
if((prevArgs.length + arguments.length) === fn.length) {
return fn.apply(null, nextArgs)
} else {
return partial(nextArgs);
}
}
}
return partial();
};
const removeSpaces = w => w.replace(/\s/g, '');
const toLower = w => w.toLowerCase();
const toArray = w => w.split('');
const numWords = curry((l, w) => w.filter(k => l.includes(k)).length);
const getKeysDistribution = (word) => ({
left: pipe(removeSpaces, toLower, toArray, numWords(leftKeys))(word),
right: pipe(removeSpaces, toLower, toArray, numWords(rightKeys))(word),
});
input.addEventListener('input', (event) => {
outPut.value = JSON.stringify(
getKeysDistribution(input.value)
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment