Garth Gilmour
1 min readDec 16, 2021

--

Sure. One way of doing things per paradigm is fine. But not many ways within the same paradigm. Take the example of summing a list of numbers plus 100. Here's 12 subtly different versions of the same solution.

Scala gives you reduce, fold, foldLeft, foldRight etc... each of which can be invoked partially or fully and with the name of a function, an abbreviated lambda, a full lambda or a block. Thats too much variation for a first language.

val numbers = List(10, 20, 30, 40, 50)

val r1 = numbers.fold(100)(_ + _)

val r2 = numbers.fold(100)((a, b) => a + b)

val r3 = numbers.fold(100) { (a, b) => a + b }

val p1 = numbers.fold(100)

val r4 = p1(_ + _)

val r5 = p1((a, b) => a + b)

val r6 = p1 { (a, b) => a + b }

val r7 = numbers.foldLeft(100)(_ + _)

val r8 = numbers.foldLeft(100)((a, b) => a + b)

val r9 = numbers.foldLeft(100) { (a, b) => a + b }

val p2 = numbers.foldLeft(100)

val r10 = p2(_ + _)

val r11 = p2((a, b) => a + b)

val r12 = p2 { (a, b) => a + b }

--

--

Garth Gilmour
Garth Gilmour

Written by Garth Gilmour

Helping devs develop software. Coding for 30 years, teaching for 20. Technical Learning Consultant at Liberty Mutual. Also martial arts, politics & philosophy.

Responses (1)