!SLIDE
Lens & Prism 勉強会 2015/05/30
!SLIDE
- 中村 学
- @gakuzzzz
- 株式会社 Tech to Value
- Scala関西 Summit 2015 スポンサーしてます
- Scala Matsuri 今年もやります
!SLIDE
<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>- Julien Truffaut さん
- Monocle の作者
!SLIDE
!SLIDE
!SLIDE
- 完全性の保証
- ネストした構造内の値束縛
!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 に合成すれば可能?
この辺よくわからないので識者の方教えてください。