background preloader

Scala

Facebook Twitter

Haskell

Actors. Lift. Linq. Practice. Intellij scala. Monads in Scala. Here's the beginning of a little tutorial on Monads in Scala. Michel Schinz provided encouragement and large parts of code, especially the trick to use bind and unit In Informatics, there are two (related) meanings of the word "monad": A triple (T,eta,mu) following some laws in category theory A way of structuring functional programs The first meaning can probably not be described easily in natural language. Michael Arbib and Ernest Manes' "Arrows, Structure, Functors - The Categorical Imperative". describe them as (generalized monoids) in Section 10.2 and through adjointness to the forgetful functor from algebras to sets. In order to make sense of the "triple" jargon above, say T is a polymorphic type List (including the higher-order function map:(A->B)->(List[A]->List[B])).

The second view is described in a dozen of Phil Wadler's papers and shall be considered from the Scala programmer's point of view. Computation def unit: a -> M[b] def bind: M[a] -> (a->M[b]) -> M[b] Examples. Subsuming the template method pattern. It all started with this Kevin Rutherford post, where he talks about his unnatural urge to use the Template Method pattern. He mentions that whenever he sees a duplicate algorithm, he has a natural tendency is to push up the skeleton into a superclass. This way he creates too many inheritance hierarchies, which prove to be quite brittle in the face of changing algorithms. Template method pattern mandates that the *template method* must model the invariant part of the algorithm, while the concrete classes will implement the variabilities that will be called back into the template method. While the pattern encourages implementation inheritance, which may lead to unnecessary coupling and brittle hierarchies, the pattern has been used quite effectively in various frameworks developed over times.

Outside the OO world, template method pattern has been put to great use by many programming languages and frameworks. Consider this code snippet in Scala .. List(1, 2, 3, 4).map {x => x * 2} Scala Explained. Growing a language. Today I needed to do some load testing on a web site. All I wanted to do was hammer it with requests and observe how well it held up. I wasn't familiar with any tools that did this, and had no desire to learn them, so I sat down wrote my own mini-framework in less than a hundred lines of Scala. It's nothing special, but I want to share bits of it to show how you can grow Scala to fit your needs. At the heart of any web testing is HTTP. Scala runs on the JVM, so I browsed through some Java libraries for fetching HTTP content, but the existing Java approaches were too verbose for my taste.

Java is a systems language and it shows, especially in the library design decisions. Thanks Ruby, that's much cleaner. Timing is Everything When load testing, it's nice to know how long things take. A call to Time(...) returns a pair with the result of the action and the time it took to complete that action. How does it work? Getting Repetitive The repeat method takes a number n and an action.

The Lesson? Meet the scala.xml book if you haven't yet. Wallify, a more functional-ish version - Bay Area Scala Enthusia. Common Lisp the Language, Second Edition. One-Line JavaScript Memoization | Oliver Steele. Computing the length of a Bezier curve is expensive, but the length of a given Bezier doesn’t change over time. In my JavaScript Bezier implementation, I wanted to compute the length only the first time it’s need, and save this result in order to return instantly thereafter. This is a special case of memoization.

There are well-known strategies for implementing memoization. But getLength is a nullary function, and there’s a trick for implementing memoization of nullary methods in a dynamic language such as JavaScript (or Python or Ruby). Memoizing by Hand The naive way to implement memoization is to store the value in a property the first time it’s computed, and test for the presence of the property thereafter: Bezier.prototype.getLength = function() { var length = this. or: Bezier.prototype.getLength = function() { if ('_length' in this) return this. There are some problems with these solutions. These implementations also require a private property for each memoized method. The Big Gun. BASE Meeting #25, Monday 10th May, 7pm, LinkedIn in Mountain Vie.