I’ve just seen I can do:
Set(1, 2, 3).map(Set(1))
which yields the result:
Set(true, false)
But I thought that the map function can only take another function not a new Set. If anything I’d expect this to return a set of sets. What’s going on and what does the result mean?
A
Setis a function. It is a function from its elements to booleans: when you pass it an element, it tells you whether that element is part of theSet.Iterates over the
Set(1, 2, 3), passing each each element to theSet(1). I.e. it first asks “is1a member of set{1}“, which istrue, then it asks the same question for2and3, which isfalse.So, the result is
Set(true, false, false), which of course is justSet(true, false).Similarly, a sequence is a function from integers to elements, a map is a function from keys to values.