mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 11:52:19 +00:00
add hashUnordered that uses a walk method and hashes all elements
This commit is contained in:
parent
f398bb1eff
commit
7a8dee888d
1 changed files with 20 additions and 0 deletions
|
@ -81,3 +81,23 @@ hashStrBytes = \hasher, s ->
|
|||
hashList = \hasher, lst ->
|
||||
List.walk lst hasher \accumHasher, elem ->
|
||||
hash accumHasher elem
|
||||
|
||||
hashUnordered = \hasher, container, walk ->
|
||||
walk
|
||||
container
|
||||
0
|
||||
(\accum, elem ->
|
||||
x =
|
||||
hasher
|
||||
|> new
|
||||
|> hash elem
|
||||
|> complete
|
||||
nextAccum = Num.addWrap accum x
|
||||
|
||||
if nextAccum < accum then
|
||||
# we dont want to lose a bit of entropy on overflow, so add it back in.
|
||||
Num.addWrap nextAccum 1
|
||||
else
|
||||
nextAccum
|
||||
)
|
||||
|> \accum -> addU64 hasher accum
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue