background preloader

Scala

Facebook Twitter

Scala - Underscore in Named Arguments. Alfredo Di Napoli - Computer Scientist, Scala and Haskell Programmer. Posted in: fp, scala, haskell. scalaz is an awesome library that extends the Scala Core, providing FP goodies that every Haskell programmer loves and needs. Yes, I’m obviously talking about Functors, Monads and other strange beasts from the Category Theory panorama. There is a lot of learning material for scalaz, but I’ve basically discovered that is not easy to organize your thoughts if you are an Haskell programmer, so I’m basically just writing this primarily as a mental note, but I hope it will be useful for others too.

The tutorial will be focused on scalaz 7, so be wary if you are not planning to switching any time soon. Before we start, I’ve found this little gem that compares, side by side, a lot of features from different programming languages: Setting up an sbt project If you are lazy and want to start right away, I suggest you use my giter8 template to have a plain project with scalaz and sbt-revolver. Monads and Control.Monad functions Unsurprisingly, it returns some(4). Monoid. #1 future is a monad - monadic web design. Scala - [scala] Structural types with generic type question. I'm curious as to what the holes would be if instead of reflection, structural types caused a synthetic class or trait to be created at compile time and every call to a structural typed method would cause the compiler to create a new anonymous adapter (or maybe even sometimes reuse an adapter class that was previously created).

Hmmm, actually I can see that identity is a potential problem...but still, here's the notion... I apologize for syntactic errors, I didn't run any of this through a compiler: type Duck = { def quack() } def makeDuckQuack(Duck duck) = duck.quack() def main(Array[String] args) = makeDuckQuack(new Mallard) Gets translated into something along the lines of class Duck = { abstract def quack() } def main(Array[String] args) = { $anonM = new Mallard $anonDuck = new Duck { def quack = $anonM.quack() } makeDuckQuack($anonDuck) } Hello list.

> I try to define structural types with abstract datatype in function > parameter. ... And now the answers. 1. 2. Sincerely, Gilles. Variance of type parameters in Scala. This is just a quick introduction to one of the features of Scala's generics. I realised earlier on IRC that they're probably quite unfamiliar looking to people new to the language, so thought I'd do a quick writeup. What does the following mean? Trait Function1[-T1, +R] It's saying that the trait Function1 is contravariant in the parameter T1 and covariant in the parameter R. Err. Lets try that again. A Function1[Any, T] is safe to use as a Function1[String, T]. So, Foo[+T] means that if S <: T then Foo[S] <: Foo[T]. Examples of this sort of behaviour abound. However, a mutable.List is *not* covariant in its type parameter.

So, we have three types of type parameter: Covariant, contravariant, invariant. But there are safe ways to treat mutable objects invariable. Well, this can indeed be done. Def doStuff[T <: Foo](arg : Array[T]) = stuff; So we introduce a type parameter for the array. This uses Scala's existential types to specify that there's an unknown type for which this holds true. What is Hindley-Milner? (and why is it cool?) 29 Dec 2008 Anyone who has taken even a cursory glance at the vast menagerie of programming languages should have at least heard the phrase “Hindley-Milner”.

F#, one of the most promising languages ever to emerge from the forbidding depths of Microsoft Research, makes use of this mysterious algorithm, as do Haskell, OCaml and ML before it. There is even some research being undertaken to find a way to apply the power of HM to optimize dynamic languages like Ruby, JavaScript and Clojure. However, despite widespread application of the idea, I have yet to see a decent layman’s-explanation for what the heck everyone is talking about.

How does the magic actually work? Ground Zero Functionally speaking, Hindley-Milner (or “Damas-Milner”) is an algorithm for inferring value types based on use. Just looking at the definition of bar, we can easily see that its type must be (String, Int)=>Int. Informal Algorithm The easiest way to see how this process works is to walk it through ourselves.

String Int. Is the Scala 2.8 collections library a case of "the longest suicide note in history" What is Hindley-Milner? (and why is it cool?) Scala - Remove entry from classpath after compile. 0.13.0 final.