For comprehension - if statement

Hi, Is there if statement in for comprehension?
For example in Scala I can do something like this

for {
  x <- xs
  y <- ys
  if cond
} yield (x, y)
2 Likes

A for comprehension can contain filters, which are predicates. For example:

    [1, 2, 4, 3, 6, 9, 4, 8, 12, 16]``` 

The elixir web site has a good page on comprehensions:
http://elixir-lang.org/getting-started/comprehensions.html especially the example of the Pythagorean triple.
4 Likes

You can use a function as a filter.

is_equal? = fn(n, m) -> n == m end

for x <- [1, 3, 4, 5, 7],
    y <- [2, 3, 3, 5, 6],
    is_equal?.(x, y),
    do: { x, y }
4 Likes