Skip to content

Instantly share code, notes, and snippets.

@eonil
Last active September 16, 2019 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eonil/6aabb7a0975fef4b3eec85ecbcfdc2c3 to your computer and use it in GitHub Desktop.
Save eonil/6aabb7a0975fef4b3eec85ecbcfdc2c3 to your computer and use it in GitHub Desktop.
Swift slices computes hashes based on their contents.
do {
let a = "ABCABC"
let i = a.index(a.startIndex, offsetBy: 3)
let b = a[a.startIndex..<i]
let c = a[i...]
var s = Set<Substring>()
s.insert(b)
s.insert(c)
print(b.hashValue)
print(c.hashValue)
print(s)
// Prints same hash with `["ABC"]`.
// Hash is based on content and works well.
}
do {
let a = [111,222,333,111,222,333]
let i = a.index(a.startIndex, offsetBy: 3)
let b = a[a.startIndex..<i]
let c = a[i...]
var s = Set<ArraySlice<Int>>()
s.insert(b)
s.insert(c)
print(b.hashValue)
print(c.hashValue)
print(s)
// Prints same hash with `[ArraySlice([111, 222, 333])]`.
// Hash is based on content and works well.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment