I want to talk about a comonad that came up at work the other day. Actually, two of them, as the data structure in question is a comonad in at least two ways, and the issue that came up is related to the difference between those two comonads. This post is sort of a continuation of the Comonad Tutorial , and we can call this “part 3”. I’m going to assume the reader has a basic…
I’ve been having fun exploring adjunctions lately and thinking about how we can take a monad apart and compose it the other way to get a comonad, and vice versa. Often I’ll find that a comonad counterpart of a given monad gives an interesting perspective on that monad, and ditto for a monad cousin to a given comonad. The monad for monoids Let’s take an example. There is a…
In the previous post , we looked at the Reader/Writer monads and comonads, and discussed in general what comonads are and how they relate to monads. This time around, we’re going to look at some more comonads, delve briefly into adjunctions, and try to get some further insight into what it all means. Nonempty structures Since a comonad has to have a counit , it must be “pointed”…
In writing up part 2 of my Scala Comonad Tutorial , and coming up with my talk for Scala World , I idly pondered this question: If all monads are given by composing adjoint pairs of functors, what adjoint pair of functors forms the `Reader` monad? And if we compose those functors the other way, which comonad do we get? Shachaf Ben-Kiki pointed out on IRC that there are at least two ways of doing…
In chapter 11 of our book , we talk about monads in Scala. This finally names a pattern that the reader has seen throughout the book and gives it a formal structure. We also give some intuition for what it means for something to be a monad. Once you have this concept, you start recognizing it everywhere in the daily business of programming. Today I want to talk about comonads , which are the dual…
I’ve found that if I’m using scala.concurrent.Future in my code, I can get some really easy performance gains by just switching to scalaz.concurrent.Task instead, particularly if I’m chaining them with map or flatMap calls, or with for comprehensions. Jumping into thread pools Every Future is basically some work that needs to be submitted to a thread pool. When you call…
After giving it a lot of thought I have come to the conclusion that I won’t be involved in “Functional Programming in Java” . There are many reasons, including that I just don’t think I can spend the time to make this a good book. Looking at all the things I have scheduled for the rest of the year, I can’t find the time to work on it. More depressingly, the thought of spending a year or more…
Our book, Functional Programming in Scala , relies heavily on exercises. Hints and answers for those exercises are not actually in the book, but are freely available on GitHub under a permissive MIT license. Likewise, we have written chapter notes that we reference throughout the book and made them available as a community-editable wiki . Naturally, readers get the most out of this book by…
Like a lot of people, I keep a list of books I want to read. And because there are a great many more books that interest me than I can possibly read in my lifetime, this list has become quite long. In the olden days of brick-and-mortar bookstores and libraries, I would discover books to read by browsing shelves and picking up what looked interesting at the time. I might even find something that I…
It’s well known that there is a trade-off in language and systems design between expressiveness and analyzability. That is, the more expressive a language or system is, the less we can reason about it, and vice versa. The more capable the system, the less comprehensible it is. This principle is very widely applicable, and it’s a useful thing to keep in mind when designing languages and…
Today I disabled my Twitter account. Probably only temporarily, but we’ll see. This is just a quick note to let everyone know that I’m not leaving Twitter because of anything specific. It’s not that you said or did anything wrong. I just find that it’s becoming an energy sink for me. I’m putting Twitter away as a measure to control my focus, and to better control what…
In early September 2014, we published Functional Programming in Scala . It is now available from all major booksellers, and from the publisher at manning.com/bjarnason . It’s available as a beautiful paper book, on Kindle and other e-book readers, and as a PDF file. I just want to share my personal story of how this book came to exist. A much shorter version of this story became the preface…
Today I want to talk about relationships between monoids. These can be useful to think about when we’re developing libraries involving monoids, and we want to express some algebraic laws among them. We can then check these with automated tests, or indeed prove them with algebraic reasoning. This post kind of fell together when writing notes on chapter 10, “Monoids”, of Functional…
Last week I gave a talk on Purely Functional I/O at Scala.io in Paris. The slides for the talk are available here. In it I presented a data type for IO that is supposedly a “free monad”. But the monad I presented is not exactly the same as scalaz.Free and some people have been asking me why there is a difference and what that difference means. IO as an application of Free The Free…
In a series of old posts, I once talked about the link between lists and monoids , as well as monoids and monads . Now I want to talk a little bit more about monoids and monads from the perspective of free structures . List is a free monoid . That is, for any given type A , List[A] is a monoid, with list concatenation as the operation and the empty list as the identity element. 1 2 3 4 def…
I gave a talk on “Machines” and stream processing in Haskell and Scala , to the Brisbane Functional Programming Group at Microsoft HQ in December 2012. A lot of people have asked me for the slides, so here they are: Machines.pdf The preëmptive answer to the usual follow-up question is that the talk was not recorded.
A lot of discussion about “purity” goes on without participants necessarily having a clear idea of what it means exactly. Such discussion is generally unhelpful and distracting. What purity is The typical definition of purity (and the one we use in our book ) goes something like this: An expression e is referentially transparent if for all programs p , every occurrence of e in p can be…
I have not seriously updated the old Apocalisp blog for quite some time. Mostly this is due to the fact that I have been spending all of my creative time outside of work on writing a book . It’s also partly that putting a post up on WordPress is a chore. It’s like building a ship in a bottle. So I have decided to make posting really easy for myself by hosting the blog on GitHub . I am…
Scalaz 5.0 adds an implementation of a concept called Iteratee. This is a highly flexible programming technique for writing enumeration-based input processors that can be freely composed. A lot of people have asked me to write a tutorial on how this works, specifically on how it is implemented in Scalaz and how to be productive with it, so here we go. The implementation in Scalaz is based on an…
(updated for Java 8) One of the great features of modern programming languages is structural pattern matching on algebraic data types. Once you’ve used this feature, you don’t ever want to program without it. You will find this in languages like Haskell and Scala. In Scala, algebraic types are provided by case classes. For example: 1 2 3 4 sealed trait Tree case object Empty extends…