Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Last active August 29, 2015 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gakuzzzz/58ae304329f2ce35d62c to your computer and use it in GitHub Desktop.
Save gakuzzzz/58ae304329f2ce35d62c to your computer and use it in GitHub Desktop.
Lens&Prism勉強会

!SLIDE

Lens の説明

Lens & Prism 勉強会 2015/05/30

!SLIDE

自己紹介

!SLIDE

Beyond Scala Lenses

<iframe src="//www.slideshare.net/slideshow/embed_code/key/ebySdJ4TtPEPWe" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe>

!SLIDE

Prism と Pattern Match

!SLIDE

!SLIDE

Pattern Match のメリット

  • 完全性の保証
  • ネストした構造内の値束縛

!SLIDE

完全性の保証

構造毎に type family にする事で完全性を保証できる、らしい?

http://www.haskellforall.com/2015/01/total-100-exhaustive-pattern-matching.html

!SLIDE

{-# LANGUAGE DeriveGeneric   #-}
{-# LANGUAGE TemplateHaskell #-}

import Control.Lens.TH (makePrisms)
import GHC.Generics (Generic)
import Lens.Family.Total

data Example a b c = C1 a | C2 b | C3 c deriving (Generic)

makePrisms ''Example

instance (Empty a, Empty b, Empty c) => Empty (Example a b c)

example :: Example String Char Int -> String  -- Same as:
example = _case                               -- example = \case
    & on _C1 (\s -> s              )          --     C1 s -> s
    & on _C2 (\c -> replicate 3  c )          --     C2 c -> replicate 3  c
    & on _C3 (\n -> replicate n '!')          --     C3 n -> replicate n '!'

!SLIDE

パターンガードを使うような任意の式を実行する Prism では 完全性を保証できなそう

!SLIDE

ネストした構造内の値束縛

case class A(left: Int, right: String)
case class B(value: Int, a: A)
case class C(value: Int, b: B)
x match {
  case C(v, B(_, A(left, right)) => a + head
}

!SLIDE

Monadic に合成すれば可能?

 

この辺よくわからないので識者の方教えてください。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment