You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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*asRfrom'fp-ts/Reader'constinput='Sup'describe('reader test',()=>{it('without Reader pattern',()=>{// GIVENconstlogger=(data: any)=>console.log(data)constlog=(logger: (data: string)=>void)=>(data: any)=>logger(data)constaddPrefix=(prefix: string)=>(data: string)=>`${prefix}${data}`constaddExclamation=(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.conststart=({
logger,
prefix,
isShouted
}: {logger: (data: string)=>voidprefix: stringisShouted: boolean})=>(data: string)=>pipe(data,addPrefix(prefix),addExclamation(isShouted),log(logger))// WHENconstresult=start({ logger,prefix: 'wzz',isShouted: true})(input)// THEN// expect is not correct, should check the logs for the resultexpect(result).toBe(false)})it('with Reader pattern',()=>{// GIVENconstlogger=(data: any)=>console.log(data)constlog=(data: any)=>({ logger }: {logger: (data: string)=>void})=>logger(data)constaddPrefix=(data: string)=>({ prefix }: {prefix: string})=>`${prefix}${data}`constaddExclamation=(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' functionconststart=(data: string)=>pipe(R.of(data),R.chain(addPrefix),R.chainW(addExclamation),R.chainW(log))// WHENconstresult=start(input)({ logger,prefix: 'wzz',isShouted: true})// THEN// expect is not correct, should check the logs for the resultexpect(result).toBe(false)})})
The text was updated successfully, but these errors were encountered:
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:
The text was updated successfully, but these errors were encountered: