shijith.k

shijith.k

Basic < Operator working between integer and list

How does the < work? By accident, in my code, I compared an integer with a list, and it gave me a boolean value as response instead of error.

iex(1)> integer = 1
1
iex(2)> list = [1]
[1]
iex(3) integer < list
true

So how does the operator work here?

Marked As Solved

NobbZ

NobbZ

</2 and friends work by comparing “terms” as explained in the erlang manual:

Also Liked

kelvinst

kelvinst

It is also explained on the Elixir guides: https://elixir-lang.org/getting-started/basic-operators.html

For those who don’t want to open another site:

The reason we can compare different data types is pragmatism. Sorting algorithms don’t need to worry about different data types in order to sort. The overall sorting order is defined below:

number < atom < reference < function < port < pid < tuple < map < list < bitstring
kelvinst

kelvinst

PS.: just expressing my opinion on the matter, I think this is a really nice way to handle comparison between different data types. I mean, a lot better than what JS does, trying to discover what the user means by comparing different types. And that also allows some powerful stuff, like using any type as a key on maps and still be able to do a binary search on it, without any extra function needed :smiley:

Last Post!

kelvinst

kelvinst

PS.: just expressing my opinion on the matter, I think this is a really nice way to handle comparison between different data types. I mean, a lot better than what JS does, trying to discover what the user means by comparing different types. And that also allows some powerful stuff, like using any type as a key on maps and still be able to do a binary search on it, without any extra function needed :smiley:

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement