Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Scalaz talk
Search
George Leontiev
April 17, 2013
Technology
530
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Scalaz talk
Berlin, April 17
George Leontiev
April 17, 2013
More Decks by George Leontiev
See All by George Leontiev
GraphQL at Twitter
folone
0
180
Typelevel programming in Scala (with lies)
folone
0
490
Case study: typelevel programming in real world
folone
1
650
Type level programming in Scala
folone
0
530
ScalaUA: Typelevel Scala
folone
2
240
Hands-on TLC
folone
2
1k
42: Rise of the dependent types
folone
2
500
∃ PROLOG |PROLOG ∈ SCALA
folone
5
1.2k
42.type: Literal-based Singleton types
folone
4
1.6k
Other Decks in Technology
See All in Technology
AIのReact習熟度を測る
uhyo
2
680
Flow 不死:AI 時代 DevOps 的不變本質
cheng_wei_chen
2
510
[AWS Summit Japan 2026]迷っているあなたへ_小さな一歩が、やがて自分を助けてくれる
sh_fk2
2
410
元・セキュリティ学習経験0大学生による業務紹介 / An Introduction to the Job by a Former College Student with Zero Security Training Experience
nttcom
0
150
ぼっちではじめた登壇が「51名」「241件」の発信に化けた
subroh0508
1
310
【Snowflake Summit 2026 Recap!!】Snowflake Summit Deep Dive: Security & Governance
civitaspo
1
310
アラート調査向けAIエージェントの本番導入とその後/AI Agents for Alert Investigation: Production Deployment and After
taddy_919
0
140
水を運ぶ人としてのリーダーシップ
izumii19
4
1k
AIペネトレーションテスト・ セキュリティ検証「AgenticSec」紹介資料
laysakura
2
7.5k
気軽に使える"情報のハブ"としてのNotion活用 〜フロー情報の集積点 と、 Claude Code × Notion AI〜
syucream
1
200
iOS アプリの「これって不具合ですか?」を AI に調べてもらう
miichan
0
140
MySQL & MySQL HeatWave Report - June 2026
freshdaz
0
120
Featured
See All Featured
Designing for humans not robots
tammielis
254
26k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.4k
The untapped power of vector embeddings
frankvandijk
2
1.8k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
250
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
200
How GitHub (no longer) Works
holman
316
150k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Transcript
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . . . . . . . . Scalaz Learn You Yet Another Real World Gentle Haskell (LYYARWGH) ((c) sproingie) George Leon ev deltamethod GmbH April 17, 2013 (λx.folonexlambda-calcul.us)@ folone.info George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 1 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . This talk https://github.com/folone/scalaz-talk-berlin https://speakerdeck.com/folone/scalaz-talk George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 2 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Agenda Some hotness without context, to draw a en on (Op on, Boolean, Memo) Typeclasses Monoid Functor, Applica ve, Monad Effects scalaz 6 vs seven typelevel.scala George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 3 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . What is scalaz Purely func onal datatypes (Fingertree, HList, DList, Trees, Zippers, Nel, ImmutableArray) Typeclasses Effects Concurrency George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 4 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Examples -- typesafe equals s> "" == 5 res0: Boolean = false s> "" === 5 <console>:14: error: type mismatch; found : Int(5) required: java.lang.String "" === 5 ^ <spoiler>∀ stuff ∈ scalaz ≡ scala.stdlib | stuff is typesafe ∨ stuff is strict</spoiler> George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 5 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Examples -- op ons s> some(5) getOrElse 0 res1: Int = 5 s> some(5) | 0 res2: Int = 5 s> some(1) getOrElse "ok" res3: Any = 1 s> some(1) | "ok" <console>:14: error: type mismatch; found : java.lang.String("ok") required: Int some(1) | "ok" ^ s> ~some(5) // Monoids res4: Int = 5 s> ~none[Int] // NB: Beware of unary_~ on Validations (swap res5: Int = 0 George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 6 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Examples -- op ons II // Smart constructors s> :t Some(1) s> :t None Some[Int] None.type s> :t some(1) s> :t none[Int] Option[Int] Option[Int] s> List(Some(1),None).foldLeft(None){(_, v) => v} <console>:14: error: type mismatch; found : v.type (with underlying type Option[Int]) required: None.type List(Some(1),None).foldLeft(None){(_, v) => v} ^ s> List(Some(1),None).foldLeft(none[Int]){(_, v) => v} res11: Option[Int] = None George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 7 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Examples -- booleans scala> true ? println("true") | println("false") true scala> true ?? 5 scala> true !? 5 res14: Int = 5 res15: Int = 0 scala> false ?? 5 scala> false !? 5 res15: Int = 0 res17: Int = 5 George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 8 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Examples -- func on composi on val a = (_:Int) + 6 val b = (_:Int).toString val c = (_:String).length scala> 5 |> a |> b |> c res18: Int = 2 scala> //(c · b · a) apply 5 // contramap res19: Int = 2 scala> 5 |> //(a ◦ b ◦ c) // map res20: Int = 2 // contramap === flip . map George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 9 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Examples -- Memo def func(s: String) = // Expensive computation scala> Memo.immutableHashMapMemo(func) res11: String => java.lang.String = <function1> // Different strategies mutableHashMapMemo arrayMemo // sized immutableListMemo immutableTreeMapMemo doubleArrayMemo // memoizing Double results != sentinel weakHashMapMemo // GC George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 10 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Examples -- Trampoline def even(n: Int): Boolean = if (n == 0) true else odd(n - 1) def odd(n: Int): Boolean = if (n == 0) false else even(n - 1) scala> even(30000) George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 11 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Examples -- Trampoline def even(n: Int): Trampoline[Boolean] = if (n == 0) done(true) else suspend(odd(n - 1)) def odd(n: Int): Trampoline[Boolean] = if (n == 0) done(false) else suspend(even(n - 1)) scala> even(30000).run George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 12 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Examples -- Trampoline def fibRec(n: Int): Int = if (n < 2) n else fibRec(n - 1) + fibRec(n - 2) def fibTailrec(n: Int) = { def loop(n: Int, next: Int, result: Int) = n match { case 0 => result case _ => loop(n - 1, next + result, next) } loop(n, 1, 0) } George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 13 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Examples -- Trampoline def fibTramp(n: Int): Trampoline[Int] = if (n < 2) done(n) else suspend { for { i <- fibTramp(n - 1) j <- fibTramp(n - 2) } yield i + j } // Continuation monad magic Consult @runarorama's paper "Stackless Scala with Free Monads" George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 14 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Typeclasses George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 15 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Typeclasses George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 16 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Typeclasses http://www.haskell.org/haskellwiki/Typeclassopedia http://typeclassopedia.bitbucket.org/ George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 17 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Typeclasses A monoid generalizes the (++) opera on. A functor generalises the map opera on. An applica ve functor generalizes the zip (or zipWith) opera on. A monad generalizes the concat opera on. http://stackoverflow.com/a/15727162/163423 George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 18 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Monoids (S, ⊗, 1) ∀a, b ∈ S : a ⊗ b ∈ S ∀a, b, c ∈ S : (a ⊗ b) ⊗ c = a ⊗ (b ⊗ c) ∀a ∈ S : 1 ⊗ a = a ⊗ 1 = a trait Semigroup[F] { def append(a1: F, a2: F): F } trait Monoid[F] extends Semigroup[F] { def zero: F } // scalacheck-binding import scalaz.scalacheck.ScalazProperties._ semigroup.laws[Int] monoid.laws[String] George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 19 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Monoids scala> 1 |+| 5 res2: Int = 6 scala> Multiplication(2) |+| Multiplication(3) res4: Int @@ Multiplication = 6 scala> some(1) |+| some(5) res5: Option[Int] = Some(6) // Monoids beget monoids scala> some(some((1, "OH ", 1 + (_:Int)))) |+| some(some((4, "HAI", 2 * (_:Int)))) res6: Option[Option[(Int, java.lang.String, Int => Int)]] = Some(Some((5, OH HAI, <function1>))) George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 20 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Monoids scala> List(1,2,3).suml res16: Int = 6 scala> List("OH ", "HAI", "!").suml res17: java.lang.String = OH HAI! George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 21 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Functors trait Functor[F[_]] { def fmap[A, B](f: A => B): F[A] => F[B] } scala> some(3) map(_.toString) res13: Option[java.lang.String] = Some(3) George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 22 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Applica ves trait Applicative[T[_]] extends Functor[T] { def pure[A](a: A): T[A] def <*>[A, B](tf: T[A => B])(ta: T[A]): T[B] } scala> some(1) <*> some((_:Int) + 2) <*> some((_:Int) * 5 res10: Option[Int] = Some(15) scala> List(1,2) <*> List((_:Int) * 5, (_: Int) + 2) res12: List[Int] = List(5, 10, 3, 4) George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 23 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Applica ves scala> List(some(1), some(2), some(3)) res21: List[Option[Int]] = List(Some(1), Some(2), Some(3)) scala> .sequence res22: Option[List[Int]] = Some(List(1, 2, 3)) scala> res21.traverse(x => some(x)) // sequence . map res23: Option[List[Int]] = Some(List(1, 2, 3)) George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 24 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Monads trait Monad[M[_]] extends Applicative[M]{ def >>=[A, B](ma: M[A])(f: A => M[B]): M[B] } scala> for { | i <- List(1,2,3) | j <- List(4,5,6) | } yield i*j res15: List[Int] = List(4, 5, 6, 8, 10, 12, 12, 15, 18) George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 25 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . IO DEMO George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 26 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Scalaz 6 vs seven a-la-carte imports typeclass instances separated from instances tags law checking via scalacheck Isomorphisms Adjunc ons etc. Consult examples and tests. http://www.folone.info/blog/Scalaz-sevenMigration/ George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 27 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . typelevel.scala scalaz spire shapeless http://typelevel.org/ George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 28 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . Links learning scalaz day n: http://eed3si9n.com/category/tags/scala/scalaz Runar's talk on the Strange Loop conf last year: http://www.infoq.com/presentations/ Scalaz-Functional-Programming-in-Scala lambda-cats: http://spl.smugmug.com/gallery/13227630_j2MHcg/ George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 29 / 29
. . . .. . . . .. . .
. .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . .. . . . .. . . . . .. . . . .. . . . . .. . . . .. . . . .. . That's it Ques ons? George Leon ev (deltamethod GmbH) Scalaz April 17, 2013 30 / 29