Skip to content

Instantly share code, notes, and snippets.

TK leandrotk

Block or report user

Report or block leandrotk

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View HeaderStatelessFunctionalComponent.js
const Header = props => (
<header className="top">
<h1>
Catch
<span className="ofThe">
<span className="of">OF</span>
<span className="the">The</span>
</span>
day
</h1>
View HeaderClassComponent.js
class Header extends React.Component {
render() {
return (
<header className="top">
<h1>
Catch
<span className="ofThe">
<span className="of">OF</span>
<span className="the">The</span>
</span>
View pi_pure_function.js
const PI = () => 3.14;
const calculateArea = (radius) => radius * radius * PI();
calculateArea(10); // returns 314
View event_listener_with_curry.js
/*
jQuery approach
$('some-css-class').on('click', callback);
*/
/*
How I want to use
- onClick(element, handler);
- onHover(element, handler);
*/
View declarative_smaller_elements.js
const isSmaller = x => element => element < x;
const filterArray = function(x, list) {
return list.filter(isSmaller(x));
}
const numbers = [10, 9, 8, 2, 7, 5, 1, 3, 0];
filterArray(3, numbers); // [2, 1, 0]
View debugging_compose_functions.js
const compose = (...fns) => n => fns.reduceRight((acc, fn) => fn(acc), n);
const trace = message => input => {
console.log(`${message} ${input}`);
return input;
}
const doStuffBetterWithDebugging = compose(trace("after f:"), f, trace("after g:"), g);
doStuffBetterWithDebugging(20);
// after g: 21
View do_stuff_with_debugging.js
const doStuffWithDebugging = x => {
const gOutput = g(x);
console.log(`after g: ${gOutput}`);
const fOutput = f(gOutput);
console.log(`after f: ${fOutput}`);
return fOutput;
}
doStuffWithDebugging(20);
// after g: 21
View chaining_is_composition.js
const list = [1, 2, 3, 4, 5];
const sumEvenNumbers = list => {
return list
.filter(n => n % 2 == 0)
.map(n => n * 2)
.reduce((previous, current) => previous + current);
};
View composing_functions.js
const g = n => n + 1;
const f = n => n * 2;
const doStuff = x => {
const gOutput = g(x);
const fOutput = f(gOutput);
return fOutput;
}
const result = doStuff(20);
View curry.js
const curry = f => {
const fn = (...args) => {
return f.length == args.length ? f(...args) : (...newArgs) => fn(...args, ...newArgs);
}
return fn;
}
const f = (x, y, z) => { return x + y + z };
You can’t perform that action at this time.