urgent / thisbounty.com-events

No Catch, No Pay - thisbounty.com
http://thisbounty.com
0 stars 0 forks source link

Request Response Receive Utility #35

Closed urgent closed 4 years ago

urgent commented 4 years ago

Effect Utility

Abstract out of Lead component to utility, to share with other components.

Need component integration and core utility

urgent commented 4 years ago

Required for implementation:

  1. Codec
  2. Dependencies
  3. Eq

If decode fails, mapped with error. How to detect threshold? Was just a count. Now need another loop.

For errors, need to emit in effect exec, or make needs deps.

urgent commented 4 years ago

Moved tasks to first comment

urgent commented 4 years ago

A reader patter avoids having to pass input

input1(deps) => input2(deps) => input3(deps) vs

input1() => input2() => input3() => deps => code

you ask for deps for last return

problem is if deps are not in series, or used in multiple places, gets confusing:

input1() => input2(deps) => input3() => input4() => input5(deps) => input6() => deps => code

~use an ask~

  1. Move deps from input to return
  2. Input for action only
  3. Use ask for make and validate.
urgent commented 4 years ago

Validation

#3.23

Combine multiple validation rules, receive all errors.

https://dev.to/gcanti/getting-started-with-fp-ts-either-vs-validation-5eja

urgent commented 4 years ago

Monadic Interface

#3.24

Start with input in a functor. Chain functions to manipulate that input

https://dev.to/gnomff_65/fp-ts-and-beautiful-api-calls-1f55

urgent commented 4 years ago

Generic Decoder type, t.Decoder<unknown, A>

#3.25

Use t.Decoder<unknown, A> for generic decoder

export const decodeWith = function<A> (decoder: t.Decoder<unknown, A>) {
  return flow(
    decoder.decode,
    E.mapLeft((errors: t.Errors) => new Error(failure(errors).join('\n')))
  )
}
urgent commented 4 years ago

Sequence and Traverse

#3.26

Use sequence to work on the entire collection, traverse to work on each element in the collection.

Need an applicative, something with of, map, and ap.

Need to look at the applicative definition. With either, just returns right. Sequence returns first error to left.

urgent commented 4 years ago

Error Combinators, no empty array

#3.27

Lift Either<Error, A> to Either<NonEmptyArray<A>,A> with a mapLeft(a => [a])

urgent commented 4 years ago

Either<Error, Lead>[] to {errors:Error[], leads:[]}

#3.28

Use reduce to a fold. Use accumulator.

TE.chain((leads: Lead[]) =>
      pipe(
        // [Lead, Lead, Lead, Lead, Lead]
        leads,
        // [Either<Error, Lead>, Either<Error, Lead>, Either<Error, Lead>, Either<Error, Lead>, ]
        reduce({ errors: [], leads: [] }, (acc: Result, lead) =>
          pipe(
            lead,
            validate(state),
            //side effects
            E.fold<Error, Lead, Result>(
              (e: Error) => {
                acc['errors'] = [...acc['errors'], e]
                return acc
              },
              (lead: Lead) => {
                acc['leads'] = [...acc['leads'], lead]
                return acc
              }
            )
          )
        ),
        TE.right
      )
    )
urgent commented 4 years ago

When to use Reader

#3.29

Reader asks for deps last. If something needs currying, such as an event emitter which needs to be removed to prevent memory leaks, it's better if deps are curried. Can't use reader here.

Also with ask, the dependencies parameter still needs to be written out, just in a chain instead of top level function. Really doesn't save much. Only for composition, taking something that doesn't read and turning it into a reader.

urgent commented 4 years ago

Seperated / Compactable

#3.30

Really nice when mapping arrays. Allows Errors to be returned alongside valid, instead of a transversable which stops the enitre result.