本文整理匯總了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')
})