Does Julia need something like Python's frozenset?

363 views
Skip to first unread message

Sal Mangano

unread,
Sep 20, 2014, 8:23:31 PM9/20/14
to juli...@googlegroups.com
In python sets do not implement hash so you can't create a set of sets or a dict with a set as the key. This is because you can corrupt the data structure. However, you can use frozenset instead. 

In Julia there is no such concept yet so you can do the following, which is a bit disturbing.

s1 = Set(1,2,3)
Set{Int64}({2,3,1})

s2 = Set(1,2,3,4)
Set{Int64}({4,2,3,1})

s3 = Set({s1,s2})
Set{Any}({Set{Int64}({2,3,1}),Set{Int64}({4,2,3,1})})

union!(s1,4)
Set{Int64}({4,2,3,1})

s3 

Set{Any}({Set{Int64}({4,2,3,1}),Set{Int64}({4,2,3,1})})

Not sure what s3 is but it is no longer a set! 

I've implemented a FrozenSet in Julia for my own use but was wondering if anyone had thoughts on this.

Jacob Quinn

unread,
Sep 22, 2014, 9:59:02 AM9/22/14
to juli...@googlegroups.com
It'd certainly be great to throw together a package that others could test out. http://docs.julialang.org/en/latest/manual/packages/#package-development

-Jacob

John Myles White

unread,
Sep 22, 2014, 10:14:47 AM9/22/14
to juli...@googlegroups.com
This seems like an endemic problem for all mutable types:

julia> a1 = [1, 2, 3]
3-element Array{Int64,1}:
1
2
3

julia> a2 = [1, 2, 3, 4]
4-element Array{Int64,1}:
1
2
3
4

julia> s =Set({a1, a2})
Set{Any}({[1,2,3,4],[1,2,3]})

julia> push!(a1, 4)
4-element Array{Int64,1}:
1
2
3
4

julia> s
Set{Any}({[1,2,3,4],[1,2,3,4]})

Does Python prevent this sort of issue for all mutable data structures? If so, how?

— John

Stefan Karpinski

unread,
Sep 22, 2014, 10:33:58 AM9/22/14
to juli...@googlegroups.com
Python just doesn't let you hash mutable things. Ruby and Perl let you do this and you just have to deal with the fact that if you hash something and mutate it things get a little strange. Basically, you end up with a value in a hash slot that it no longer maps to – and in particular, it is no longer isequal to anything that maps into that slot. I agree that it's not a particularly good thing, but it comes up shockingly rarely in practice in the languages that allow it. The solution seems to be "don't mutate things you use as hash keys". Python's solution, on the other hand, is to disallow hashing anything that's mutable.

Steven Sagaert

unread,
Sep 22, 2014, 11:41:58 AM9/22/14
to juli...@googlegroups.com
Java (& probable all derived languages like Groovy,Scala,C#,..) has this problem too. That's why it's considered good practice to just use Strings as keys since these are immutable in Java.

Kevin Squire

unread,
Sep 22, 2014, 1:02:34 PM9/22/14
to juli...@googlegroups.com
This is the scone time recently I'm inclined to bring up FunctionalCollections.jl, which would fit this roll, but which I should probably check the status of. Had anyone used these recently?  

Cheers, Kevib 

Stefan Karpinski

unread,
Sep 22, 2014, 1:03:58 PM9/22/14
to Julia Dev
Smartphone typing strikes back.

Kevin Squire

unread,
Sep 22, 2014, 1:15:24 PM9/22/14
to juli...@googlegroups.com
LoL!  Noticed that just as I hit send... 
Reply all
Reply to author
Forward
0 new messages