Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/ext/infestines.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const divideBy = I.curry((d, n) => n / d)

export const negate = x => -x

export const not = x => !x

export const ltU = (x, y) => x < y
export const gtU = (x, y) => x > y

Expand All @@ -36,3 +38,49 @@ export function isPrimitiveData(x) {
}

export const iterator = Symbol.iterator

export const thenU = I.Async.chain
export const then = I.curry(thenU)

export const thenIdentityU = I.IdentityAsync.chain

export const thenResolveU = (fn, x) =>
I.isThenable(x) ? thenU(fn, x) : I.resolve(fn(x))

const EMPTY = 'empty'
const CONCAT = 'concat'

export function Monoid(concat, empty) {
if (!I.isInstanceOfU(Monoid, this)) return I.freeze(new Monoid(concat, empty))
this[CONCAT] = concat
this[EMPTY] = empty
}

export const MonoidWith = (concat, empty) => Monoid(concat, I.always(empty))

export const MonoidAsyncOf = m => {
const concat = m[CONCAT]
return Monoid(
(l, r) =>
I.isThenable(l)
? thenU(
l => (I.isThenable(r) ? thenU(r => concat(l, r), r) : concat(l, r)),
l
)
: I.isThenable(r)
? thenU(r => concat(l, r), r)
: concat(l, r),
m[EMPTY]
)
}

export const ProductMonoid = MonoidWith(multiplyU, 1)

export const SumMonoid = MonoidWith(addU, 0)

export const ConstantWith = (ap, empty) =>
I.Applicative(I.sndU, I.always(empty), ap)

export const ConstantOf = m => ConstantWith(m[CONCAT], m[EMPTY]())

export const ConstantAsyncOf = I.pipe2U(MonoidAsyncOf, ConstantOf)
Loading