Python does not have if statements

Yes of course i does. Please look at the interesting tweet below.
Julia has the Bool type of course. But when I did a little digging I find that Bool takes up 8 bits - I guess that does not really matter on modern computer systems with lots of memory.
Would the same ‘trick’ as in this tweet work in Julia though?
https://docs.julialang.org/en/v1/base/numbers/#Core.Bool
I guess not - or can you use Bool as an index?

You sure can but we start indexing with one. So this will work in Julia if you add 1 to the Bool (or use OffsetArrays). But… why?

1 Like

In fact, working with “bits” directly is either impossible or expensive on modern architectures. You would not be “saving” 7 bits. Cf performance if BitArray vs Array{Bool}.

Also, the Bool in a condition usually does not end up in RAM.

5 Likes

Memory addresses are referring to bytes, not to single bits (see Memory address - Wikipedia).
Thus, you would not even save memory when you do not need to store 8 or more booleans because a partly occupied byte is not usable for other purposes.
In BitArrays, each byte of memory is used to store 8 booleans, but this has a performance cost because the byte content has to be converted to single booleans.
This is at least my understanding, please correct me if I am wrong.

4 Likes

i have looked the tweet. i think you can get the same outputs in Julia using the ternary operator like

num = 100; numberGreaterThan10 = num > 10 ? true : false

And there is also a ternary operator in Python.

You can index a Dict with no issues.

julia> num = 100
100

julia> Dict([false,true] .=> ["Number is less than 10", "Number is greater than 10"])[>(10)(num)]
"Number is greater than 10"

julia> Dict([false,true] .=> ["Number is less than 50", "Number is greater than 50"])[>(50)(num)]
"Number is greater than 50"

julia> Dict([false,true] .=> ["Number is less than 100", "Number is greater than 100"])[>(100)(num)]
"Number is less than 100"

I did not understand exactly what the question is, but this syntax of course also works:

julia> n = 101
101

julia> isgt100 = if n > 100 true else false end
true

And another interesting syntax (not sure if useful at all, but maybe), is:

julia> function (x::Int)(n)
         if x > n
           true
         else
           false
         end
       end

julia> x = 100
100

julia> x(99)
true

julia> x(101)
false

1 Like

More or less. Booleans in Julia are a type of integer:

julia> Bool <: Integer
true

and you can use them to index arrays by adding 1 (since the standard array type in Julia is 1-based):

julia> println(["100 is < 10", "100 is > 10"][(100 > 10)+1])
100 is > 10
1 Like

But it is bad style, both in Python and Juila.

1 Like

@Tamas_Papp – do you mind providing a citation for this? I’m unaware of any impossibilities or expensive outcomes when using bitwise operators in modern architectures.

Use of 0 for false and 1 for true in data science is useful and pretty much ubiquitious. For example, you could sum an array of boolean values to count the number of true outcomes.

This is fine, I was rather referring to write

foo = ["ifFalse","ifTrue"][booleanCondition]

instead of (in Python) e.g.

foo = "ifTrue" if booleanCondition else "ifFalse"

or e.g. a ternary operator in Julia.

The latter versions are both more readable (at least in my oppinion) and should be faster (no creation of an intermediate list/array).

2 Likes

In julia you can commit some misdemeanor type piracy to make this work:

julia> Base.to_index(b::Bool) = b + 1

julia> ["ifFalse", "ifTrue"][true]
"ifTrue"

julia> ["ifFalse", "ifTrue"][false]
"ifFalse"

Disclaimer: Please please please don’t actually do this in real code

5 Likes

I think you misunderstand — bitwise operators are fine, but using individual bits as first-class objects (eg for the purposes of a conditional) is not a thing. Bits are just not the unit modern CPUs or RAM work with directly.

1 Like