Sundar R :julia: · @Sundar
34 followers · 26 posts · Server julialang.social


2022-12-01

To quickly create a new 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

Last updated 3 years ago

Sundar R :julia: · @Sundar
28 followers · 22 posts · Server julialang.social


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 to return `x`'s value.

(contd. from previous: julialang.social/@Sundar/10941
as part 2 of )

#juliatipoftheday #juliabeginners #julialang #fieldsandproperties

Last updated 3 years ago

Sundar R :julia: · @Sundar
25 followers · 21 posts · Server julialang.social


2022-11-26

When you see `foo.bar` in your code, Julia sees that as the call `getproperty(foo, :bar)`.

Let's say you load data into a `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

Last updated 3 years ago

Sundar R :julia: · @Sundar
21 followers · 15 posts · Server julialang.social


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

Last updated 3 years ago

Sundar R 🟢 🟣 🔴 · @Sundar
18 followers · 7 posts · Server julialang.social


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

Last updated 3 years ago

Sundar R 🟢 🟣 🔴 · @Sundar
13 followers · 4 posts · Server julialang.social


2022-11-21

The search on the JuliaHub site (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

Last updated 3 years ago

Sundar R 🟢 🟣 🔴 · @Sundar
5 followers · 3 posts · Server julialang.social


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

Last updated 3 years ago

Sundar R 🟢 🟣 🔴 · @Sundar
4 followers · 1 posts · Server julialang.social


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

Last updated 3 years ago