Showing posts with label monads. Show all posts
Showing posts with label monads. Show all posts

2013-08-25

Monads in Scala Part Three: Lisst[A]

We continue our Monads in Scala series by reviewing and translating the third page of the chapter on monads from the Haskell wikibook. This page takes quite an interesting view on lists, as it does not treat lists as things that you cons onto or even look things up in. It manages to treat lists nearly entirely as monadic structures. It even manages to come up with a couple of examples of using lists that only use the basic monadic operations. To view lists as things that we cons onto, we have to wait for a later page on additive monads.

2013-06-26

Scala Days Recap and Video

Recap

I really had a blast at Scala Days this year. I met so many wonderful people, saw many wonderful talks, and had a ton of fun. I met so many great people, I can't possibly mention them all. My best memory is drinking a glass of wine at a bar with all the Scala Days attendees, when from out of the blue come Matthias Nehlsen to greet me. We have known each other on Google+ for some time, but had never met before in person. It felt like one of those Dora episodes where they all jump into the Fairy Book and go to Fairy Land.

I also got to speak to Dick Wall a bit, which was sort of a Wayne's World moment for me ("We're not worthy!"), as I used to be an avid Java Posse listener before I started writing mostly Scala. Great podcast that really helped me keep in touch with what was going on in the Java community. For Scala, I listen to the Scalawags, and I eagerly look forward to the resumption of the Scala Types podcast.

I really loved Rod Johnson's keynote. Scala poetry is a beautiful idea. I'll try to write some one day. The only point in his keynote that I question is that startups are not going to be using Scala. I don't object out of reason, but from the observation that there are many startups in the Cambridge/Boston area that are taking up Scala. There were too many great talks to start telling stories, but I won't forget asking a stupid question at Heather Miller's Pickles and Spores talk for a while. There are still many talks that I need to catch up with online. In many slots it was hard to pick between sessions!

2013-03-26

Monads in Scala Part Two: More Maybes

In the first installment of this series, we translated the examples from the first page of the chapter on monads in the Haskell wikibook from Haskell to Scala. In this blog post, we will continue on with the second page. This page continues with more Maybe examples, which will help us to generalize our monadic laws test for Maybes a bit. We'll also learn about kleisli composition, which composes functions suitable for passing to flatMap.

All the source code presented here is available here on GitHub. I'm compiling against Scala 2.10.1. Some of this code will not compile under Scala 2.9.x.

2013-02-24

Monads in Scala Part One: Maybe[Person]

Intro

I've split the introduction to this post into two parts. The first part is long and sentimental, describing my personal journey with monads up to now. The second part is short and to the point. Feel free to skip over the next section if you're not interested in the sappy stuff - you won't be missing anything important.

Sappy Intro

I've been programming Scala for a little over two years now, and I love it. I was an old-time Java hack, firmly embedded in the OO programming paradigm. Scala to me was largely like a souped up Java, with all the clunky, broken things about Java fixed (aside from type erasure). I had taken courses Scheme and the lambda calculus in college, and I thought I had a pretty good understanding of functional programming. However, while programming Scala, and getting exposure to the language, libraries, and community, I began to discover that there is a lot more to functional programming than I originally thought. By far the most prominent example of this was the the importance of the concept of the monad in the Scala community.

I didn't know what a monad was, and over the past couple of years, I've made various efforts to try to understand it. For the most part, the videos I watched, and the articles I read, left me with one of two impressions: Either it didn't make any sense, or I just couldn't see what the big deal was. As I continued to explore, I began to notice that most of the explanatory examples are in Haskell, and not understanding the Haskell language or syntax was a serious impediment for me. At that point, I set out specifically searching for monad examples in Scala, and found this excellent series of blog posts by James Iry: Monads are Elephants (see also parts two, three, and four). Finally, monads were beginning to make sense! But after reading these articles, I realized I was going to have to implement some monads myself to really grok them. I also knew that I would have to follow by example to do this. Which meant that I really needed to bite the bullet and look at some monad examples in Haskell.

No Nonsense Intro

I found an excellent exposition on monads in this Haskell Wikibook, and set out to translate the examples into Scala. In this blog post, we will present a translation of the Maybe/Person example presented in the first page the chapter on monads. In future blog posts, I hope we can do the same for the rest of the pages of this chapter.