Skip to content

Instantly share code, notes, and snippets.

@pfgray
Created September 20, 2019 16:04
Show Gist options
  • Save pfgray/371d745ecea8c7106b0769731b7b9e79 to your computer and use it in GitHub Desktop.
Save pfgray/371d745ecea8c7106b0769731b7b9e79 to your computer and use it in GitHub Desktop.
Functions are functors
import * as React from 'react'
import { compose } from 'redux'
import { reader } from 'fp-ts/lib/Reader'
type StateContext<A> = [A, (a: A) => void]
const keepMax: (input: StateContext<number>) => StateContext<number> =
([state, setState]) => {
const keepMaxSetState = React.useCallback((n: number) => {
if(n > state) {
setState(n)
}
}, [state])
return [state, keepMaxSetState];
}
// small brain
const [max, trackMax] = keepMax(React.useState(0))
// large brain
const [max2, trackMax2] = compose(keepMax, React.useState)(0)
// galaxy brain
const [max3, trackMax3] = reader.map(React.useState, keepMax)(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment