acrolink
How to check if a variable is a list of the nature ["1", "2"]?
And how to convert the above list to [1, 2] (i.e. to list of integers). Thank you.
Most Liked
NobbZ
If you want to check if all elements of a list do have a certain attribute, you can use Enum.all?, to check if something is a binary you can use is_binary/1.
So to check if all items in a list are binarys you can do the following:
iex(1)> list1 = ["1", "2"]
["1", "2"]
iex(2)> list2 = [:a, "1"]
[:a, "1"]
iex(3)> Enum.all?(list1, &is_binary/1)
true
iex(4)> Enum.all?(list2, &is_binary/1)
false
Next we will do the conversion, you asked for. String.to_integer/1 do what you have asked for directly, but will raise as soon as they hit the first string that does not represent a number.
Therefore we do also have Integer.parse/2. It does either return a tuple or :error, whenever you get :error you do not have a valid integer in the string, but as soon as you get a tuple, you know, that at least your string was prefixed by something that represents the integer in the first element of the tuple. Depending on your needs, you do want to check if the second element (remainder) is equal to "", to ensure, that there was nothing behind the number.
So to convert a list and also check if all elements could be converted, I’d do something like this:
# untested
def check_and_convert_list(list) do
parsed_list = Enum.map(list, &Integer.parse/1) # [{1, ""}, {2, ""}]
valid = Enum.all?(parsed_list, fn ({_, ""}) -> true
(_) -> false
end)
if valid do
Enum.map(parsed_list, fn ({x, _}) -> x end)
else
:error
end
end
Eiji
Use Enum or Stream to map a List and for each element use &String.to_integer/1 and for @spec use String.t like in &String.to_integer/1 method.
blackode
bobbypriambodo
The second question can be done by using Enum.map/2 and String.to_integer/1. A nice way to exercise mapping a function through a list!
For the first question, how much of the “nature” would you want to capture? It being a:
- two-element list? Can it have more elements?
- two-element list of anything?
- two-element list that are strictly equal to
["1", "2"]? - two-element list of strings?
- two-element list of strings that represent integers?
- two-element list of strings that represent integers and are strictly increasing?
It might help to expand on what you actually want.
blackode
This is very descriptive … Even I learned something how to write the code.
Thanks a lot
![]()








