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.

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.

2 Likes
list = ["1","2","3"]
list |> Enum.map(&String.to_integer/1)

2 Likes

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.

2 Likes

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
4 Likes

This is very descriptive … Even I learned something how to write the code.
Thanks a lot :bouquet: :bouquet:

1 Like

It’s overkill for this problem, but I’ve written a library that solves the “is this similar to X” problem based on a single instance of X. i.e. it defines “types” based on a single instance and a definition of similar.

You give it a single instance and it returns a function that returns true or false given any other term
based on whether the term is in the same similarity class. I’ve defined 3 classes so far, congruent, similar and exact.

It’s still a work in progress and in this case it would probably match things you don’t care about since it treats all Strings as similar. i.e. [“a”,“b”] would be an exact similarity to [“1”,“2”].

The real intended use was for deeply nested data structures.

Some samples from my tests below.

https://github.com/philosophers-stone/phenetic/blob/master/test/phst_phenetic_test.exs

1 Like