Giter Club home page Giter Club logo

Comments (5)

giordano avatar giordano commented on June 2, 2024 1

I've added the hash method in #104. However, due to the fact isequal(x, y) must imply hash(x) == hash(y) we have this weird situation:

julia> x = 3 ± 0.1
3.0 ± 0.1

julia> a = 3 ± 0.1
3.0 ± 0.1

julia> x === a
false

julia> d = Dict(x => 42)
Dict{Measurement{Float64}, Int64} with 1 entry:
  3.0±0.1 => 42

julia> d[x]
42

julia> d[a]
42

julia> d[a] = 0
0

julia> d[a]
0

julia> d[x]
0

I don't think this can be avoided given the requirement on the relation between isequal and hash

from measurements.jl.

giordano avatar giordano commented on June 2, 2024

Frankly, I've never implemented hashing because

  1. I don't understand how it's supposed to work for non-standard numbers (and lack of documentation about this doesn't help much)
  2. so far I never had to implement them in practice.

I'm happy to do it if someone can help in particular with point 1, or if they open a pull request 🙂

from measurements.jl.

jwscook avatar jwscook commented on June 2, 2024

Taking inspiration from other packages:

from measurements.jl.

giordano avatar giordano commented on June 2, 2024

Out of curiosity, did you find a case in practice where the hash method was needed? I'm aware the method isn't implemented, but I couldn't find a real-world case in my applications where this would be needed.

from measurements.jl.

jwscook avatar jwscook commented on June 2, 2024

One instance it using a Measurement as a key in a Dict:

julia> using Measurements

julia> m = 1.0 ± 0.1
1.0 ± 0.1

julia> Dict(m=>"anything else here")
ERROR: MethodError: no method matching decompose(::Measurement{Float64})
Closest candidates are:
  decompose(::Integer) at float.jl:555
  decompose(::Float16) at float.jl:557
  decompose(::Float32) at float.jl:568
  ...
Stacktrace:
 [1] hash(x::Measurement{Float64}, h::UInt64)
   @ Base ./float.jl:491
 [2] hash(x::Measurement{Float64})
   @ Base ./hashing.jl:18
 [3] hashindex(key::Measurement{Float64}, sz::Int64)
   @ Base ./dict.jl:169
 [4] ht_keyindex2!(h::Dict{Measurement{Float64}, String}, key::Measurement{Float64})
   @ Base ./dict.jl:310
 [5] setindex!(h::Dict{Measurement{Float64}, String}, v0::String, key::Measurement{Float64})
   @ Base ./dict.jl:383
 [6] Dict{Measurement{Float64}, String}(kv::Tuple{Pair{Measurement{Float64}, String}})
   @ Base ./dict.jl:104
 [7] Dict(ps::Pair{Measurement{Float64}, String})
   @ Base ./dict.jl:124
 [8] top-level scope
   @ REPL[3]:1

julia> Base.hash(m::Measurement) = foldr(hash, (m.val, m.err, m.der, m.tag))

julia> Dict(m=>"anything else here")
Dict{Measurement{Float64}, String} with 1 entry:
  1.0±0.1 => "anything else here"

from measurements.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.