Rúnar Bjarnason's Blog

April 2, 2016

A Comonad of Graph Decompositions

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 familiarity with comonads.

Inductive Graphs

At work, we develop and use a Scala library called Quiver for working with...

1 like ·   •  0 comments  •  flag
Share on Twitter
Published on April 02, 2016 10:02

October 12, 2015

Freedom and Forgetfulness

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 category of monoids Mon with monoids as objects and monoid homomorphisms as arrows between them. Then there is a functor f...

 •  0 comments  •  flag
Share on Twitter
Published on October 12, 2015 20:13

October 4, 2015

Scala Comonad Tutorial, Part 2

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” or nonempty in some sense. That is, given a value of type W[A] for some comonad W, we must be able to get a value of...

 •  0 comments  •  flag
Share on Twitter
Published on October 04, 2015 14:36

September 30, 2015

An Adjunction That Induces the Reader Monad

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?

Following leads from Shachaf Ben-Kiki on IRC, I thought about whether there was a free-forgetful adjunction given by an isomorphism between Kleisli arrows in the Reader monad on...

 •  0 comments  •  flag
Share on Twitter
Published on September 30, 2015 04:48

June 24, 2015

A Scala Comonad Tutorial, Part 1

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 of monads. The utility of comonads in everyday life is not quite as immediately obvious as that of...

 •  0 comments  •  flag
Share on Twitter
Published on June 24, 2015 08:35

June 20, 2015

Pulling Out of Functional Programming in Java

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 writing another book makes me anxious. I know from experience that making a book (at least a good one)...

 •  0 comments  •  flag
Share on Twitter
Published on June 20, 2015 10:26

Easy Performance Wins With Scalaz

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 futureA.flatMap(a = futureB), both Future[A] and Future[B] need to be submitted to the thread pool, even though they are not...

 •  0 comments  •  flag
Share on Twitter
Published on June 20, 2015 10:26

March 7, 2015

A Companion Booklet to FPiS

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 downloading the source code from GitHub and doing the exercises as they read. But a number of readers have...

 •  0 comments  •  flag
Share on Twitter
Published on March 07, 2015 09:26

February 15, 2015

What Purity Is and Isn't

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 replaced with the result of evaluating e without changing the result of evaluating p.



A function f is pure if the...

 •  0 comments  •  flag
Share on Twitter
Published on February 15, 2015 01:58

Machines and Stream Processing

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.

 •  0 comments  •  flag
Share on Twitter
Published on February 15, 2015 01:58