What is Scala’s yield?

I think the accepted answer is great, but it seems many people have failed to grasp some fundamental points. First, Scala’s for comprehensions are equivalent to Haskell’s do notation, and it is nothing more than a syntactic sugar for composition of multiple monadic operations. As this statement will most likely not help anyone who needs … Read more

What does “coalgebra” mean in the context of programming?

Algebras I think the place to start would be to understand the idea of an algebra. This is just a generalization of algebraic structures like groups, rings, monoids and so on. Most of the time, these things are introduced in terms of sets, but since we’re among friends, I’ll talk about Haskell types instead. (I … Read more

Read entire file in Scala?

val lines = scala.io.Source.fromFile(“file.txt”).mkString By the way, “scala.” isn’t really necessary, as it’s always in scope anyway, and you can, of course, import io’s contents, fully or partially, and avoid having to prepend “io.” too. The above leaves the file open, however. To avoid problems, you should close it like this: val source = scala.io.Source.fromFile(“file.txt”) … Read more

What is the apply function in Scala?

Mathematicians have their own little funny ways, so instead of saying “then we call function f passing it x as a parameter” as we programmers would say, they talk about “applying function f to its argument x“. In mathematics and computer science, Apply is a function that applies functions to arguments. Wikipedia apply serves the … Read more

What is the formal difference in Scala between braces and parentheses, and when should they be used?

I tried once to write about this, but I gave up in the end, as the rules are somewhat diffuse. Basically, you’ll have to get the hang of it. Perhaps it is best to concentrate on where curly braces and parenthesis can be used interchangeably: when passing parameters to method calls. You may replace parenthesis … Read more

What is a sealed trait?

A sealed trait can be extended only in the same file as its declaration. They are often used to provide an alternative to enums. Since they can be only extended in a single file, the compiler knows every possible subtypes and can reason about it. For instance with the declaration: sealed trait Answer case object … Read more

How do I get around type erasure on Scala? Or, why can’t I get the type parameter of my collections?

This answer uses the Manifest-API, which is deprecated as of Scala 2.10. Please see answers below for more current solutions. Scala was defined with Type Erasure because the Java Virtual Machine (JVM), unlike Java, did not get generics. This means that, at run time, only the class exists, not its type parameters. In the example, … Read more

What is the advantage of using abstract classes instead of traits?

I can think of two differences Abstract classes can have constructor parameters as well as type parameters. Traits can have only type parameters. There was some discussion that in future even traits can have constructor parameters Abstract classes are fully interoperable with Java. You can call them from Java code without any wrappers. Traits are … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)