Skip to main content

Posts

Showing posts with the label Scala

Kjært barn har mange navn - or, the story of how I discovered Ruby's each_cons

Reading the Pragmatic Programmer's PragPub from March (I must admit I don't have/take the time to read them regularly enough) I stumbled upon Scala's sliding function on Arrays. This creates a sliding window of consecutive sub arrays of an array. So an  Array(1,2,3,4).sliding(2) would produce an iterator of arrays (1,2), (2,3), and (3,4). Useful! While googling around to see if Ruby has something similar I stumbled across the windowed function in F# that pretty much works the same way, before finding Ruby's each_cons . The first thing I did to test this was to revisit an old (fairly obvious, brute force) seven line Ruby solution (from back in the day when I had just discovered the language) to an Euler problem , and replaced it with the following (admittedly still brute force) one liner (where 'number' is a 1000 character long string containing only numbers): Unlike Scala where you would have the function sliding on string objects, we need to call chars ...

Parenthesis (and dots) (in Scala (as compared to Ruby))

I must say that I quite like what I encounter of the Scala language in Martin Odersky's online " Functional Programming in Scala " course at EPFL (as well as the course itself!). It is almost as expressive as my favourite language these days, Ruby . (Of course it will never really be able to match it on expressiveness, I'd say (the latter being duck typed, and the former being strictly typed (even with a most excellent type inference)).) One detail that I like about both languages is that parenthesis are (most of the times) optional. (By "(most of the time)" I mean that you (obviously) can't really skip them if it leads to problems with operator precedence.) This (can) really lead(s) to more readable code. Scala, however, (seemingly) has some inconsistencies when it comes to when  they are needed. Most notably the println  (and similar?) function(s). ((Quite possibly inspired by scripting languages) you have a quick way to display text at the user w...

Progfun, Eclipse, JDKs, and Platform Agnosticism

Fattigfolket I'll be following the   Functional Programming Principles in Scala  course offered by Coursera this Autumn.   To get you started the good folks behind the course have provided lots of info on how to install necessary software etc. However, I ran into a problem, and it's subsequent solution, that I would like to document here. I'll be following the course a bit from here and a bit from there. Sometimes on my mac, sometimes on a linux laptop, sometimes on a windows machine. Anyway, I have my course files in my Dropbox and pick them up from there as needed on the various OSes. Problem was when opening the my scala projects in eclipse in OSX after compiling them under Windows, I got a cryptic " error in scala compiler null " and was unable to clean and (re)compile my scala programs. I realised that I was running JDK 6 on my mac and JDK 7 on anything else, so I figured that might have something to do with it. After following Mary Hamlin 's ans...