I went to the Typelevel Summit / NE Scala Symposium this week. It was my first time going to a Scala conference, ever. At this point Iâve been working in Scala professionally for about ~1.5 years.
My goal in attending NE Scala was to see what Scala looks like outside of my particular corner of the universe. I write Scala at Twitter, which has a particular style; and Iâve only worked on one team at Twitter, which makes my view even narrower. Scala is notoriously âflexibleâ as a language, i.e. put three Scala devs in a room and youâll have at least nine dialects. What does Scala look like when other people write it? What challenges and interesting problems do people have? What are people excited about?
It turns out that I had inadvertently picked a conference thatâs known for being a little more functional-programming-theory-heavy and less about âpragmaticâ Scala. One of the other attendees said to me, âI feel like I write blue-collar Scala compared to this.â
I have Strong Feelings about jargon. Jargon comes with an inherent cost. It needs to earn its place. (And it often does: jargon is necessary to discuss complex and domain-specific ideas.) ButâŠeh. There was a lot of jargon at this conference.
Hereâs some jargon that I heard at the conference that I didnât understand:
- monoids
- monads
- monadic
- free monads (sometimes with a capital-F)
- cats
- kittens
- spark
- pure
- suspend
- lift
- monad transformers
- applicatives
- functors
- endofunctors
- algebra
- trampoline
There were also some Scala concepts that Iâm familiar with but admittedly donât use often in my own code, like implicits and certain operators like ~>
, :+
, +:
, and |@|
. (These are really hard to Google for, btw, which is one of my biases against them.)
And again - I want to emphasize this - I manage to be a productive software engineer in Scala without these concepts. These are not mandatory bars to entry, at least in my workplace. But I wanted to learn more about them, so here we go. (Spoiler alert, it turns out that many of them were things I was already using but didnât know the names for. Neat.)
Shoutouts to the following people for teaching me things, sending me great links, etc:
Note: I probably get some things wrong below. Got more stuff for me to read, or thoughts? Iâm @brindelle on Twitter.
monoids
Apparently monoids are just things that 1. have an identity function (aka no-op) and 2. have an append function.
This sounds so simple. OK then!
(s/o to Kelley Robinsonâs talk )
monads
Iâve always been a bit embarrassed to admit that I donât really grok monads. The good news is that when you admit you donât understand something, people usually send you great links / answers.
@b0rk replied to me and was like, arenât monads just things with flatMap? Which was a bit of an ah-ha moment for me. I have plenty of experience working with things with flatMap! And thatâs clearly a pattern for which I donât currently have language. Hmmmm.
Libby Kent sent me a link to her talk, Monads Made Semi-Understandable. Best quote: âCan you explain what monads are? Are they a burrito?â
This was an even bigger ah-ha moment! Ah, yes, this is a thing Iâve worked with but only have an inferred, non-systematic understanding of.
Monads (in Scala) are things with flatMap and apply defined. In other words: monads let you compose functions for values in a context. Common monads are: Option, List, Try, Future.
flatMap lets you nest functions.
The Monad Laws, as defined by Libbyâs talk: they boil down to âdonât do weird shit inside of flaTMap or applyâ.
Identity Law:
monadA.flatMap(a => apply(a)) == monadA
Associative Law:
flatMap(f).flatMap(g) = flatMap(a => f(a).flatMap(g))
Googling this also led me to Monad laws in Scala which was useful.
Free monads
The first talk at NE Scala was by @djspiewak, in which he built up something called the Free monad from scratch. This was confusing because I had never heard of free monads before and didnât know what they were, or why they were useful or interesting or etc.
So! Today I watched Kelley Robinsonâs talk, Why the free Monad isnât free. The Free monad is apparently a pattern that lets you separate out function composition from computation/interpretation.
âFreeâ here means âunrestrictedâ, not âzero costâ.
Free monads should not lose any data during the composition (flatMap
) step. So we canât evaluate any functions as we do this. So we store up the data (e.g. the functions) as weâre building the structure; and then weâll evaluate it later.
Itâs not something thatâs âbuilt inâ to the language or anything like that; itâs a pattern for structuring how you handle stuff. Youâre delaying evaluation (aka side effects). There are implementations available in Scalaz and Cats.
Whatâs the point? Separating composition from computation seems really great for testing, since youâre separating out side effects from the wiring / composition step.
However, it involves a lot of boilerplate, and a steep learning curve. Means building up big interpreters, too. I hear that there are also some performance issues with this in Scala?
Free monoids
https://youtu.be/U0lK0hnbc4U?t=12m34s
Free from interpretation; donât lose input data when appending.
Example: list concatenation doesnât involve interpretation. Integer addition, on the other hand, loses information during the input step.
trampolining
Express it in a loop: use the heap instead of the stack.
I actually really like this phrase, itâs cute.
Cats
https://github.com/typelevel/cats
The tagline calls it a âlightweight, modular, and extensible library for functional programmingâ which tells me âŠ. absolutely nothing about what it is, except that itâs for functional programming. OK? Canât I do functional programming in a bunch of languages without a library?
Wait, just kidding, the README actually has a great introduction:
Cats is a library which provides abstractions for functional programming in Scala.
The name is a playful shortening of the word category.
Scala supports both object-oriented and functional programming, and this is reflected in the hybrid approach of the standard library. Cats augments the standard library with tools that further enable functional programming such as Validated, Monad, and Traverse. A broader goal of Cats is to provide a foundation for an ecosystem of pure, typeful libraries.
Given that Day 1 of the conference was the Typelevel Summit, it makes sense that there was much discussion of Cats.
Kittens
https://github.com/milessabin/kittens
This one has a better Github tagline: âAutomatic type class derivation for Catsâ.
OK! Cool. I like these puns but there were fewer cat GIFs during presentations than I would have liked.
Spark
From http://spark.apache.org/: âApache Sparkâą is a fast and general engine for large-scale data processing.â
The âkiller appâ for Scala, apparently. Interestingly, a lot of Spark code is apparently not very functional at all. (As usual, programming for data science looks different than other kinds of programming?) There were lots of Spark users in the audience.
pure
No side effects. Thatâs it??
suspend
This is a free monad thing, it means âdonât do the computation yetâ.
lift
This is what the apply method does in Scala: âliftâ a value into a ~monadic context~. I guess itâs sort of like putting something into a different abstraction?
From Libbyâs talk: apply lifts a value into a ~monadic context~ (aka the context of whatever you called .apply()
on). map
âliftsâ a function into the same.
monad transformers
Something that takes a monad and turns it into another monad?
applicatives
âA type which wraps a valueâ. More at this blog post explaining applicative functors.
functors
âA family of types that has implemented the map methodâ; thereâs an identity function, and also grouping doesnât matter.
Kelley Robinsonâs talk, again: https://youtu.be/U0lK0hnbc4U?t=5m21s
endofunctors
LaĆ Kelley Robinsonâs talk, again, âfunctors are endofunctorsâ in Scala.
âendoâ = mapping from one category to itself; in Scala that means mapping from a Scala type to a SCala type.
algebra
There is a very math-y usage of this word. I still havenât found a good concise definition for it.