Skip to content

Commit

Permalink
simplify readerTask
Browse files Browse the repository at this point in the history
  • Loading branch information
sledorze authored and gcanti committed Dec 6, 2019
1 parent 6168985 commit 713c41c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 42 deletions.
20 changes: 2 additions & 18 deletions src/ReaderTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ export interface ReaderTask<R, A> {
(r: R): Task<A>
}

/**
* @since 2.3.0
*/
export function run<R, A>(ma: ReaderTask<R, A>, r: R): Promise<A> {
return ma(r)()
}

/**
* @since 2.3.0
*/
Expand All @@ -50,7 +43,7 @@ export const fromTask: <R, A>(ma: Task<A>) => ReaderTask<R, A> = T.fromM
/**
* @since 2.3.0
*/
export const reader: <R, A = never>(ma: Reader<R, A>) => ReaderTask<R, A> = T.fromReader
export const fromReader: <R, A = never>(ma: Reader<R, A>) => ReaderTask<R, A> = T.fromReader

/**
* @since 2.3.0
Expand All @@ -62,16 +55,7 @@ export function fromIO<R, A>(ma: IO<A>): ReaderTask<R, A> {
/**
* @since 2.3.0
*/
export function of<R, A>(a: A): ReaderTask<R, A> {
return fromTask(TA.of(a))
}

/**
* @since 2.3.0
*/
export function fromReader<R, A>(ma: Reader<R, A>): ReaderTask<R, A> {
return r => TA.of(ma(r))
}
export const of: <R, A>(a: A) => ReaderTask<R, A> = T.of

/**
* @since 2.3.0
Expand Down
40 changes: 16 additions & 24 deletions test/ReaderTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,55 @@ describe('ReaderTask', () => {
describe('Monad', () => {
it('map', async () => {
const double = (n: number): number => n * 2
const x = await _.run(_.readerTask.map(_.of(1), double), {})

const x = await _.readerTask.map(_.of(1), double)({})()
assert.deepStrictEqual(x, 2)
})

it('ap', async () => {
const double = (n: number): number => n * 2
const mab = _.of(double)
const ma = _.of(1)
const x = await _.run(_.readerTask.ap(mab, ma), {})
const x = await _.readerTask.ap(mab, ma)({})()
assert.deepStrictEqual(x, 2)
})

it('chain', async () => {
const f = (a: string) => _.of(a.length)
const e1 = await _.run(_.readerTask.chain(_.of('foo'), f), {})
const e1 = await _.readerTask.chain(_.of('foo'), f)({})()
assert.deepStrictEqual(e1, 3)
})

describe('readerTaskSeq', () => {
it('chain ', async () => {
const f = (a: string) => _.of(a.length)
const e1 = await _.run(_.readerTaskSeq.chain(_.of('foo'), f), {})
const e1 = await _.readerTaskSeq.chain(_.of('foo'), f)({})()
assert.deepStrictEqual(e1, 3)
})
})
})

it('ask', async () => {
const e = await _.run(_.ask<number>(), 1)
const e = await _.ask<number>()(1)()
return assert.deepStrictEqual(e, 1)
})

it('asks', async () => {
const e = await _.run(
_.asks((s: string) => s.length),
'foo'
)
const e = await _.asks((s: string) => s.length)('foo')()
return assert.deepStrictEqual(e, 3)
})

it('local', async () => {
const len = (s: string): number => s.length
const e = await _.run(
pipe(
_.asks((n: number) => n + 1),
_.local(len)
),
'aaa'
)
const e = await pipe(
_.asks((n: number) => n + 1),
_.local(len)
)('aaa')()
assert.deepStrictEqual(e, 4)
})

it('fromTask', async () => {
const e = await _.run(_.fromTask(task.of(1)), {})
const e = await _.fromTask(task.of(1))({})()
assert.deepStrictEqual(e, 1)
})

Expand All @@ -90,7 +85,7 @@ describe('ReaderTask', () => {
})

it('reader', async () => {
const e = await _.run(_.reader(reader.of(1)), {})
const e = await _.fromReader(reader.of(1))({})()
assert.deepStrictEqual(e, 1)
})

Expand All @@ -100,7 +95,7 @@ describe('ReaderTask', () => {
const t1 = _.readerTask.chain(append('start 1'), () => append('end 1'))
const t2 = _.readerTask.chain(append('start 2'), () => append('end 2'))
const sequenceParallel = array.sequence(_.readerTask)
const ns = await _.run(sequenceParallel([t1, t2]), {})
const ns = await sequenceParallel([t1, t2])({})()
assert.deepStrictEqual(ns, [3, 4])
assert.deepStrictEqual(log, ['start 1', 'start 2', 'end 1', 'end 2'])
})
Expand All @@ -111,17 +106,14 @@ describe('ReaderTask', () => {
const t1 = _.readerTask.chain(append('start 1'), () => append('end 1'))
const t2 = _.readerTask.chain(append('start 2'), () => append('end 2'))
const sequenceSeries = array.sequence(_.readerTaskSeq)
const ns = await _.run(sequenceSeries([t1, t2]), {})
const ns = await sequenceSeries([t1, t2])({})()
assert.deepStrictEqual(ns, [2, 4])
assert.deepStrictEqual(log, ['start 1', 'end 1', 'start 2', 'end 2'])
})

describe('MonadIO', () => {
it('fromIO', async () => {
const e = await _.run(
_.readerTask.fromIO(() => 1),
{}
)
const e = await _.readerTask.fromIO(() => 1)({})()
assert.deepStrictEqual(e, 1)
})
})
Expand Down

0 comments on commit 713c41c

Please sign in to comment.