本文整理汇总了TypeScript中funds/optics.PPrism类的典型用法代码示例。如果您正苦于以下问题:TypeScript PPrism类的具体用法?TypeScript PPrism怎么用?TypeScript PPrism使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PPrism类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: describe
describe('Prisms', () => {
const lp: PPrism<Either<number, Labelled<number>>, string, Labelled<number>, number> =
mkPrism(
(eln: Either<number, Labelled<number>>) => eln.leftMap(n => `Extraction failed: ${n}`),
(n: number) => `Upcasted ${n}`)
describe('perform', () => {
it('extraction', () => {
expect(lp.preview(left(4))).to.eql(left('Extraction failed: 4'))
expect(lp.preview(right(lab4))).to.eql(right(lab4))
})
it('upcasting', () => {
expect(lp.re(4)).to.eql('Upcasted 4')
})
})
describe('compose', () => {
it('with prisms', () => {
const zero: PPrism<Labelled<number>, number, string, number> =
mkPrism<Labelled<number>, number, string, number>(
(ln: Labelled<number>) => ln.x === 0 ? right(ln.label) : left(ln.x),
identity as (n: number) => number
)
const comp = lp.compose(zero)
const preview = comp.preview
expect(preview(right(lab4))).to.eql(left('Upcasted 4'))
expect(preview(right({ x: 0, label: 'fake' }))).to.eql(right('fake'))
expect(preview(left(7))).to.eql(left('Extraction failed: 7'))
})
it('with setters', () => {
const labelledToNum = mkSetter(
(f: (label: string) => number) => (ln: Labelled<number>) => f(ln.label) + ln.x)
const comp = lp.compose(labelledToNum)
const strlen = (s: string) => s.length
expect(comp.modify(strlen, left(7))).to.eql('Extraction failed: 7')
expect(comp.modify(strlen, right(lab4))).to.eql('Upcasted 8')
expect(comp.set(7, left(5))).to.eql('Extraction failed: 5')
expect(comp.set(7, right(lab4))).to.eql('Upcasted 11')
})
})
})
示例2: it
it('with setters', () => {
const labelledToNum = mkSetter(
(f: (label: string) => number) => (ln: Labelled<number>) => f(ln.label) + ln.x)
const comp = lp.compose(labelledToNum)
const strlen = (s: string) => s.length
expect(comp.modify(strlen, left(7))).to.eql('Extraction failed: 7')
expect(comp.modify(strlen, right(lab4))).to.eql('Upcasted 8')
expect(comp.set(7, left(5))).to.eql('Extraction failed: 5')
expect(comp.set(7, right(lab4))).to.eql('Upcasted 11')
})