title | nav_order | parent |
---|---|---|
index.ts |
4 |
Modules |
Added in v1.0.0
- LensFromPath (interface)
- ModifyF (interface)
- At (class)
- Fold (class)
- Getter (class)
- Index (class)
- Iso (class)
- reverse (method)
- modify (method)
- asLens (method)
- asPrism (method)
- asOptional (method)
- asTraversal (method)
- asFold (method)
- asGetter (method)
- asSetter (method)
- compose (method)
- composeIso (method)
- composeLens (method)
- composePrism (method)
- composeOptional (method)
- composeTraversal (method)
- composeFold (method)
- composeGetter (method)
- composeSetter (method)
- Lens (class)
- fromPath (static method)
- fromProp (static method)
- fromProps (static method)
- fromNullableProp (static method)
- modify (method)
- asOptional (method)
- asTraversal (method)
- asSetter (method)
- asGetter (method)
- asFold (method)
- compose (method)
- composeLens (method)
- composeGetter (method)
- composeFold (method)
- composeOptional (method)
- composeTraversal (method)
- composeSetter (method)
- composeIso (method)
- composePrism (method)
- Optional (class)
- fromNullableProp (static method)
- fromOptionProp (static method)
- modify (method)
- modifyOption (method)
- asTraversal (method)
- asFold (method)
- asSetter (method)
- compose (method)
- composeOptional (method)
- composeTraversal (method)
- composeFold (method)
- composeSetter (method)
- composeLens (method)
- composePrism (method)
- composeIso (method)
- composeGetter (method)
- Prism (class)
- fromPredicate (static method)
- some (static method)
- modify (method)
- modifyOption (method)
- set (method)
- asOptional (method)
- asTraversal (method)
- asSetter (method)
- asFold (method)
- compose (method)
- composePrism (method)
- composeOptional (method)
- composeTraversal (method)
- composeFold (method)
- composeSetter (method)
- composeIso (method)
- composeLens (method)
- composeGetter (method)
- Setter (class)
- Traversal (class)
- fromFoldable (function)
- fromTraversable (function)
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
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
Signature
export class At<S, I, A> {
constructor(readonly at: (i: I) => Lens<S, A>) { ... }
...
}
Added in v1.2.0
lift an instance of At
using an Iso
Signature
fromIso<T>(iso: Iso<T, S>): At<T, I, A> { ... }
Added in v1.2.0
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 a Fold
with a Fold
Signature
compose<B>(ab: Fold<A, B>): Fold<S, B> { ... }
Added in v1.0.0
Alias of compose
Signature
composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Fold
with a Getter
Signature
composeGetter<B>(ab: Getter<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Fold
with a Traversal
Signature
composeTraversal<B>(ab: Traversal<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Fold
with a Optional
Signature
composeOptional<B>(ab: Optional<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Fold
with a Lens
Signature
composeLens<B>(ab: Lens<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Fold
with a Prism
Signature
composePrism<B>(ab: Prism<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Fold
with a Iso
Signature
composeIso<B>(ab: Iso<A, B>): Fold<S, B> { ... }
Added in v1.0.0
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
get the first target of a Fold
Signature
headOption(s: S): Option<A> { ... }
Added in v1.0.0
Signature
export class Getter<S, A> {
constructor(readonly get: (s: S) => A) { ... }
...
}
Added in v1.0.0
view a Getter
as a Fold
Signature
asFold(): Fold<S, A> { ... }
Added in v1.0.0
compose a Getter
with a Getter
Signature
compose<B>(ab: Getter<A, B>): Getter<S, B> { ... }
Added in v1.0.0
Alias of compose
Signature
composeGetter<B>(ab: Getter<A, B>): Getter<S, B> { ... }
Added in v1.0.0
compose a Getter
with a Fold
Signature
composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Getter
with a Lens
Signature
composeLens<B>(ab: Lens<A, B>): Getter<S, B> { ... }
Added in v1.0.0
compose a Getter
with a Iso
Signature
composeIso<B>(ab: Iso<A, B>): Getter<S, B> { ... }
Added in v1.0.0
compose a Getter
with a Optional
Signature
composeTraversal<B>(ab: Traversal<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Getter
with a Optional
Signature
composeOptional<B>(ab: Optional<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Getter
with a Prism
Signature
composePrism<B>(ab: Prism<A, B>): Fold<S, B> { ... }
Added in v1.0.0
Signature
export class Index<S, I, A> {
constructor(readonly index: (i: I) => Optional<S, A>) { ... }
...
}
Added in v1.2.0
Signature
static fromAt<T, J, B>(at: At<T, J, Option<B>>): Index<T, J, B> { ... }
Added in v1.2.0
lift an instance of Index
using an Iso
Signature
fromIso<T>(iso: Iso<T, S>): Index<T, I, A> { ... }
Added in v1.2.0
Laws:
- reverseGet(get(s)) = s
- 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 the Iso
: the source becomes the target and the target becomes the source
Signature
reverse(): Iso<A, S> { ... }
Added in v1.0.0
Signature
modify(f: (a: A) => A): (s: S) => S { ... }
Added in v1.0.0
view an Iso
as a Lens
Signature
asLens(): Lens<S, A> { ... }
Added in v1.0.0
view an Iso
as a Prism
Signature
asPrism(): Prism<S, A> { ... }
Added in v1.0.0
view an Iso
as a Optional
Signature
asOptional(): Optional<S, A> { ... }
Added in v1.0.0
view an Iso
as a Traversal
Signature
asTraversal(): Traversal<S, A> { ... }
Added in v1.0.0
view an Iso
as a Fold
Signature
asFold(): Fold<S, A> { ... }
Added in v1.0.0
view an Iso
as a Getter
Signature
asGetter(): Getter<S, A> { ... }
Added in v1.0.0
view an Iso
as a Setter
Signature
asSetter(): Setter<S, A> { ... }
Added in v1.0.0
compose an Iso
with an Iso
Signature
compose<B>(ab: Iso<A, B>): Iso<S, B> { ... }
Added in v1.0.0
Alias of compose
Signature
composeIso<B>(ab: Iso<A, B>): Iso<S, B> { ... }
Added in v1.0.0
compose an Iso
with a Lens
Signature
composeLens<B>(ab: Lens<A, B>): Lens<S, B> { ... }
Added in v1.0.0
compose an Iso
with a Prism
Signature
composePrism<B>(ab: Prism<A, B>): Prism<S, B> { ... }
Added in v1.0.0
compose an Iso
with an Optional
Signature
composeOptional<B>(ab: Optional<A, B>): Optional<S, B> { ... }
Added in v1.0.0
compose an Iso
with a Traversal
Signature
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }
Added in v1.0.0
compose an Iso
with a Fold
Signature
composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose an Iso
with a Getter
Signature
composeGetter<B>(ab: Getter<A, B>): Getter<S, B> { ... }
Added in v1.0.0
compose an Iso
with a Setter
Signature
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }
Added in v1.0.0
Laws:
- get(set(a)(s)) = a
- set(get(s))(s) = s
- 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
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
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
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
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
Signature
modify(f: (a: A) => A): (s: S) => S { ... }
Added in v1.0.0
view a Lens
as a Optional
Signature
asOptional(): Optional<S, A> { ... }
Added in v1.0.0
view a Lens
as a Traversal
Signature
asTraversal(): Traversal<S, A> { ... }
Added in v1.0.0
view a Lens
as a Setter
Signature
asSetter(): Setter<S, A> { ... }
Added in v1.0.0
view a Lens
as a Getter
Signature
asGetter(): Getter<S, A> { ... }
Added in v1.0.0
view a Lens
as a Fold
Signature
asFold(): Fold<S, A> { ... }
Added in v1.0.0
compose a Lens
with a Lens
Signature
compose<B>(ab: Lens<A, B>): Lens<S, B> { ... }
Added in v1.0.0
Alias of compose
Signature
composeLens<B>(ab: Lens<A, B>): Lens<S, B> { ... }
Added in v1.0.0
compose a Lens
with a Getter
Signature
composeGetter<B>(ab: Getter<A, B>): Getter<S, B> { ... }
Added in v1.0.0
compose a Lens
with a Fold
Signature
composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Lens
with an Optional
Signature
composeOptional<B>(ab: Optional<A, B>): Optional<S, B> { ... }
Added in v1.0.0
compose a Lens
with an Traversal
Signature
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }
Added in v1.0.0
compose a Lens
with an Setter
Signature
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }
Added in v1.0.0
compose a Lens
with an Iso
Signature
composeIso<B>(ab: Iso<A, B>): Lens<S, B> { ... }
Added in v1.0.0
compose a Lens
with a Prism
Signature
composePrism<B>(ab: Prism<A, B>): Optional<S, B> { ... }
Added in v1.0.0
Laws:
- getOption(s).fold(() => s, a => set(a)(s)) = s
- getOption(set(a)(s)) = getOption(s).map(_ => a)
- 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
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
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
Signature
modify(f: (a: A) => A): (s: S) => S { ... }
Added in v1.0.0
Signature
modifyOption(f: (a: A) => A): (s: S) => Option<S> { ... }
Added in v1.0.0
view a Optional
as a Traversal
Signature
asTraversal(): Traversal<S, A> { ... }
Added in v1.0.0
view an Optional
as a Fold
Signature
asFold(): Fold<S, A> { ... }
Added in v1.0.0
view an Optional
as a Setter
Signature
asSetter(): Setter<S, A> { ... }
Added in v1.0.0
compose a Optional
with a Optional
Signature
compose<B>(ab: Optional<A, B>): Optional<S, B> { ... }
Added in v1.0.0
Alias of compose
Signature
composeOptional<B>(ab: Optional<A, B>): Optional<S, B> { ... }
Added in v1.0.0
compose an Optional
with a Traversal
Signature
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }
Added in v1.0.0
compose an Optional
with a Fold
Signature
composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose an Optional
with a Setter
Signature
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }
Added in v1.0.0
compose an Optional
with a Lens
Signature
composeLens<B>(ab: Lens<A, B>): Optional<S, B> { ... }
Added in v1.0.0
compose an Optional
with a Prism
Signature
composePrism<B>(ab: Prism<A, B>): Optional<S, B> { ... }
Added in v1.0.0
compose an Optional
with a Iso
Signature
composeIso<B>(ab: Iso<A, B>): Optional<S, B> { ... }
Added in v1.0.0
compose an Optional
with a Getter
Signature
composeGetter<B>(ab: Getter<A, B>): Fold<S, B> { ... }
Added in v1.0.0
Laws:
- getOption(s).fold(s, reverseGet) = s
- 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
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
Signature
static some<A>(): Prism<Option<A>, A> { ... }
Added in v1.0.0
Signature
modify(f: (a: A) => A): (s: S) => S { ... }
Added in v1.0.0
Signature
modifyOption(f: (a: A) => A): (s: S) => Option<S> { ... }
Added in v1.0.0
set the target of a Prism
with a value
Signature
set(a: A): (s: S) => S { ... }
Added in v1.0.0
view a Prism
as a Optional
Signature
asOptional(): Optional<S, A> { ... }
Added in v1.0.0
view a Prism
as a Traversal
Signature
asTraversal(): Traversal<S, A> { ... }
Added in v1.0.0
view a Prism
as a Setter
Signature
asSetter(): Setter<S, A> { ... }
Added in v1.0.0
view a Prism
as a Fold
Signature
asFold(): Fold<S, A> { ... }
Added in v1.0.0
compose a Prism
with a Prism
Signature
compose<B>(ab: Prism<A, B>): Prism<S, B> { ... }
Added in v1.0.0
Alias of compose
Signature
composePrism<B>(ab: Prism<A, B>): Prism<S, B> { ... }
Added in v1.0.0
compose a Prism
with a Optional
Signature
composeOptional<B>(ab: Optional<A, B>): Optional<S, B> { ... }
Added in v1.0.0
compose a Prism
with a Traversal
Signature
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }
Added in v1.0.0
compose a Prism
with a Fold
Signature
composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Prism
with a Setter
Signature
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }
Added in v1.0.0
compose a Prism
with a Iso
Signature
composeIso<B>(ab: Iso<A, B>): Prism<S, B> { ... }
Added in v1.0.0
compose a Prism
with a Lens
Signature
composeLens<B>(ab: Lens<A, B>): Optional<S, B> { ... }
Added in v1.0.0
compose a Prism
with a Getter
Signature
composeGetter<B>(ab: Getter<A, B>): Fold<S, B> { ... }
Added in v1.0.0
Signature
export class Setter<S, A> {
constructor(readonly modify: (f: (a: A) => A) => (s: S) => S) { ... }
...
}
Added in v1.0.0
Signature
set(a: A): (s: S) => S { ... }
Added in v1.0.0
compose a Setter
with a Setter
Signature
compose<B>(ab: Setter<A, B>): Setter<S, B> { ... }
Added in v1.0.0
Alias of compose
Signature
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }
Added in v1.0.0
compose a Setter
with a Traversal
Signature
composeTraversal<B>(ab: Traversal<A, B>): Setter<S, B> { ... }
Added in v1.0.0
compose a Setter
with a Optional
Signature
composeOptional<B>(ab: Optional<A, B>): Setter<S, B> { ... }
Added in v1.0.0
compose a Setter
with a Lens
Signature
composeLens<B>(ab: Lens<A, B>): Setter<S, B> { ... }
Added in v1.0.0
compose a Setter
with a Prism
Signature
composePrism<B>(ab: Prism<A, B>): Setter<S, B> { ... }
Added in v1.0.0
compose a Setter
with a Iso
Signature
composeIso<B>(ab: Iso<A, B>): Setter<S, B> { ... }
Added in v1.0.0
Signature
export class Traversal<S, A> {
constructor(
// Van Laarhoven representation
readonly modifyF: ModifyF<S, A>
) { ... }
...
}
Added in v1.0.0
Signature
modify(f: (a: A) => A): (s: S) => S { ... }
Added in v1.0.0
Signature
set(a: A): (s: S) => S { ... }
Added in v1.0.0
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
view a Traversal
as a Fold
Signature
asFold(): Fold<S, A> { ... }
Added in v1.0.0
view a Traversal
as a Setter
Signature
asSetter(): Setter<S, A> { ... }
Added in v1.0.0
compose a Traversal
with a Traversal
Signature
compose<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }
Added in v1.0.0
Alias of compose
Signature
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> { ... }
Added in v1.0.0
compose a Traversal
with a Fold
Signature
composeFold<B>(ab: Fold<A, B>): Fold<S, B> { ... }
Added in v1.0.0
compose a Traversal
with a Setter
Signature
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> { ... }
Added in v1.0.0
compose a Traversal
with a Optional
Signature
composeOptional<B>(ab: Optional<A, B>): Traversal<S, B> { ... }
Added in v1.0.0
compose a Traversal
with a Lens
Signature
composeLens<B>(ab: Lens<A, B>): Traversal<S, B> { ... }
Added in v1.0.0
compose a Traversal
with a Prism
Signature
composePrism<B>(ab: Prism<A, B>): Traversal<S, B> { ... }
Added in v1.0.0
compose a Traversal
with a Iso
Signature
composeIso<B>(ab: Iso<A, B>): Traversal<S, B> { ... }
Added in v1.0.0
compose a Traversal
with a Getter
Signature
composeGetter<B>(ab: Getter<A, B>): Fold<S, B> { ... }
Added in v1.0.0
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
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