scala - Are the head and tail of a Set guaranteed to be mutually exclusive? -


the documentation says set.head returns "first" item, , .tail returns "all first".* since set doesn't have "first" item, documentation warns without ordered type, might different result on different runs. guaranteed tail won't include head?

the reason i'm asking i'm wondering if it's ok recurse down set this:

def recurse(itemstoexamine: set[item], acc: result): result =   if (itemstoexamine.isempty) acc   else {     val item = itemstoexamine.head     recurse(       item.spawnmoreitems ++ itemstoexamine.tail,       acc.updatedfor(item))   } 

if that's legitimate, nicer converting set seq , in order separate head , tail on each recursion.


*actually, says "selects first item" , "selects first item". assume "selects" poor choice of word. if there's reason saying "selects" rather "returns", please let me know.

i'm not 100% sure this, because haven't looked @ implementation, hashset there's implicit ordering based on hashcode (of type int) of values in set.

that means set instance, calls head , tail respect ordering, won't same element. more, successive iteration through elements of given set instance should yield elements in same order, because set immutable.

the takeaway while ordering unknown, there 1 instance, may change add (mutably or immutably) new element set.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -