517 HIKES and counting

Top Stories

Lava Kafle


Scala vs. F#: Comparing Functional Programming Features — Developer.com

Immutability Fosters Concurrent Programming

Sharing Is Cheap

If you’re certain that an object will never change, then sharing this object becomes a simple matter of providing a reference to it. In Java doing so often requires a lot of defensive copying. Along this vein, because we can freely share references for immutable objects, we can likewise intern them for free.

Flattening the Levels of Indirection

There’s a marked difference between a mutable object and a mutable reference. The default in Java is that there are mutable references that contain mutable data. But in Clojure there are only mutable references. This may seem like a minor detail, but it certainly works to reduce unnecessary complexities. Why else is there a proliferation of final in next-generation Java programming styles?

Equality Has Meaning

Equality in the presence of mutability has no meaning. Equality in the face of mutability and concurrency is utter lunacy. That is, if any two objects resolve as being equal now, then there’s no guarantee that they will a moment from now. And if two objects aren’t equal forever, then they’re technically never equal (For more information about these statements, see Henry Baker’s essay “Equal Rights for Functional Objects or, The More Things Change, The More They’re the Same.”). Providing immutable objects once again assigns meaning to equality in that if two objects are equal now, then they will always be so.

In a concurrency-oriented programming language like Clojure, the primary benefit of immutability is that the core types can be shared freely among separate threads without fear. In the next section we’ll discuss this particular benefit in more detail.

F# vs. Scala: Lazy Evaluation

F# supports lazy evaluation, but for performance reasons it is not enabled by default. Instead, F# supports so-called eager evaluation: functions can be marked for lazy evaluation by explicitly labeling them with the keyword lazy and running the program with the Lazy.force option specified.

let lazyMultiply = lazy ( let multiply = 4 * 4 )

Like F#, Scala is not lazy by default, but unlike F#, values — not functions –have to be marked with the keyword lazy and therefore evaluated as call-by-need.

def lazyMultiply(x: => y:) = { lazy val y = x * x }

F# vs. Scala: Lambda Expressions and Currying

via Scala vs. F#: Comparing Functional Programming Features — Developer.com.