Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[request] How to use the Reader pattern with Tasks #25

Open
brahimmouhamou opened this issue Nov 1, 2022 · 0 comments
Open

[request] How to use the Reader pattern with Tasks #25

brahimmouhamou opened this issue Nov 1, 2022 · 0 comments

Comments

@brahimmouhamou
Copy link

brahimmouhamou commented Nov 1, 2022

Request

Would love some real live examples where using the Reader pattern makes your live easier.

I have a Jest example of a "hello world" example but it's a different thing to use it in combination with a combo of different types like TaskOption en TaskEithers.

Simple example:

import { pipe } from 'fp-ts/lib/function'
import * as R from 'fp-ts/Reader'

const input = 'Sup'

describe('reader test', () => {
	it('without Reader pattern', () => {
    // GIVEN
    const logger = (data: any) => console.log(data)
    const log = (logger: (data: string) => void) => (data: any) => logger(data)
    const addPrefix = (prefix: string) => (data: string) => `${prefix}${data}`
    const addExclamation = (isShouted: boolean) => (data: string) =>
      isShouted ? `${data.toUpperCase()}!!!` : data

    // Problem here is that 'start' needs to know all the dependencies
    // while using Reader they are infered.
    const start =
      ({
        logger,
        prefix,
        isShouted
      }: {
        logger: (data: string) => void
        prefix: string
        isShouted: boolean
      }) =>
      (data: string) =>
        pipe(data, addPrefix(prefix), addExclamation(isShouted), log(logger))

    // WHEN
    const result = start({ logger, prefix: 'wzz', isShouted: true })(input)

    // THEN
    // expect is not correct, should check the logs for the result
    expect(result).toBe(false)
  })


  it('with Reader pattern', () => {
    // GIVEN
    const logger = (data: any) => console.log(data)
    const log = (data: any) => ({ logger }: { logger: (data: string) => void }) => logger(data)
    const addPrefix = (data: string) => ({ prefix }: { prefix: string }) => `${prefix}${data}`
    const addExclamation = (data: string) => ({ isShouted }: { isShouted: boolean }) => 
			isShouted ? `${data.toUpperCase()}!!!` : data

		// As you can see here we do not need to define the dependencies when
		// creating our 'start' function
    const start = (data: string) =>
      pipe(R.of(data), R.chain(addPrefix), R.chainW(addExclamation), R.chainW(log))

    // WHEN
    const result = start(input)({ logger, prefix: 'wzz', isShouted: true })

    // THEN
    // expect is not correct, should check the logs for the result
    expect(result).toBe(false)
  })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant