More #AdventOfCode in #JuliaLang. Updated my graphics for 2022 day 14 and learnt to make animated gifs direct from data. (Un)surprisingly easy 😸
As always, full code at https://github.com/screwdog/AoC, including hints for #JuliaBeginners for each day up to 14
#adventofcode #julialang #juliabeginners
Went back to my #AdventOfCode solution for day 12 and drew the results. Good opportunity to learn a little of #JuliaLang Images.jl and related packages.
Full solutions (including hints for #JuliaBeginners!) available at https://github.com/screwdog/AoC
#adventofcode #julialang #juliabeginners
Just finished #AdventOfCode 2015 in #JuliaLang, after finishing 2022 a week ago. Had started it to complement my 2022 attempt and happy to get both done.
All my solutions are available at https://github.com/screwdog/AoC if anyone is interested.
I'm slowly going through my 2022 solutions and adding hints and comments to help any #JuliaBeginners
#adventofcode #julialang #juliabeginners
#JuliaTipOfTheDay​ #JuliaBeginners
2022-12-01
To quickly create a new #JuliaLang project environment to try out some piece of code, use
`]activate --temp`
or
`Pkg.activate(; temp = true)`
A fresh Julia environment is created, independent of your current env, where you can install any packages needed for that code. When you exit your Julia session, this temporary env will be deleted.
Very useful for running code you don't intend to keep saved, eg. when debugging someone else's Julia code.
#juliatipoftheday #juliabeginners #julialang
Nice! You can consider also tagging them with #JuliaBeginners, since you're also including descriptions that are useful to beginners. That may help separate it from other posts that just want to post their solutions or show off different advanced ways of solving them.
#JuliaTipOfTheDay​ #JuliaBeginners
2022-11-28
Say you define a type
```
struct Point2D
x::Int
y::Int
end
```
then x and y are the "fields" of this struct. `fieldnames(Point2D)` returns `(:x, :y)`.
If you define `mypt = Point2D(4, 8)`, then
`mypt.x` calls `getproperty(mypt, :x)` (see prev. post),
which by default calls `getfield(mypt, :x)`,
which is automatically defined by #JuliaLang to return `x`'s value.
(contd. from previous: https://julialang.social/@Sundar/109411842573985228
as part 2 of #FieldsAndProperties)
#juliatipoftheday #juliabeginners #julialang #fieldsandproperties
#JuliaBeginners #JuliaTipOfTheDay​
2022-11-26
When you see `foo.bar` in your #JuliaLang code, Julia sees that as the call `getproperty(foo, :bar)`.
Let's say you load data into a #DataFrame `df`. When you access `df.Species`, that calls `getproperty(df, :Species)`.
DataFrames.jl has a matching method for that call, which returns `df[!, :Species]` i.e. makes it a column access.
You can do that for your custom types too! (Useful to understand "fields" before that, a post on those is upcoming.)
#juliabeginners #juliatipoftheday #julialang #dataframe
#JuliaBeginners #JuliaLang #JuliaTipOfTheDay​
2022-11-23
"I call `plot`, but I see no plot!"
This is a common source of confusion for beginners in Julia - calling `plot(..)` seems to work in the REPL and in notebooks, but not in a script.
Calling `plot(..)` only creates a Plot object. It's the job of `display` to actually show it. The REPL and notebooks automatically call `display` for you, but scripts need to do
`p = plot(..); display(p)`
or add a `show = true` argument to the `plot` call.
#juliabeginners #julialang #juliatipoftheday
#JuliaBeginners #JuliaLang #JuliaTipOfTheDay​
2022-11-22
If you have a `@chain` of operations (when using Chain.jl), and want to store an intermediate result of the chain, you can use
@aside var = _
and then continue the rest of the operations.
This can come up when using DataFramesMeta.jl, to store part of the data during a dataframe transformation chain. Eg.,
@aside var = _.columnname
(If var doesn't exist outside the `@chain` already, pre-declare it with a `local var` statement.)
#juliabeginners #julialang #juliatipoftheday
#JuliaBeginners #JuliaLang #JuliaTipOfTheDay​
2022-11-21
The search on the JuliaHub site (https://juliahub.com/ui/Search) is an incredible resource.
Came across a macro in a snippet of code, and need to know where the macro is from? Try searching for it via the Symbols tab, filtering by "definition" and "macro".
You can also search across the documentations of all registered packages, regex-search through public Julia code, etc.
#juliabeginners #julialang #juliatipoftheday #juliahub #search
#JuliaBeginners #JuliaLang #JuliaTipOfTheDay
2022-11-18
Say you're working at the REPL, and get an ERROR message with a Stacktrace. To look up the fourth method in the call chain, just type 4 and press Ctrl-Q. Your editor will instantly be opened, with the cursor on the location where that method is defined.
Also works for the results of `methods` and `methodswith` calls; just use the number in square brackets next to the method you want. Such a neat little quality-of-life feature of the REPL.
#juliabeginners #julialang #juliatipoftheday
#JuliaBeginners #JuliaLang #JuliaTipOfTheDay
2022-11-17
When writing a method definition, if there is a parameter c that's a complex value with integer parts, write it as `c::Complex{<:Integer}`. This will match an argument of type `Complex{Int}`, `Complex{Int8}`, Complex{Int128}`, etc.
`c::Complex{Integer}` (without the <: subtype specifier) doesn't match any of those, since `Complex{Int}` is not a subtype of `Complex{Integer}`. The fancy term for this is "parametric type invariance".
#juliabeginners #julialang #juliatipoftheday