Skip to content

Latest commit

 

History

History
1941 lines (1261 loc) · 32.2 KB

index.ts.md

File metadata and controls

1941 lines (1261 loc) · 32.2 KB
title nav_order parent
index.ts
4
Modules

index overview

Added in v1.0.0


Table of contents


LensFromPath (interface)

Signature

export interface LensFromPath<S> {
  <
    K1 extends keyof S,
    K2 extends keyof S[K1],
    K3 extends keyof S[K1][K2],
    K4 extends keyof S[K1][K2][K3],
    K5 extends keyof S[K1][K2][K3][K4]
  >(
    path: [K1, K2, K3, K4, K5]
  ): Lens<S, S[K1][K2][K3][K4][K5]>
  <K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2], K4 extends keyof S[K1][K2][K3]>(
    path: [K1, K2, K3, K4]
  ): Lens<S, S[K1][K2][K3][K4]>
  <K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2]>(path: [K1, K2, K3]): Lens<S, S[K1][K2][K3]>
  <K1 extends keyof S, K2 extends keyof S[K1]>(path: [K1, K2]): Lens<S, S[K1][K2]>
  <K1 extends keyof S>(path: [K1]): Lens<S, S[K1]>
}

Added in v1.3.0

ModifyF (interface)

Signature

export interface ModifyF<S, A> {
  <F extends URIS3>(F: Applicative3<F>): <U, L>(f: (a: A) => Kind3<F, U, L, A>) => (s: S) => Kind3<F, U, L, S>
  <F extends URIS2>(F: Applicative2<F>): <L>(f: (a: A) => Kind2<F, L, A>) => (s: S) => Kind2<F, L, S>
  <F extends URIS2, L>(F: Applicative2C<F, L>): (f: (a: A) => Kind2<F, L, A>) => (s: S) => Kind2<F, L, S>
  <F extends URIS>(F: Applicative1<F>): (f: (a: A) => Kind<F, A>) => (s: S) => Kind<F, S>
  <F>(F: Applicative<F>): (f: (a: A) => HKT<F, A>) => (s: S) => HKT<F, S>
}

Added in v1.0.0

At (class)

Signature

export class At<S, I, A> {
  constructor(readonly at: (i: I) => Lens<S, A>) { ... }
  ...
}

Added in v1.2.0

fromIso (method)

lift an instance of At using an Iso

Signature

fromIso<T>(iso: Iso<T, S>): At<T, I, A> { ... }

Added in v1.2.0

Fold (class)

Signature

export class Fold<S, A> {
  constructor(readonly foldMap: <M>(M: Monoid<M>) => (f: (a: A) => M) => (s: S) => M) { ... }
  ...
}

Added in v1.0.0

compose (method)

compose a Fold with a Fold

Signature

