Return Values ​​in Elixir?

Recently, I decided to study the Elixir. Based on the background of C ++ / Java / JavaScript, I am having trouble understanding the basics. This may sound silly, but how would I return the statements to Elixir? I looked around and it seems that this is only the last line of the ie function

def Hello do "Hello World!" end 

Will this function return “Hello World!”, Is there any other way to return? Also, how would you return earlier? in JavaScript, we could write something like this to find out if it has a specific meaning:

 function foo(a){ for(var i = 0;i<a.length;i++){ if(a[i] == "22"){ return true; } } return false; } 

How does it work in Elixir?

+6
source share
2 answers

Elixir does not have the keyword “break out”, which would be equivalent to the keyword “return” in other languages.

Typically, you should restructure your code so that the last statement executed returns a value.

The following is an idiomatic way for an elixir to do the equivalent of your code test, assuming you meant “a” as something that behaves like an array, as in your original example:

 def is_there_a_22(a) do Enum.any?(a, fn(item) -> item == "22" end.) end 

What is really going on here, we are rebuilding our thinking a bit. Instead of a procedural approach, where we will look for an array and return earlier, if we find what we were looking for, we will ask what you really do in the code fragment: "Does this array have 22 anywhere?"

Then we are going to use the Enum elixir library to run through the array and provide the any? with a test that will tell us if anything meets the criteria we cared about.

Although this is a case that can be easily solved with the help of an enumeration, I think that perhaps the basis of your question relates more to such things as the “embroidery pattern” used in procedural methods. For example, if I meet certain criteria in a method, return immediately. A good example of this would be a method that returns false if the thing you're going to work on is null:

 function is_my_property_true(obj) { if (obj == null) { return false; } // .....lots of code.... return some_result_from_obj; } 

The only way to achieve this in the elixir is to set a guard and decompose the method:

 def test_property_val_from_obj(obj) do # ...a bunch of code about to get the property # want and see if it is true end def is_my_property_true(obj) do case obj do nil -> false _ -> test_property_value_from_obj(obj) end end 

tl; dr - No, there is no equivalent - you need to structure your code accordingly. On the other hand, this leads to the fact that your methods are small - and your intention is clear.

+5
source

You are right that the last expression always returns. Moreover - there are no expressions in the language - just expressions. Each construct matters - if , case , etc. Also there is no early return. It may seem limited, but you quickly get used to it.

The usual way to solve your Elixir example would be to use a higher order function:

 def foo(list) do Enum.any?(list, fn x -> x == "22" end) end 
+4
source

All Articles