当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript optics.PPrism类代码示例

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

  })
})
开发者ID:WojciechP,项目名称:funds,代码行数:40,代码来源:prism.ts

示例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')
 })
开发者ID:WojciechP,项目名称:funds,代码行数:10,代码来源:prism.ts


注:本文中的funds/optics.PPrism类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。