A switch type function

It seems that Julia doesn’t support a switch-case type function. I have the following code example, and it wouldn’t pass in Julia:

Function Gauss(option)
##Here, option is a string
if option ==‘L3’
a=0
end

end

What is the solution for this?

1 Like

I was looking for this too, but it is currently not implemented. See the following proposal for more information:

A case/switch statement for Julia · Issue #18285 · JuliaLang/julia · GitHub

2 Likes

Using valid syntax is always an option:

function gauss(option)
    if option == "L3"
        a=0
    end
end
11 Likes

Thanks for the link. There are no alternatives for this? If you look the code example above, it looks very simple to me.

Ok. Many thanks. It is working!

Python doesn’t have one either. You’re expected to use multiple dispatch, elseif chains, or dictionaries for most cases.

The proposal for a switch statement is not intended to be something you use all the time, but for performance sensitive code where you want to guarentee that the compiler is generating a jump table.

A chain of simple if statements generate a branch table in julia anyway.

5 Likes

Right, optimization passes largely eliminate the direct need for it. Syntax-wise, Julia has macros, and there’s packages like match.jl that provide macros for this and more.

Hi! I have tested your suggestion, however, I encountered a problem for the case that I am evaluating:
I want to do something like this:

function tipo2(n)
    if 0.1 ≤ n ≤ 0.6
        m = 1
    elseif 0.6 < n ≤ 0.8
        m = 1.1
    elseif 0.8 < n ≤ 1.0
        m = 1.2
    elseif 1.0 < n ≤ 1.3
        m = 1.35
    else m = 2.2
    end
end

Using @match I tried to use like this:

m = @match n begin
        0.1 ≤ n ≤ 0.6  => 1
        0.6 < n ≤ 0.8  => 1.1
        0.8 < n ≤ 1.0  => 1.2
        1.0 < n ≤ 1.3  => 1.35
        _              => 2.2
    end

However, no matter what will be the value of n (which varies from 0.1 to 1.6 by 0.01), m always is 2.2.
Did I do something wrong?
Regards

3 Likes

You’ll probably want to use range patterns instead of inequalities here if you are using @match.

@Olof_Salberger, how could one use the range patterns in Match.jl in order to solve @ameresv’s switch problem?

1 Like

You can use MLStyle.jl for this Pattern Matching — MLStyle.jl Documentation

2 Likes

@jzr, thanks a lot, it works but a bit more verbose:

using MLStyle
n = 1.0
m = @match n begin
        if 0.1 ≤ n ≤ 0.6 end    => 1
        if 0.6 < n ≤ 0.8 end    => 1.1
        if 0.8 < n ≤ 1.0 end    => 1.2
        if 1.0 < n ≤ 1.3 end    => 1.35
        _                       => 2.2
    end

1.2

Yes it would be nice to have a shorter syntax than if/end. where would be a good candidate but it currently has the wrong operator precedence.

Less verbose (and builtin) is chained ternaries. With indentation they can be quite readable for short expressions like these:

n = 1.0
m =       n ≤ 0.1 ? 0.0 :
    0.1 < n ≤ 0.6 ? 1.0 :
    0.6 < n ≤ 0.8 ? 1.1 :
    0.8 < n ≤ 1.0 ? 1.2 :
    1.0 < n ≤ 1.3 ? 1.35 :
    #= otherwise =# 2.2
17 Likes

@mbauman, love it.

Using an early return is quite common in Julia, it’s concise and readable too:

function tipo2(n)
    0.1 ≤ n ≤ 0.6 && return 1.0
    0.6 < n ≤ 0.8 && return 1.1
    0.8 < n ≤ 1.0 && return 1.2
    1.0 < n ≤ 1.3 && return 1.35
    return 2.2
end
6 Likes

Magic switch function :stuck_out_tongue_winking_eye::

switch(f, x...) = f(x...)

Usage:

n = 1.0
m = switch(n) do n
    n ≤ 0.1 && return 0.0
    n ≤ 0.6 && return 1.0
    n ≤ 0.8 && return 1.1
    n ≤ 1.0 && return 1.2
    n ≤ 1.3 && return 1.35
    return 2.2
end
1.2
x, y = 0.3, 0.4+eps()
z = switch(x, y) do x, y
    x^2 + y^2 ≤ 0.5^2 && return 0.5
    x^2 + y^2 ≤ 1.0^2 && return 1.0
    return 2.0
end
1.0

Edit 1: Thank you, Eben60-san.
Edit 2: Add another example.

5 Likes

It is not necessary to check the left interval border.

n = 1.1
m = n ≤ 0.1   ? 0.0 :
    n ≤ 0.6   ? 1.0 :
    n ≤ 0.8   ? 1.1 :
    n ≤ 1.0   ? 1.2 :
    n ≤ 1.3   ? 1.35 :
    #= otherwise =# 2.2
6 Likes