compose<B>(ab: Fold<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeFold (method)

Alias of compose

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeGetter (method)

compose a Fold with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeTraversal (method)

compose a Fold with a Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeOptional (method)

compose a Fold with a Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeLens (method)

compose a Fold with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composePrism (method)

compose a Fold with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeIso (method)

compose a Fold with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Fold<S, B> { ... }

Added in v1.0.0

find (method)

find the first target of a Fold matching the predicate

Signature

find<B extends A>(p: Refinement<A, B>): (s: S) => Option<B>
find(p: Predicate<A>): (s: S) => Option<A> { ... }

Added in v1.0.0

headOption (method)

get the first target of a Fold

Signature

headOption(s: S): Option<A> { ... }

Added in v1.0.0

Getter (class)

Signature

export class Getter<S, A> {
  constructor(readonly get: (s: S) => A) { ... }
  ...
}

Added in v1.0.0

asFold (method)

view a Getter as a Fold

Signature

asFold(): Fold<S, A> { ... }

Added in v1.0.0

compose (method)

compose a Getter with a Getter

Signature

compose<B>(ab: Getter<A, B>): Getter<S, B> { ... }

Added in v1.0.0

composeGetter (method)

Alias of compose

Signature

composeGetter<B>(ab: Getter<A, B>): Getter<S, B> { ... }

Added in v1.0.0

composeFold (method)

compose a Getter with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeLens (method)

compose a Getter with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Getter<S, B> { ... }

Added in v1.0.0

composeIso (method)

compose a Getter with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Getter<S, B> { ... }

Added in v1.0.0

composeTraversal (method)

compose a Getter with a Optional

Signature

composeTraversal<B>(ab: Traversal<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeOptional (method)

compose a Getter with a Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composePrism (method)

compose a Getter with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Fold<S, B> { ... }

Added in v1.0.0

Index (class)

Signature

export class Index<S, I, A> {
  constructor(readonly index: (i: I) => Optional<S, A>) { ... }
  ...
}

Added in v1.2.0

fromAt (static method)

Signature

static fromAt<T, J, B>(at: At<T, J, Option<B>>): Index<T, J, B> { ... }

Added in v1.2.0

fromIso (method)

lift an instance of Index using an Iso

Signature

fromIso<T>(iso: Iso<T, S>): Index<T, I, A> { ... }

Added in v1.2.0

Iso (class)

Laws:

  1. reverseGet(get(s)) = s
  2. get(reversetGet(a)) = a

Signature

export class Iso<S, A> {
  constructor(readonly get: (s: S) => A, readonly reverseGet: (a: A) => S) { ... }
  ...
}

Added in v1.0.0

reverse (method)

reverse the Iso: the source becomes the target and the target becomes the source

Signature

reverse(): Iso<A, S> { ... }

Added in v1.0.0

modify (method)

Signature

modify(f: (a: A) => A): (s: S) => S { ... }

Added in v1.0.0

asLens (method)

view an Iso as a Lens

Signature

asLens(): Lens<S, A> { ... }

Added in v1.0.0

asPrism (method)

view an Iso as a Prism

Signature

asPrism(): Prism<S, A> { ... }

Added in v1.0.0

asOptional (method)

view an Iso as a Optional

Signature

asOptional(): Optional<S, A> { ... }

Added in v1.0.0

asTraversal (method)

view an Iso as a Traversal

Signature

asTraversal(): Traversal<S, A> { ... }

Added in v1.0.0

asFold (method)

view an Iso as a Fold

Signature

asFold(): Fold<S, A> { ... }

Added in v1.0.0

asGetter (method)

view an Iso as a Getter

Signature

asGetter(): Getter<S, A> { ... }

Added in v1.0.0

asSetter (method)

view an Iso as a Setter

Signature

asSetter(): Setter<S, A> { ... }

Added in v1.0.0

compose (method)

compose an Iso with an Iso

Signature

compose<B>(ab: Iso<A, B>): Iso<S, B> { ... }

Added in v1.0.0

composeIso (method)

Alias of compose

Signature

composeIso<B>(ab: Iso<A, B>): Iso<S, B> { ... }

Added in v1.0.0

composeLens (method)

compose an Iso with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Lens<S, B> { ... }

Added in v1.0.0

composePrism (method)

compose an Iso with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Prism<S, B> { ... }

Added in v1.0.0

composeOptional (method)

compose an Iso with an Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Optional<S, B> { ... }

Added in v1.0.0

composeTraversal (method)

compose an Iso with a Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }

Added in v1.0.0

composeFold (method)

compose an Iso with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeGetter (method)

compose an Iso with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Getter<S, B> { ... }

Added in v1.0.0

composeSetter (method)

compose an Iso with a Setter

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }

Added in v1.0.0

Lens (class)

Laws:

  1. get(set(a)(s)) = a
  2. set(get(s))(s) = s
  3. set(a)(set(a)(s)) = set(a)(s)

Signature

export class Lens<S, A> {
  constructor(readonly get: (s: S) => A, readonly set: (a: A) => (s: S) => S) { ... }
  ...
}

Added in v1.0.0

fromPath (static method)

Signature

static fromPath<S>(): LensFromPath<S> { ... }

Example

import { Lens } from 'monocle-ts'

type Person = {
  name: string
  age: number
  address: {
    city: string
  }
}

const city = Lens.fromPath<Person>()(['address', 'city'])

const person: Person = { name: 'Giulio', age: 43, address: { city: 'Milan' } }

