Skip to content

Instantly share code, notes, and snippets.

@sudowork
Last active December 15, 2015 10:19
Show Gist options
  • Select an option

  • Save sudowork/5244478 to your computer and use it in GitHub Desktop.

Select an option

Save sudowork/5244478 to your computer and use it in GitHub Desktop.
% Simple comprehensions (notice the similarity to map?)
% increment
[ X+1 || X <- [1,2,3,4] ].
% powers of two
[ math:pow(2,X) || X <- [1,2,3,4] ].
% Comprehension with list of tuples
CityWeathers = [{cloudy, chicago}, {sunny, florida}, {raining, london}, {cloudy, durham}].
[ case Weather of cloudy -> io:format("~p is cloudy\n", [City]); _ -> false end || {Weather, City} <- CityWeathers ].
% Almost the same thing: using a filter instead of case
[ io:format("~p is cloudy\n", [City]) || {Weather, City} <- CityWeathers, Weather =:= cloudy ].
% Even better: anything that doesn't match the pattern gets ignored
[ io:format("~p is cloudy\n", [City]) || {cloudy, City} <- CityWeathers ].
% Multiple generators
ChessBoard = [ {X, Y} || X <- lists:seq(1,8), Y <- lists:seq(1,8) ].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment