All files / src/typeclass/data Endo.ts

100% Statements 16/16
100% Branches 6/6
100% Functions 4/4
100% Lines 16/16

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 411x 1x                                 1x 1x 2393x 101393x         1x 1401x 1401x 100x 100x 100x 100x 1401x         1x 1401x  
import {Monoid as MO, Semigroup as SE} from '@effect/typeclass'
import {identity, pipe} from 'effect'
import type {TypeLambda} from 'effect/HKT'
import * as FN from './Function.js'
 
/**
 * A unary function where argument type is return type.
 * @category datatype
 */
export interface Endo<A> extends FN.FunctionIn<A, A> {}
 
/**
 * @category type lambda
 */
export interface EndoTypeLambda extends TypeLambda {
  readonly type: Endo<this['Target']>
}
 
const combine =
  <A>(self: Endo<A>, that: Endo<A>): Endo<A> =>
  a =>
    pipe(a, self, that)
 
/**
 * @category instances
 */
export const getSemigroup = <A>(): SE.Semigroup<Endo<A>> => ({
  combine,
  combineMany: (self, collection) =>
    Array.from(collection).reduce(
      (accumulator, value) => combine(accumulator, value),
      self,
    ),
})
 
/**
 * @category instances
 */
export const getMonoid = <A>(): MO.Monoid<Endo<A>> =>
  MO.fromSemigroup(getSemigroup<A>(), identity)