assert.strictEqual(city.get(person), 'Milan')
assert.deepStrictEqual(city.set('London')(person), { name: 'Giulio', age: 43, address: { city: 'London' } })

Added in v1.0.0

fromProp (static method)

generate a lens from a type and a prop

Signature

static fromProp<S>(): <P extends keyof S>(prop: P) => Lens<S, S[P]> { ... }

Example

import { Lens } from 'monocle-ts'

type Person = {
  name: string
  age: number
}

const age = Lens.fromProp<Person>()('age')

const person: Person = { name: 'Giulio', age: 43 }

assert.strictEqual(age.get(person), 43)
assert.deepStrictEqual(age.set(44)(person), { name: 'Giulio', age: 44 })

Added in v1.0.0

fromProps (static method)

generate a Lens from a type and an array of props

Signature

static fromProps<S>(): <P extends keyof S>(props: Array<P>) => Lens<S, { [K in P]: S[K] }> { ... }

Example

import { Lens } from 'monocle-ts'

interface Person {
  name: string
  age: number
  rememberMe: boolean
}

const lens = Lens.fromProps<Person>()(['name', 'age'])

const person: Person = { name: 'Giulio', age: 44, rememberMe: true }

assert.deepStrictEqual(lens.get(person), { name: 'Giulio', age: 44 })
assert.deepStrictEqual(lens.set({ name: 'Guido', age: 47 })(person), { name: 'Guido', age: 47, rememberMe: true })

Added in v1.0.0

fromNullableProp (static method)

generate a Lens from a type and a prop whose type is nullable

Signature

static fromNullableProp<S>(): <A extends S[K], K extends keyof S>(
    k: K,
    defaultValue: A
  ) => Lens<S, NonNullable<S[K]>> { ... }

Example

import { Lens } from 'monocle-ts'

interface Outer {
  inner?: Inner
}

interface Inner {
  value: number
  foo: string
}

const inner = Lens.fromNullableProp<Outer>()('inner', { value: 0, foo: 'foo' })
const value = Lens.fromProp<Inner>()('value')
const lens = inner.compose(value)

assert.deepStrictEqual(lens.set(1)({})), { inner: { value: 1, foo: 'foo' } })
assert.strictEqual(lens.get({})), 0)
assert.deepStrictEqual(lens.set(1)({ inner: { value: 1, foo: 'bar' } })), { inner: { value: 1, foo: 'bar' } })
assert.strictEqual(lens.get({ inner: { value: 1, foo: 'bar' } })), 1)

Added in v1.0.0

modify (method)

Signature

modify(f: (a: A) => A): (s: S) => S { ... }

Added in v1.0.0

asOptional (method)

view a Lens as a Optional

Signature

asOptional(): Optional<S, A> { ... }

Added in v1.0.0

asTraversal (method)

view a Lens as a Traversal

Signature

asTraversal(): Traversal<S, A> { ... }

Added in v1.0.0

asSetter (method)

view a Lens as a Setter

Signature

asSetter(): Setter<S, A> { ... }

Added in v1.0.0

asGetter (method)

view a Lens as a Getter

Signature

asGetter(): Getter<S, A> { ... }

Added in v1.0.0

asFold (method)

view a Lens as a Fold

Signature

asFold(): Fold<S, A> { ... }

Added in v1.0.0

compose (method)

compose a Lens with a Lens

Signature

compose<B>(ab: Lens<A, B>): Lens<S, B> { ... }

Added in v1.0.0

composeLens (method)

Alias of compose

Signature

composeLens<B>(ab: Lens<A, B>): Lens<S, B> { ... }

Added in v1.0.0

composeGetter (method)

compose a Lens with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Getter<S, B> { ... }

Added in v1.0.0

composeFold (method)

compose a Lens with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeOptional (method)

compose a Lens with an Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Optional<S, B> { ... }

Added in v1.0.0

composeTraversal (method)

compose a Lens with an Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }

Added in v1.0.0

composeSetter (method)

compose a Lens with an Setter

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }

Added in v1.0.0

composeIso (method)

compose a Lens with an Iso

