Towards a useful F#-style |> operator in Scala

Recently I’ve read a story about emulating F#’s very awesome |> operator in Scala. Obviously, Scala ain’t F# in more ways than just this, however, this got me thinking about the differences, and caused me to fall down a long and winding code hole that I’m just now climbing out of.

The way F# structures collection operations (map, filter, …) is quite useful: these are defined as module-level functions on modules named after collections (List, Seq, Map, …). Because F# functions are inherently curried, collection operations are typed as (I’m using the equivalent Scala syntax here):

Conversely, in Scala we’re forced to wait until a collection instance is available, and only then are we allowed to call various operations on it.

I thought this was pretty uncool, so I embarked upon a quest to find some way to write F# in Scala. My initial assumption was that this would necessarily involve macros, however, these were not the quasiquotes I was looking for.

The result is this:

A full report of my adventure is linked above; suffice it to say that I’m now able to write code like this:

This is almost entirely useless (like, why would anyone want to do this ever?), but definitely a loud and obnoxious testament to the expressiveness of Scala and flexibility of its syntax in the wrong hands.