What's your favorite syntactical sugar in Julia

Python has it, plus a general facility for writing your own "context manager"s. Common Lisp has had it forever. I would be surprised if there are not several other languages as well. There’s a tendency to attribute innovation to the big player (Not just in software, but engineering in general, and probably wider still) I’m susceptible to this. I kind of vaguely thought Julia got comprehensions from python.

1 Like

Using unicode symbols for constants. Instead of calling something like np.pi writing π just feels right.

5 Likes

I like end
As in xs[end] for the last element.
but end can do lots of other cool things.
Because it is replaces with lastindex(xs) during lowering
Meaning you can use it just like a number.

Like getting the first 5 elements, or all the elements if there are less than 5:
xs[1:min(end, 5)]

Or or breaking the data into splits:

train = data[1:9end÷10]
test = data[9end÷10 + 1 : end]
27 Likes

I like @mbauman 's example for constructing from a finite array an array whose elements repeat forever x[mod1(i, end)].

5 Likes

@Albert_Zevelev, Julia’s diabetics abstain, it’s too much sugar.

1 Like

As of now my favorite sugar is

julia> function f()
           (; a = 1, b = 2, c = 3)
       end
f (generic function with 1 method)

julia> (; b, c) = f()
(a = 1, b = 2, c = 3)

julia> b
2

julia> c
3
16 Likes

This had never occured to me - really neat!

This is erroring for me in the REPL on 1.6rc1. Any idea why?

ERROR: syntax: invalid assignment location "; b, c" around REPL[9]:1
Stacktrace:
 [1] top-level scope
   @ REPL[9]:1
1 Like

It’s 1.7 feature :slight_smile: For those, who like to live on the edge.

10 Likes

Well past the edge, since 1.6 isn’t out yet :slight_smile: perhaps we could call it “off the edge of the world and into the starry darkness of nightly

5 Likes

Also wrong on 1.5.3. I am wondering what the feature is called and where is the documentation :grin:

https://docs.julialang.org/en/v1.7-dev/NEWS/

It’s worth noting that this new feature also allows you to unpack structs (i.e., pattern matching) in function arguments, like this:

struct A
    x
    y
end

foo((; x, y)::A) = x + y
julia> foo(A(1, 2))
3
22 Likes

This (; ...) named destructuring is awesome for working with data frames:

julia> using DataFrames

julia> df = DataFrame(rand.([Bool, "abc", Float64, Float64], 5),
                      ["flag", "type", "x", "y"])
5×4 DataFrame
 Row │ flag   type  x         y         
     │ Bool   Char  Float64   Float64   
─────┼──────────────────────────────────
   1 │ false  c     0.757074  0.637436
   2 │  true  a     0.12791   0.163183
   3 │ false  b     0.452078  0.547038
   4 │  true  c     0.187923  0.0529017
   5 │  true  b     0.10274   0.242524

julia> for (; x, y) in eachrow(df)
           println("x-y = $(x-y)")
       end

julia> map(eachrow(df)) do (; x, y)
           x-y
       end

julia> (; x, y) = df
17 Likes

No body mention function definition?
e.g. f(x) = x^2

19 Likes

I wonder if anyone here realized that refined sugar is poisonous to humans?

9 Likes

Happy Birthday @PetrKryslUCSD!
Today you can have as much sugar as you want!

4 Likes

Some poison from excess sugar:

julia> f(x) = f(x)
f (generic function with 1 method)
julia> f(1)
ERROR: StackOverflowError:
Stacktrace:
 [1] f(x::Int64) (repeats 79984 times)
7 Likes

Thank you, I would rather take the other 364 days without sugar. :slight_smile:

2 Likes

Every feature of Julia that I don’t yet know is a potential favorite syntactical sugar.

Just a moments ago, my favorite changed from the |> and .|> to the assert macro

@assert( σ >=0 , "σ must be non-negative")

:man_facepalming:

2 Likes