learning Scalaz, an html5 book

in

Scalaz 7.0 is out. For easier reading and toc, I converted learning Scalaz series into an html5 book using @n8han's Pamflet:

translating a conference

It's been a month now, but on March 1, 2013 I flew to Japan to attend "Scala Conference in Japan 2013." That's the name of the conference.

from a podcast

One day (June 2, 2012), I was listening to Scala Types recorded at Scala Days 2012, and someone (turns out it's @timperrett) said "I would love to see Scala Days in Asia. We had two in Europe now. It would be wicked to have it in China or Japan, or somewhere like that." I relayed this info on twitter saying there's an opportunity for the Scala community in Japan to invite the conference.

sudoku using Func

in

This is the 5th entry of Scalaz Advent Calendar 2012.

During the months of December, tech-savvy geeks in Japan take turns to post themed blog articles, known as the "Advent Calendar". For last year's Scala Advent Calendar 2011 I translated Eric Torreborre's post covering The Essence of Iterator Pattern. It was somewhat of a calculated move, knowing Japanese fondness for functional programming articles. Another selfish motive was that some of the concept would seep in to my thickness as I was translating the post word by word. In hindsight, both goals were achieved handsomely thanks to the quality of both Jeremy Gibbons, Bruno Oliveira and Eric's work. This seeped in knowledge was probably the secret sauce behind the learning Scalaz series that I worked on this year.

As covered in learning Scalaz day 12 Scalaz 7 already included product and compose methods in typeclass instances as well as Traverse. It even has the word count example from the paper. What I realized missing was the value-level composition. One of the interesting points from the paper was "composition of applicative functors," which enables a kind of modular programming.

By "applicative functors" Gibbons and Oliveira actually mean composition of applicative functions, not just the typeclass instances. This is evident in the following snippet from the paper:

data (m ⊠ n) a = Prod { pfst :: m a, psnd :: n a }
() :: (Functor m, Functor n)(a → m b)(a → n b)(a → (m ⊠ n) b)
(f ⊗ g) x = Prod(f x)(gx)

The algebraic data type is the type-level product, while the infix function is the value-level product of two applicative functions, which returns applicative function of type a → (m ⊠ n). In other words, the programmer would construct functions that return an applicative functor, and the type-level compositions are done automatically.

learning Scalaz: day 18

in

Hey there. There's an updated html5 book version, if you want.

On day 17 we looked at IO monad as a way of abstracting side effects, and Iteratees as a way of handling streams. And the series ended.

Func

I wanted to continue exploring a better way to compose applicative functions, and came up with a wrapper called AppFunc:

val f = AppFuncU { (x: Int) => x + 1 }
val g = AppFuncU { (x: Int) => List(x, 5) }
(f @&&& g) traverse List(1, 2, 3)

learning Scalaz: day 17

in

Hey there. There's an updated html5 book version, if you want.

Daniel Steger for openphoto.net

Yesterday we looked at Memo for caching computation results, and ST as a way of encapsulating mutation. Today we'll continue into IO.

IO Monad

Instead of reading the second half of the paper, we can get the gist by reading Towards an Effect System in Scala, Part 2: IO Monad by Rúnar (@runarorama):

While ST gives us guarantees that mutable memory is never shared, it says nothing about reading/writing files, throwing exceptions, opening network sockets, database connections, etc.

learning Scalaz: day 16

in

Hey there. There's an updated html5 book version, if you want.

Yesterday we looked at Arrow as a way of abstracting function-like things and Unapply as a way of providing typeclass meta-instances. We also continued on with the applicative experiment by implementing XProduct that supports parallel compositions.

Memo

Pure functions don't imply they are computationally cheap. For example, calcuate a list of SHA-1 hash for all permutations of ASCII character string up to 8 characters length. If we don't count the tab character there are 95 printable characters in ASCII, so let's round that up to 100. 100 ^ 8 is 10 ^ 16. Even if we could handle 1000 hashing per second, it takes 10 ^ 13 secs, or 316888 years.

learning Scalaz: day 15

in

Hey there. There's an updated html5 book version, if you want.

Rodolfo Cartas for openphoto.net

On day 14 we started hacking on Scalaz. First, typeclass instances for Vector was added to import Scalaz._. Next, we rolled back <*> to be infix ap. Finally, we added an implicit converter to unpack A as [α]A, which helps compiler find Applicative[({type λ[α]=Int})#λ].

All three of the pull requests were accepted by the upstream! Here's how to sync up:

$ git co scalaz-seven
$ git pull --rebase

learning Scalaz: day 14

in

Hey there. There's an updated html5 book version, if you want.

bman ojel for openphoto.net

Yesterday we looked at what import scalaz._ and Scalaz._ bring into the scope, and also talked about a la carte style import. Knowing how instances and syntax are organized prepares us for the next step, which is to hack on Scalaz.

mailing list

Before we start hacking on a project, it's probably good idea to join its Google Group.

git clone

$ git clone -b scalaz-seven git://github.com/scalaz/scalaz.git scalaz-seven

learning Scalaz: day 13

in

Hey there. There's an updated html5 book version, if you want.

e.e d3si9n

Yesterday we skimmed two papers by Jeremy Gibbons and quickly looked at origami programming and applicative traversal. Instead of reading something, why don't we focus on using Scalaz today.

implicits review

Scalaz makes heavy use of implicits. Both as a user and an extender of the library, it's important to have general idea on where things are coming from. Let's quickly review Scala's imports and implicits!

In Scala, imports are used for two purposes:
1. To include names of values and types into the scope.
2. To include implicits into the scope.

learning Scalaz: day 12

in

Hey there. There's an updated html5 book version, if you want.

reynaldo f. tamayo for openphoto.net

On day 11 we looked at Lens as a way of abstracting access to nested immutable data structure.

Today, let's skim some papers. First is Origami programming by Jeremy Gibbons.

Origami programming

Gibbons says:

In this chapter we will look at folds and unfolds as abstractions. In a precise technical sense, folds and unfolds are the natural patterns of computation over recursive datatypes; unfolds generate data structures and folds consume them.

Syndicate content