This Hashable package defines a class, Hashable, for types that can be converted to a hash value. This class exists for the benefit of hashing-based data structures. The package provides instances for basic types and a way to combine hash values.
Questions tagged [hashable]
161 questions
16
votes
3 answers
How does Dictionary use the Equatable protocol in Swift?
In order to solve this question, I have been playing around with a custom struct that implements the Hashable Protocol. I'm trying to see how many times the equivalency operator overload (==) gets called depending on if there is a hash collision or…

Suragch
- 484,302
- 314
- 1,365
- 1,393
13
votes
6 answers
Is it right to conform Hashable by only taking id into consideration?
A lot of online example I have came across, when they try to conform to Hashable, they only take id as consideration. For instance https://www.raywenderlich.com/8241072-ios-tutorial-collection-view-and-diffable-data-source ,…

Cheok Yan Cheng
- 47,586
- 132
- 466
- 875
13
votes
2 answers
Add a dictionary to a `set()` with union
I just ran across something interesting that I thought I'd ask about.
Adding a dictionary to a set, I had assumed that the dictionary would be added as a full dictionary, it isn't. Only the keys are added:
dicty = {"Key1": "Val1", "Key2":…

NotAnAmbiTurner
- 2,553
- 2
- 21
- 44
12
votes
2 answers
Creating a protocol that represents hashable objects that can be on or off
I'm trying to create a simple protocol that says whether or not an object is in an "on" state or an "off" state. The interpretation of what that is depends on the implementing object. For a UISwitch, it's whether the switch is on or off (duh). For a…

Tim Fuqua
- 1,635
- 1
- 16
- 25
10
votes
2 answers
Python: how to slice a dictionary based on the values of its keys?
Say I have a dictionary built like this:
d={0:1, 1:2, 2:3, 10:4, 11:5, 12:6, 100:7, 101:8, 102:9, 200:10, 201:11, 202:12}
and I want to create a subdictionary d1 by slicing d in such a way that d1 contains the following keys: 0, 1, 2, 100, 101, 102.…

FaCoffee
- 7,609
- 28
- 99
- 174
10
votes
1 answer
Can metatype (.Type) be used as Key in Dictionary?
I have something like this:
class Lumber { }
class Fruit { }
enum Size {
case small
case medium
case large
}
let lumberSize = [
Size.small: "2x4",
Size.medium: "4x6",
Size.large: "6x10"
]
let fruitSize = [
Size.small:…

Jeff
- 3,829
- 1
- 31
- 49
9
votes
4 answers
How to test for "immutability-at-any-depth" in Python?
I'm defining a Python object as being "immutable at any depth" iff
it is (nominally) immutable; and
if it is a "container" object, then it contains only objects that are "immutable at any depth";
For example ((1, 2), (3, 4)) is immutable at any…

kjo
- 33,683
- 52
- 148
- 265
8
votes
2 answers
How to make a tuple including a numpy array hashable?
One way to make a numpy array hashable is setting it to read-only. This has worked for me in the past. But when I use such a numpy array in a tuple, the whole tuple is no longer hashable, which I do not understand. Here is the sample code I put…

Demento
- 4,039
- 3
- 26
- 36
8
votes
2 answers
Why are mutable values allowed in Python Enums?
This is somewhat of a follow on to Why are mutable values in Python Enums the same object?.
If the values of an Enum are mutable (e.g. lists, etc.), those values can be changed at any time. I think this poses something of an issue if Enum members…

Billy
- 5,179
- 2
- 27
- 53
7
votes
2 answers
Unable to use a tuple as a dictionary key?
The code is a little complex, sorry. Please focus on the parallel_p function. Although sign is a tuple, Python complains:
if sign in hashtable and gives a TypeError. Why is sign a numpy.ndarray rather than a tuple? I created it as a tuple.
p_dist =…

Baskaya
- 7,651
- 6
- 29
- 27
7
votes
1 answer
Is this approach to dealing with hash collisions new/unique?
When dealing with hash maps, I have seen a few strategies to deal with hash collisions, but we have come up with something different.
I was wondering if this is something new or not.
This version of a hash map only works if the hash and the data…

Syd Kerckhove
- 783
- 5
- 19
7
votes
3 answers
swift 3.0 How can I access `AnyHashable` types in `Any` in Swift 3?
I'm using sqlite file to get the diaryEntriesTeacher from the authorId. it generates the following object of authorId when I print the variable authorId is nil
Code :-
func applySelectQuery() {
checkDataBaseFile()
objFMDB =…

Jayprakash Singh
- 1,343
- 3
- 15
- 28
7
votes
3 answers
NSObject is Hashable but a protocol that adopts NSObject is not?
Just a sanity check with the community before I file a radar:
In a .h Obj-C file:
@protocol myProto
@end
In a .swift file (that has access to the above protocol definition via bridging header):
class myClass {
// This line compiles…

Teo Sartori
- 1,082
- 14
- 24
6
votes
2 answers
How to implement Identifiable using two enum variables
Using Swift 5.3, how can I implement the Identifiable protocol on a struct by having its identity depend on the combination of two enum variables?
The code in question is simple,
struct Card: Identifiable {
let suit: Suit
let rank: Rank
…

Isaiah
- 1,852
- 4
- 23
- 48
6
votes
1 answer
map[gorm.DB]struct{}{} gives invalid map key type gorm.DB
I'd like to create a "set" of gorm types used in my application. So I'd like to define a map with my types gorm.DB as keys and empty structs{} as flags:
var (
autoMigrations map[gorm.DB]struct{}
)
But compiler doesn't allow me do this with…

Eugene Lisitsky
- 12,113
- 5
- 38
- 59