Skip to content

Commit

Permalink
More support for asynchronous operations
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed Feb 14, 2019
1 parent 5fd5f9a commit 1d6a2d9
Show file tree
Hide file tree
Showing 5 changed files with 550 additions and 27 deletions.
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

0 comments on commit 1d6a2d9

Please sign in to comment.