List Comprehensions
List comprehensions in Erlang use ||
as a separator between expression and generators, but |
is used in Hamler.
Hamler:
[x*2 | x <- [1,2,3]] -- [2,4,6]
-- multiple generators
[(x,y) | x <- [1,2,3], y <- [4,5]]
-- dependent generators
[(x,y) | x <- [1..3], y <- [x..3]]
-- Conditions
even i = 0 == i % 2
[x | x <- [1..10], even x]
Erlang:
[X*2 || X <- [1,2,3]]. %% [2,4,6]
-- multiple generators
[{X, Y} || X <- [1,2,3], Y <- [4,5]].
-- dependent generators
[{X, Y} || X <- [1,2,3], Y <- lists:seq(X,3)].
-- Conditions
even(I) -> 0 == (I rem 2).
[X || X <- lists:seq(1, 10), even(X)].
当前内容版权归 hamler-lang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 hamler-lang .