All files / src/laws/typeclass/parameterized FlatMap.ts

100% Statements 24/24
100% Branches 2/2
100% Functions 1/1
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 481x   1x     1x           1x 10x 10x 10x 10x   10x 10x 10x 10x 10x 10x 10x 10x 10x 901x 901x 901x 901x 10x 10x 10x                              
import {Law, lawTests} from '#law'
import {FlatMap} from '@effect/typeclass'
import {flow, pipe} from 'effect'
import type {TypeLambda} from 'effect/HKT'
import type {BuildParameterized} from './given.js'
import {unfoldGiven} from './given.js'
 
/**
 * Typeclass laws for `FlatMap`.
 * @category typeclass laws
 */
export const flatMapLaws: BuildParameterized<FlatMapTypeLambda> = (
  given,
  suffix?,
) => {
  const {F, fa, equalsFc, afb, bfc} = unfoldGiven(given)
 
  return lawTests(
    `FlatMap${suffix ?? ''}`,
    Law(
      'associativity',
      'fa ▹ flatMap(afb) ▹ flatMap(bfc) = fa ▹ flatMap(flatMap(bfc) ∘ afb)',
      fa,
      afb,
      bfc,
    )((fa, afb, bfc) =>
      equalsFc(
        pipe(fa, F.flatMap(afb), F.flatMap(bfc)),
        pipe(fa, F.flatMap(flow(afb, F.flatMap(bfc)))),
      ),
    ),
  )
}
 
/**
 * Type lambda for the `FlatMap` typeclass.
 * @category type lambda
 */
export interface FlatMapTypeLambda extends TypeLambda {
  readonly type: FlatMap.FlatMap<this['Target'] & TypeLambda>
}
 
declare module './given.js' {
  interface ParameterizedLambdas {
    FlatMap: FlatMapTypeLambda
  }
}