當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。