Last active
December 15, 2015 10:19
-
-
Save sudowork/5244478 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % 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