4 ms·
I'm only skimming this post and I'm not familiar with Julia so maybe I'm missing it, but does it have a way to get an item AND its index? There's I think Enumra
by runevault 4y ago
I'm only skimming this post and I'm not familiar with Julia so maybe I'm missing it, but does it have a way to get an item AND its index? There's I think Enumrable? in Rust where it gives you a tuple with both the item and its index in cases where you need both.
- lmiq 4y agofor (i,val) in pairs(array)
- leephillips 4y agojulia> pairs("François") |> collect 8-element Vector{Pair{Int64, Char}}: 1 => 'F' 2 => 'r' 3 => 'a' 4 => 'n' 5 => 'ç' 7 => 'o' 8 => 'i' 9 => 's' Notice the missing index 6, because ç takes two bytes. In contrast, enumerate() gets you the iteration number: julia> enumerate("François") |> collect 8-element Vector{Tuple{Int64, Char}}: (1, 'F') (2, 'r') (3, 'a') (4, 'n') (5, 'ç') (6, 'o') (7, 'i') (8, 's') This can trip you up.
- runevault 4y agoRust has the same problem with dealing with strings where if you don't realize how you are supposed to handle it with unicode you'll get burned when you don't correctly access code points. Edit: Also thank you for the answer. I have been curious about Julia even though I'm not a data science/ML type, but never find time. I do like to keep an eye on it though.
- leephillips 4y agoI haven’t used Rust, but Julia keeps you from getting burned too badly by giving you an informative error message if you try to index "inside" a character: julia> "François"[6] ERROR: StringIndexError: invalid index [6], valid nearby indices [5]=>'ç' [7]=>'o' I’m not a data science type either. I came to Julia through physics and general computing. It’s the best language for science I’ve ever encountered.