Fold
A Fold<S, A> extracts zero or more values from S.
type Fold<S, A> = {
readonly _tag: 'fold'
readonly getAll: (s: S) => ReadonlyArray<A>
}
Create A Fold
import { Fold } from '@fuiste/optics'
const words = Fold<string, string>((text) => text.split(' '))
words.getAll('hello world')
// ['hello', 'world']
Folds From Composition
Read-only and partial composition often produces a Fold.
import { Getter, Lens, Prism, compose } from '@fuiste/optics'
const address = Prism<Person>().of({
get: (person) => person.address,
set: (address) => (person) => ({ ...person, address }),
})
const city = Getter<Address, string>((value) => value.city)
const maybeCity = compose(address, city)
// Fold<Person, string>
Notes
- Folds do not have
setormodify. - Anything composed with a
Foldbecomes aFold. - Use a fold when extraction is the point and mutation would be fiction.