This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Predicate<T> = (value: T) => boolean; | |
type Mapper<T, R> = (value: T) => R; | |
type CondPair<T, R> = [Predicate<T>, Mapper<T, R>]; | |
// recursive cond that can breaks early or returns undefined | |
const cond = <T, R>( | |
[[pred, mapr] = [] as unknown as CondPair<T, R>, ...rest]: CondPair<T, R>[] = [] | |
): ((val: T) => R | undefined) => (val: T): R | undefined => | |
!pred ? undefined : | |
pred(val) ? mapr(val) : cond(rest)(val); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const recMerge = (objA, objB) => Object.entries(objB).reduce( | |
(acc, [key, val]) => Object.assign( | |
{}, | |
objA, | |
{ [key]: is(Object, val) ? recMerge(objA[key], val) : val } | |
), | |
{} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const strongOrPlain = (bool, str) => bool | |
? `<strong>${str}</strong>` | |
: str; | |
const RangeItemComp = ({header, start, end, highlighted}) => `<li>${ | |
strongOrPlain( | |
highlighted, | |
`${header}: ${start} - ${end}` | |
)}</li>`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const S = f => g => x => f (x) (g (x)); | |
const S2 = f => g => h => x => f (g (x)) (h (x)); | |
// create a list based on a list of numbers that won't | |
// contain a zero, where the result is a list of products | |
// of the list without the number at the position of a given member | |
// eg [5,2,1,4,3] => [24, 60, 120, 30, 40] | |
// the key is that the computed product of the list and | |
// then divided by a given member, is equivalent to |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assocPth = ([head, ...rest], value, obj) => Array.isArray(obj) | |
? [].concat( | |
obj.slice(0, head), | |
[rest.length | |
? assocPth(rest, value, obj[head]) | |
: value], | |
obj.slice(head + 1)) | |
: Object.assign({}, | |
obj, | |
{[head]: rest.length |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const data = [ | |
{ label: 'first', val: 'gold-like' }, | |
{ label: 'second', val: 'silver-esque' }, | |
{ label: 'third', val: 'bronz-ish' }, | |
]; | |
const propLen = (propName) => compose( | |
length, | |
propOr('', propName) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// permitted actions for state changes | |
const eventActions = { | |
write: (path, val = null) => assocPath(path, val), | |
erase: (path) => dissocPath(path), | |
}; | |
const noop = () => () => {}; | |
// run a legitmate action on a state object | |
const eventToState = ({ type, path, val }, stateObj, actions) => propOr( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const formula = | |
{'+': [ | |
2, 2, 3, | |
{'-': [ | |
5, | |
{'+': [ | |
2, {'var': 'base-val'}, {var: 'base'}, | |
{'+': [ | |
1, | |
{'*': [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dot = (f, g) => a => f(g(a)); | |
const cmp = reduce(dot, identity); | |
const cmp2 = reduce(o, identity); | |
[ | |
compose( | |
sum, | |
range(2), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// accepts an argument object and a data object. | |
// first returns the value at lookupName in the data object | |
// and then applies the value of that back onto the data object | |
// treating it as meta data about the data object, eg | |
// lookWIthin( | |
// { lookupName: 'currPage', endOfPath: ['position'] }, | |
// { currPage: 'page!', page!: {position: 55} } | |
// ) => 55 | |
const lookWithin = ({ lookupName, endOfPath = [] }) => compose( |
NewerOlder