Set has not a functor

November 29, 2015

Mapping and binding sets can have unexpected results.

def f(x: Int) = Set(x+1,x+2)
def g(x: Int) = Set(1)

println(Set(1,2,3).flatMap(f).flatMap(g))
println(Set(1,2,3).flatMap({ x => f(x).flatMap(g) }))

This article is literate Scala:

$ scodedown https://earldouglas.com/posts/set-no-functor.md