Tutorial by Examples: comprehension

The for comprehension is a compact way to run a block of code that depends on the successful result of multiple futures. With f1, f2, f3 three Future[String]'s that will contain the strings one, two, three respectively, val fCombined = for { s1 <- f1 s2 <- f2 ...
You can do neat things via the results of Array "comprehensions"... Like assign multiple variables... from the result of a looping for statement... [express,_] = (require x for x in ['express','underscore']) Or a syntactically sweet version of a "mapped" function call, etc.....
Options have a flatMap method. This means they can be used in a for comprehension. In this way we can lift regular functions to work on Options without having to redefine them. val firstOption: Option[Int] = Option(1) val secondOption: Option[Int] = Option(2) val myResult = for { firstValue ...
Before the Julia 0.5, there is no way to use conditions inside the array comprehensions. But, it is no longer true. In Julia 0.5 we can use the conditions inside conditions like the following: julia> [x^2 for x in 0:9 if x > 5] 4-element Array{Int64,1}: 36 49 64 81 Source of the ...
Python allows you to hack list comprehensions to evaluate conditional expressions. For instance, [value_false, value_true][<conditional-test>] Example: >> n = 16 >> print [10, 20][n <= 15] 10 Here n<=15 returns False (which equates to 0 in Python). So what Python i...
For iterating more than two lists simultaneously within list comprehension, one may use zip() as: >>> list_1 = [1, 2, 3 , 4] >>> list_2 = ['a', 'b', 'c', 'd'] >>> list_3 = ['6', '7', '8', '9'] # Two lists >>> [(i, j) for i, j in zip(list_1, list_2)] [(1, '...
Nested for loops may be used to iterate over a number of unique iterables. result = [] for a = iterable_a for b = iterable_b push!(result, expression) end end Similarly, multiple iteration specifications may be supplied to an array comprehension. [expression for a = iterabl...
Generator comprehensions follow a similar format to array comprehensions, but use parentheses () instead of square brackets []. (expression for element = iterable) Such an expression returns a Generator object. julia> (x^2 for x = 1:5) Base.Generator{UnitRange{Int64},##1#2}(#1,1:5) Fun...

Page 2 of 2