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 Nov 19, 2018
1 parent bf9271c commit e24b742
Show file tree
Hide file tree
Showing 5 changed files with 457 additions and 27 deletions.
43 changes: 43 additions & 0 deletions src/ext/infestines.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,46 @@ 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

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 e24b742

Please sign in to comment.