Signature

composeIso<B>(ab: Iso<A, B>): Lens<S, B> { ... }

Added in v1.0.0

composePrism (method)

compose a Lens with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Optional<S, B> { ... }

Added in v1.0.0

Optional (class)

Laws:

  1. getOption(s).fold(() => s, a => set(a)(s)) = s
  2. getOption(set(a)(s)) = getOption(s).map(_ => a)
  3. set(a)(set(a)(s)) = set(a)(s)

Signature

export class Optional<S, A> {
  constructor(readonly getOption: (s: S) => Option<A>, readonly set: (a: A) => (s: S) => S) { ... }
  ...
}

Added in v1.0.0

fromNullableProp (static method)

Signature

static fromNullableProp<S>(): <K extends keyof S>(k: K) => Optional<S, NonNullable<S[K]>> { ... }

Example

import { Optional, Lens } from 'monocle-ts'

interface Phone {
  number: string
}
interface Employment {
  phone?: Phone
}
interface Info {
  employment?: Employment
}
interface Response {
  info?: Info
}

const info = Optional.fromNullableProp<Response>()('info')
const employment = Optional.fromNullableProp<Info>()('employment')
const phone = Optional.fromNullableProp<Employment>()('phone')
const number = Lens.fromProp<Phone>()('number')
const numberFromResponse = info
  .compose(employment)
  .compose(phone)
  .composeLens(number)

const response1: Response = {
  info: {
    employment: {
      phone: {
        number: '555-1234'
      }
    }
  }
}
const response2: Response = {
  info: {
    employment: {}
  }
}

numberFromResponse.getOption(response1) // some('555-1234')
numberFromResponse.getOption(response2) // none

Added in v1.0.0

fromOptionProp (static method)

Signature

static fromOptionProp<S>(): <P extends OptionPropertyNames<S>>(prop: P) => Optional<S, OptionPropertyType<S, P>> { ... }

Example

import { Optional, Lens } from 'monocle-ts'
import { Option } from 'fp-ts/lib/Option'

interface Phone {
  number: string
}
interface Employment {
  phone: Option<Phone>
}
interface Info {
  employment: Option<Employment>
}
interface Response {
  info: Option<Info>
}

const info = Optional.fromOptionProp<Response>()('info')
const employment = Optional.fromOptionProp<Info>()('employment')
const phone = Optional.fromOptionProp<Employment>()('phone')
const number = Lens.fromProp<Phone>()('number')
export const numberFromResponse = info
  .compose(employment)
  .compose(phone)
  .composeLens(number)

Added in v1.0.0

modify (method)

Signature

modify(f: (a: A) => A): (s: S) => S { ... }

Added in v1.0.0

modifyOption (method)

Signature

modifyOption(f: (a: A) => A): (s: S) => Option<S> { ... }

Added in v1.0.0

asTraversal (method)

view a Optional as a Traversal

Signature

asTraversal(): Traversal<S, A> { ... }

Added in v1.0.0

asFold (method)

view an Optional as a Fold

Signature

asFold(): Fold<S, A> { ... }

Added in v1.0.0

asSetter (method)

view an Optional as a Setter

Signature

asSetter(): Setter<S, A> { ... }

Added in v1.0.0

compose (method)

compose a Optional with a Optional

Signature

compose<B>(ab: Optional<A, B>): Optional<S, B> { ... }

Added in v1.0.0

composeOptional (method)

Alias of compose

Signature

composeOptional<B>(ab: Optional<A, B>): Optional<S, B> { ... }

Added in v1.0.0

composeTraversal (method)

compose an Optional with a Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }

Added in v1.0.0

composeFold (method)

compose an Optional with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeSetter (method)

compose an Optional with a Setter

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }

Added in v1.0.0

composeLens (method)

compose an Optional with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Optional<S, B> { ... }

Added in v1.0.0

composePrism (method)

compose an Optional with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Optional<S, B> { ... }

Added in v1.0.0

composeIso (method)

compose an Optional with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Optional<S, B> { ... }

Added in v1.0.0

composeGetter (method)

compose an Optional with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Fold<S, B> { ... }

