Re:Things I learnt about Julia during Advent of Code
by stellartux on Thursday January 13, 2022 @02:31PM
(#29925556)
There are few easier ways built in to do some of the file functions you've made, instead of `open(f -> read(f, String), "input")` you can use `readchomp("input")`. There's also `readlines` which is equivalent to `open(f -> read(f, String), "input") |> f -> split(f, "\n")`, and `eachline` which is like `readlines` but returns a generator instead of an array.
There are curried forms of the operators that might make it easier to write functional code, like instead of `x -> x == "0"` you can write `==("0")`. Another useful thing is you can broadcast pipes, so instead of `x |> xs -> f.(xs)` or `x |> Map(f)`, you can write `x .|> f`.
It's funny that the first section is about everything loading as a matrix and not knowing the easy way to load things as a vector, I had the opposite problem and every problem that has a matrix I've ended up making a giant vector then reparsing the first line to figure out what dimensions to reshape it to. I'll have to try `readdlm`, I'm currently working through the older AoC years in Julia.
Re:Things I learnt about Julia during Advent of Code
by kwertzzz on Friday January 14, 2022 @09:33AM
(#29934850)
Nice article!
One can you also use reduce(hcat,n) instead of hcat(n...).
The function reduce(hcat,n) should be faster as there is optimized implementation of it:
Re:Things I learnt about Julia during Advent of Code
by snicker7 on Saturday January 15, 2022 @02:01AM
(#29944320)
Julia matrices are laid out as column-major (like Fortan, unlike C). So, it is better to get into the habit of thinking of a matrix more as a vector of columns than a vector of rows.
Re:Things I learnt about Julia during Advent of Code
by dunefox on Saturday January 15, 2022 @05:35AM
(#29945355)
I also did AoC in Julia and it was pretty great. I did a few days in Kotlin as well and the value of good tooling is hard to overstate. This is one of my biggest problems with Julia, otherwise it's a very good language.
There are curried forms of the operators that might make it easier to write functional code, like instead of `x -> x == "0"` you can write `==("0")`. Another useful thing is you can broadcast pipes, so instead of `x |> xs -> f.(xs)` or `x |> Map(f)`, you can write `x .|> f`.
It's funny that the first section is about everything loading as a matrix and not knowing the easy way to load things as a vector, I had the opposite problem and every problem that has a matrix I've ended up making a giant vector then reparsing the first line to figure out what dimensions to reshape it to. I'll have to try `readdlm`, I'm currently working through the older AoC years in Julia.
https://github.com/JuliaLang/julia/blob/89a51fb98485219680c7...
This feature was discussed here: https://github.com/JuliaLang/julia/issues/21672