chrisvoncsefalvay / learn-julia-the-hard-way

Learn Julia the hard way!
https://app.gitbook.com/@chrisvoncsefalvay/s/learn-julia-the-hard-way/
Other
772 stars 153 forks source link

Chapter 4: Collections #20

Open sbromberger opened 9 years ago

sbromberger commented 9 years ago
Existence of a particular value

To find out whether an array has a particular value among its elements, you can use in():

    julia> in(2, primes)
    true
Somewhat strangely, in the in() syntax, the needle comes before the haystack, i.e. in(value, array), where value denotes the value you are looking for.

Why not talk about contains() as well, where the haystack comes first?

sbromberger commented 9 years ago

Also,

Change values

To change a value, access it via the bracket syntax, then assign it the new value:

    julia> statisticians["Kendall"] = "1907-1983"
    "1907-1983"

    julia> statisticians
    Dict{ASCIIString,ASCIIString} with 4 entries:
      "Galton"  => "1822-1911"
      "Pearson" => "1857-1936"
      "Kendall" => "1907-1983"
      "Gosset"  => "1876-1937"

The example doesn't fit the previous contents of the Dict: you should be assigning "Gosset".

sbromberger commented 9 years ago

Also,

This is because dicts are not indexable, therefore there is no ordering that would make inherent sense. However, sometimes, we like dictionaries sorted. Disappointingly, sorting dicts is not as easy as sorting arrays:

Perhaps a mention of DataStructures.jl?