Added in v1.0.0

Prism (class)

Laws:

  1. getOption(s).fold(s, reverseGet) = s
  2. getOption(reverseGet(a)) = Some(a)

Signature

export class Prism<S, A> {
  constructor(readonly getOption: (s: S) => Option<A>, readonly reverseGet: (a: A) => S) { ... }
  ...
}

Added in v1.0.0

fromPredicate (static method)

Signature

static fromPredicate<S, A extends S>(refinement: Refinement<S, A>): Prism<S, A>
static fromPredicate<A>(predicate: Predicate<A>): Prism<A, A> { ... }

Added in v1.0.0

some (static method)

Signature

static some<A>(): Prism<Option<A>, A> { ... }

Added in v1.0.0

modify (method)

Signature

modify(f: (a: A) => A): (s: S) => S { ... }

Added in v1.0.0

modifyOption (method)

Signature

modifyOption(f: (a: A) => A): (s: S) => Option<S> { ... }

Added in v1.0.0

set (method)

set the target of a Prism with a value

Signature

set(a: A): (s: S) => S { ... }

Added in v1.0.0

asOptional (method)

view a Prism as a Optional

Signature

asOptional(): Optional<S, A> { ... }

Added in v1.0.0

asTraversal (method)

view a Prism as a Traversal

Signature

asTraversal(): Traversal<S, A> { ... }

Added in v1.0.0

asSetter (method)

view a Prism as a Setter

Signature

asSetter(): Setter<S, A> { ... }

Added in v1.0.0

asFold (method)

view a Prism as a Fold

Signature

asFold(): Fold<S, A> { ... }

Added in v1.0.0

compose (method)

compose a Prism with a Prism

Signature

compose<B>(ab: Prism<A, B>): Prism<S, B> { ... }

Added in v1.0.0

composePrism (method)

Alias of compose

Signature

composePrism<B>(ab: Prism<A, B>): Prism<S, B> { ... }

Added in v1.0.0

composeOptional (method)

compose a Prism with a Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Optional<S, B> { ... }

Added in v1.0.0

composeTraversal (method)

compose a Prism with a Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }

Added in v1.0.0

composeFold (method)

compose a Prism with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeSetter (method)

compose a Prism with a Setter

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }

Added in v1.0.0

composeIso (method)

compose a Prism with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Prism<S, B> { ... }

Added in v1.0.0

composeLens (method)

compose a Prism with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Optional<S, B> { ... }

Added in v1.0.0

composeGetter (method)

compose a Prism with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Fold<S, B> { ... }

Added in v1.0.0

Setter (class)

Signature

export class Setter<S, A> {
  constructor(readonly modify: (f: (a: A) => A) => (s: S) => S) { ... }
  ...
}

Added in v1.0.0

set (method)

Signature

set(a: A): (s: S) => S { ... }

Added in v1.0.0

compose (method)

compose a Setter with a Setter

Signature

compose<B>(ab: Setter<A, B>): Setter<S, B> { ... }

Added in v1.0.0

composeSetter (method)

Alias of compose

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }

Added in v1.0.0

composeTraversal (method)

compose a Setter with a Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Setter<S, B> { ... }

Added in v1.0.0

composeOptional (method)

compose a Setter with a Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Setter<S, B> { ... }

Added in v1.0.0

composeLens (method)

compose a Setter with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Setter<S, B> { ... }

Added in v1.0.0

composePrism (method)

compose a Setter with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Setter<S, B> { ... }

Added in v1.0.0

composeIso (method)

compose a Setter with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Setter<S, B> { ... }

Added in v1.0.0

Traversal (class)

Signature

export class Traversal<S, A> {
  constructor(
    // Van Laarhoven representation
    readonly modifyF: ModifyF<S, A>
  ) { ... }
  ...
}

Added in v1.0.0

modify (method)

Signature

modify(f: (a: A) => A): (s: S) => S { ... }

Added in v1.0.0

set (method)

Signature

set(a: A): (s: S) => S { ... }

Added in v1.0.0

filter (method)

focus the items matched by a traversal to those that match a predicate

Signature

