Read entire file in Scala?

What’s a simple and canonical way to read an entire file into memory in Scala? (Ideally, with control over character encoding.) The best I can come up with is: scala.io.Source.fromPath(“file.txt”).getLines.reduceLeft(_+_) or am I supposed to use one of Java’s god-awful idioms, the best of which (without using an external library) seems to be: import java.util.Scanner … Read more

What is the apply function in Scala?

I never understood it from the contrived unmarshalling and verbing nouns ( an AddTwo class has an apply that adds two!) examples. I understand that it’s syntactic sugar, so (I deduced from context) it must have been designed to make some code more intuitive. What meaning does a class with an apply function give? What … Read more

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

I have heard the term “coalgebras” several times in functional programming and PLT circles, especially when the discussion is about objects, comonads, lenses, and such. Googling this term gives pages that give mathematical description of these structures which is pretty much incomprehensible to me. Can anyone please explain what coalgebras mean in the context of … Read more

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

What is the formal difference between passing arguments to functions in parentheses () and in braces {}? The feeling I got from the Programming in Scala book is that Scala’s pretty flexible and I should use the one I like best, but I find that some cases compile while others don’t. For instance (just meant … Read more

What is a TypeTag and how do I use it?

All I know about TypeTags is that they somehow replaced Manifests. Information on the Internet is scarce and doesn’t provide me with a good sense of the subject. So I’d be happy if someone shared a link to some useful materials on TypeTags including examples and popular use-cases. Detailed answers and explanations are also welcome. … Read more