Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fun of Types

Fun of Types

B936321bf044487eaf990b605a8826d3?s=128

Ryosuke TAKASHIMA

February 22, 2019
Tweet

Transcript

  1. ͨͷ͍͠ܕ ج൫(ɹߴౡ྄༞

  2. ࣗݾ঺հ w ߴౡ྄༞ʢ!STUʣ w ޷͖ͳ΋ͷɿখઆɺϚϯΨɺΞχϝʢ4'த৺ʣ
 ΧϥΦέɺຑ੃ɺόεέ w

  3. ຋༁ w ʰϫϯεɾΞϙϯɾΞϯɾΞϧΰ ϦζϜʱͱ͍͏ຊΛ༁͠·ͨ͠ ʢʣ
 IUUQTXXXBNB[PODPKQEQ  w l*NQSBDUJDBM1ZUIPO1SPKFDUT 1MBZGVM1SPHSBNNJOH"DUJWJUJFT

    UP.BLF:PV4NBSUFSz
 ͱ͍͏ຊΛ຋༁༧ఆ
 ϨϏϡΞืूதͰ͢ʂ
  4. ܕ w ࠷ۙ͸੩తܕ෇͚͕ਓؾʁʢ5ZQF4DSJQUɺ(Pʣ w ܕͷ͋Δݴޠɾܕͷແ͍ݴޠ w ܕ͸ͨͷ͍͠ʂʂʂ

  5. ܕͷ֊૚ʢ+BWBʣ Collection List Set ArrayList LinkedList HashSet TreeSet add(E e)

    get(int index)
  6. ܕͷ֊૚ʢ)BTLFMMʣ Functor Applicative
 Functor Monad [] Maybe IO r ->

    fmap (<$>) (>>=) Ϧετ ೖग़ྗ ؔ਺ ͍ΘΏΔ0QUJPOBM
  7. 'VODUPS GNBQ ͷྫ fmap (show . (* 3)) [1..4] --

    3ഒͯ͠จࣈྻʹม׵
 => ["3", "6", "9", "12"] fmap fromEnum (Just 'F') -- ίʔυϙΠϯτΛऔಘ
 => Just 70 fmap fromEnum Nothing -- த਎͕ͳ͚Ε͹Կ΋͠ͳ͍
 => Nothing
  8. 'VODUPS GNBQ ͷղऍ w 'VODUPSΛശͱଊ͑Δͱɺ
 GNBQ͸ശͷܗΛม͑ͣʹத਎Λม͑Δૢ࡞ fmap :: Functor f

    => (a -> b) -> f a -> f b
 fmap :: (a -> b) -> [a] -> [b]
 fmap :: (a -> b) -> Maybe a -> Maybe b
  9. 'VODUPSͱͯ͠ͷؔ਺ w ؔ਺SB΋'VODUPSʢSΛ౉͢ͱBΛฦͯ͘͠ΕΔശʁʣ
 Ͱ͸GNBQ͸Կʁ fmap :: Functor f => (a

    -> b) -> f a -> f b
 fmap :: (a -> b) -> (r -> a) -> (r -> b) fmap = (.) fmap (fmap show (* 3)) [1..4] w SBͱBCΛͭͳ͛Δؔ਺߹੒ʂ w ͔ͩΒ͜͏΋ॻ͚Δ
  10. .POBEͱ  w ҎԼͷͭͷίʔυ͸౳Ձ w Ұͭ໨ͷ݁Ռ͸ແࢹͯ͠ɺೋͭ໨Λऔಘ print [1..4] >> print

    "hoge" do
 print [1..4]
 print "hoge" (>>) :: Monad m => m a -> m b -> m b
 (>>) :: IO a -> IO b -> IO b
  11. .POBEͱͯ͠ͷϦετ w ҎԼ͸Ͳ͏ͳΔʁ w  ͸  ͭ·ΓqBU.BQͷಛघܗͳͷͰʜ
 ͪͳΈʹҎԼͷΑ͏ʹ΋ॻ͚Δ (>>)

    :: Monad m => m a -> m b -> m b
 (>>) :: [a] -> [b] -> [b] [1..4] >> "hoge" do
 [1..4]
 "hoge"
  12. ·ͱΊ w Ϧετ΋ೖग़ྗ΋ؔ਺΋ಉ͡΋ͷ w ܕ͸ͨͷ͍͠ʂʂ)BTLFMM΋ͨͷ͍͠ʂʂ w ܕ͕ແ͍ੈքͷ࿩͸ผͷػձʹ