Skip to content
Closed
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
2 changes: 1 addition & 1 deletion docs/modules/IO.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ Added in v2.0.0
**Signature**

```ts
export const of = <A>(a: A): IO<A> => () => ...
export const of = <A>(a: A): IO<A> => ...
```

Added in v2.0.0
Expand Down
78 changes: 74 additions & 4 deletions src/IO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ export function getMonoid<A>(M: Monoid<A>): Monoid<IO<A>> {
/**
* @since 2.0.0
*/
export const of = <A>(a: A): IO<A> => () => a
export const of = <A>(a: A): IO<A> => pureE(a)

/**
* @since 2.0.0
*/
export const io: Monad1<URI> & MonadIO1<URI> = {
URI,
map: (ma, f) => () => f(ma()),
map: mapE,
of,
ap: (mab, ma) => () => mab()(ma()),
chain: (ma, f) => () => f(ma())(),
ap: applyE,
chain: bindE,
fromIO: identity
}

Expand Down Expand Up @@ -187,3 +187,73 @@ export {
*/
map
}

const PURE = 'PURE'
const MAP = 'MAP'
const APPLY = 'APPLY'
const BIND = 'BIND'
const APPLY_FUNC = 'APPLY_FUNC'

function pureE(x: any) {
return mkEff(PURE, x)
}

function mapE(effect: any, f: any) {
return mkEff(MAP, f, effect)
}

function applyE(effF: any, effect: any) {
return mkEff(APPLY, effect, effF)
}

function bindE(effect: any, f: any) {
return mkEff(BIND, f, effect)
}

function mkEff(tag: 'PURE' | 'MAP' | 'APPLY' | 'BIND' | 'APPLY_FUNC', _0: any, _1?: any) {
const effect = function $effect() {
return runEff($effect)
} as any
effect.tag = tag
effect._0 = _0
effect._1 = _1
return effect
}

function runEff(inputEff: any) {
let operations = []
let effect = inputEff
let res
let op
effLoop: for (;;) {
if (effect.tag !== undefined) {
if (effect.tag === MAP || effect.tag === BIND || effect.tag === APPLY) {
operations.push(effect)
effect = effect._1
continue
}
// here `tag === PURE`
res = effect._0
} else {
res = effect()
}

// tslint:disable-next-line no-conditional-assignment
while ((op = operations.pop())) {
if (op.tag === MAP) {
res = op._0(res)
} else if (op.tag === APPLY_FUNC) {
res = op._0(res)
} else if (op.tag === APPLY) {
effect = op._0
operations.push({ tag: APPLY_FUNC, _0: res })
continue effLoop
} else {
// op.tag === BIND
effect = op._0(res)
continue effLoop
}
}
return res
}
}
5 changes: 5 additions & 0 deletions test/IO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as assert from 'assert'
import { IO, getSemigroup, io, getMonoid } from '../src/IO'
import { semigroupSum } from '../src/Semigroup'
import { monoidSum } from '../src/Monoid'
import { array, range } from '../src/Array'

describe('IO', () => {
it('ap', () => {
Expand Down Expand Up @@ -32,4 +33,8 @@ describe('IO', () => {
assert.strictEqual(M.concat(M.empty, append('b'))(), 2)
assert.deepStrictEqual(log, ['a', 'b'])
})

it('stack safe', () => {
array.sequence(io)(range(0, 20000).map(io.of))()
})
})
6 changes: 5 additions & 1 deletion test/IOEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { monoidString } from '../src/Monoid'
import { semigroupSum, semigroupString } from '../src/Semigroup'
import { none, some } from '../src/Option'
import { pipe, pipeable } from '../src/pipeable'
import { getMonoid } from '../src/Array'
import { getMonoid, array, range } from '../src/Array'

describe('IOEither', () => {
it('fold', () => {
Expand Down Expand Up @@ -283,4 +283,8 @@ describe('IOEither', () => {
assert.deepStrictEqual(r3(), _.left(['a'])())
})
})

it('stack safe', () => {
array.sequence(_.ioEither)(range(0, 20000).map(_.ioEither.of))()
})
})