filter<B extends A>(refinement: Refinement<A, B>): Traversal<S, B>
filter(predicate: Predicate<A>): Traversal<S, A> { ... }

Example

import { fromTraversable, Lens } from 'monocle-ts'
import { array } from 'fp-ts/lib/Array'

interface Person {
  name: string
  cool: boolean
}

const peopleTraversal = fromTraversable(array)<Person>()
const coolLens = Lens.fromProp<Person>()('cool')
const people = [{ name: 'bill', cool: false }, { name: 'jill', cool: true }]

const actual = peopleTraversal
  .filter(p => p.name === 'bill')
  .composeLens(coolLens)
  .set(true)(people)

assert.deepStrictEqual(actual, [{ name: 'bill', cool: true }, { name: 'jill', cool: true }])

Added in v1.0.0

asFold (method)

view a Traversal as a Fold

Signature

asFold(): Fold<S, A> { ... }

Added in v1.0.0

asSetter (method)

view a Traversal as a Setter

Signature

asSetter(): Setter<S, A> { ... }

Added in v1.0.0

compose (method)

compose a Traversal with a Traversal

Signature

compose<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }

Added in v1.0.0

composeTraversal (method)

Alias of compose

Signature

composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }

Added in v1.0.0

composeFold (method)

compose a Traversal with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }

Added in v1.0.0

composeSetter (method)

compose a Traversal with a Setter

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }

Added in v1.0.0

composeOptional (method)

compose a Traversal with a Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Traversal<S, B> { ... }

Added in v1.0.0

composeLens (method)

compose a Traversal with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Traversal<S, B> { ... }

Added in v1.0.0

composePrism (method)

compose a Traversal with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Traversal<S, B> { ... }

Added in v1.0.0

composeIso (method)

compose a Traversal with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Traversal<S, B> { ... }

Added in v1.0.0

composeGetter (method)

compose a Traversal with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Fold<S, B> { ... }

Added in v1.0.0

fromFoldable (function)

create a Fold from a Foldable

Signature

export function fromFoldable<F extends URIS3>(F: Foldable3<F>): <U, L, A>() => Fold<Kind3<F, U, L, A>, A>
export function fromFoldable<F extends URIS2>(F: Foldable2<F>): <L, A>() => Fold<Kind2<F, L, A>, A>
export function fromFoldable<F extends URIS>(F: Foldable1<F>): <A>() => Fold<Kind<F, A>, A>
export function fromFoldable<F>(F: Foldable<F>): <A>() => Fold<HKT<F, A>, A> { ... }

Added in v1.0.0

fromTraversable (function)

create a Traversal from a Traversable

Signature

export function fromTraversable<T extends URIS3>(T: Traversable3<T>): <U, L, A>() => Traversal<Kind3<T, U, L, A>, A>
export function fromTraversable<T extends URIS2>(T: Traversable2<T>): <L, A>() => Traversal<Kind2<T, L, A>, A>
export function fromTraversable<T extends URIS>(T: Traversable1<T>): <A>() => Traversal<Kind<T, A>, A>
export function fromTraversable<T>(T: Traversable<T>): <A>() => Traversal<HKT<T, A>, A> { ... }

Example

import { Lens, fromTraversable } from 'monocle-ts'
import { array } from 'fp-ts/lib/Array'

interface Tweet {
  text: string
}

interface Tweets {
  tweets: Tweet[]
}

const tweetsLens = Lens.fromProp<Tweets>()('tweets')
const tweetTextLens = Lens.fromProp<Tweet>()('text')
const tweetTraversal = fromTraversable(array)<Tweet>()
const composedTraversal = tweetsLens.composeTraversal(tweetTraversal).composeLens(tweetTextLens)

const tweet1: Tweet = { text: 'hello world' }
const tweet2: Tweet = { text: 'foobar' }
const model: Tweets = { tweets: [tweet1, tweet2] }

const actual = composedTraversal.modify(text =>
  text
    .split('')
    .reverse()
    .join('')
)(model)

assert.deepStrictEqual(actual, { tweets: [{ text: 'dlrow olleh' }, { text: 'raboof' }] })

Added in v1.0.0