Duck typing is like the force

Scala’s type system is impressive, and yesterday one feature came in handy: converting certain classes into a common type with useful methods on it. Say several types have a core set of characteristics, and we want to define methods based on those without modifying the original classes. In this example, various classes represent regions of …

Read moreDuck typing is like the force

What is flatMap?

flatMap is a method on collections that, at a surface level, transforms each element into another collection of any type, then takes all the elements out of those collections and puts them into one. It is like a map followed by a flatten. Why do two things in one step? Conceptually, List.map is always 1:1 …

Read moreWhat is flatMap?

Consistency in Scala

One of the goals of Scala is increased consistency compared to Java.Here are three high-level consistencies in Scala: Every value is an object. There are no primitives in Scala. Even its version of void (Unit) is an object. Nothing is an object. Nil is an object. Arrays are objects. Everything is an object. Every statement …

Read moreConsistency in Scala

For-confusion in Scala

In Scala, the “for” structure can throw off the Java programmer. For one, it looks enough like a Java “for” loop for a Java programmer to make a guess on how it works — but that guess is likely to be wrong. For another, the for-loop is implemented in Scala as a special case of …

Read moreFor-confusion in Scala

Familiarity v readability

The topic of discussion at the office today (yes, it’s Sunday) is Languages, Verbosity, and Java by Dhanji R. Prasanna, which purports to extoll the clarity of Java compared to more expressive languages like Ruby, Python, and Scala. What it really says is: “Languages that are familiar are more readable.” Of course languages that are …

Read moreFamiliarity v readability