All files / src/typeclass/data Function.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 0/0
100% Lines 15/15

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 421x 1x                                               1x 1x 1x 1x 1021x 1021x   1021x 102100x 1x     1x 1x 1x 1x  
import {Contravariant as CN} from '@effect/typeclass'
import {dual, pipe} from 'effect/Function'
import type {Kind, TypeLambda} from 'effect/HKT'
 
/**
 * A unary function from `A` to `E`. The underlying type is the argument type `A`.
 * @category datatype
 */
export interface FunctionIn<A, E> {
  (a: A): E
}
 
/**
 * Type lambda for a unary function with its argument as the underlying type.
 * @category type lambda
 */
export interface FunctionInTypeLambda extends TypeLambda {
  readonly type: FunctionIn<this['Target'], this['Out1']>
}
 
/**
 * Map over the argument of a unary function converting a function of
 * type `(a: A) ⇒ E` to `(b: B) ⇒ E`.
 * @category combinators
 */
export const mapInput: CN.Contravariant<FunctionInTypeLambda>['contramap'] =
  dual(
    2,
    <R, O, E, A, B>(
      self: Kind<FunctionInTypeLambda, R, O, E, A>,
      f: (b: B) => A,
    ): Kind<FunctionInTypeLambda, R, O, E, B> =>
      b =>
        pipe(b, f, self),
  )
 
/** @category instances */
export const Contravariant: CN.Contravariant<FunctionInTypeLambda> = {
  imap: CN.imap<FunctionInTypeLambda>(mapInput),
  contramap: mapInput,
}