Last active
February 10, 2017 20:38
-
-
Save EarlGray/86197b7a2f5246a3ed9572fccd62f4e1 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
-module(loop). | |
-export([for/2, for/4, while/2, repeat/2]). | |
%%% Let's make Erlang great again! | |
% Look, today we are going to fix the glaring omission, | |
% I promise you that. | |
% We are going to build a loop module for Erlang! | |
% A loop module for Erlang, yes! | |
for([], _Fun) -> []; | |
for([H | T], Fun) -> | |
Fun(H), for(T, Fun). | |
for(Start, Cond, Step, Fun) -> | |
I = Start(), | |
C = Cond(I), | |
if | |
C -> | |
Fun(I), | |
for(fun() -> Step(I) end, Cond, Step, Fun); | |
true -> | |
ok | |
end. | |
% if you need state here, use actors or whatever, now it's your problem | |
while(Cond, Fun) -> | |
C = Cond(), | |
if C -> Fun(), while(Cond, Fun); | |
true -> ok | |
end. | |
% see the comment for `while/2` | |
repeat(Fun, Cond) -> | |
Fun(), | |
while(Cond, Fun). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment