Skip to content

Instantly share code, notes, and snippets.

@tarasowski
Last active January 24, 2019 06:35
Show Gist options
  • Save tarasowski/39770c3651140414cf9b467e15594b19 to your computer and use it in GitHub Desktop.
Save tarasowski/39770c3651140414cf9b467e15594b19 to your computer and use it in GitHub Desktop.
Pure function composition, no assignments.
const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x)
const useState = init =>
([init, (init =>
x =>
useState(init + x))(init)])
const createState = useState =>
useState(3)
const render = ([count, setCount]) =>
(`<div><p>${count}</p></div>
<button onclick=${setCount}>My button</button>
<div><p>${count}</p></div>`)
const component = useState =>
compose(
render,
createState
)(useState)
console.log(
component(useState),
)
// "<div><p>3</p></div>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment