Last active
May 7, 2019 14:19
-
-
Save elbrujohalcon/35c6491c6a1bbd5205f134ab70bb68dc to your computer and use it in GitHub Desktop.
Esos Locos Bajitos (by Joan Manuel Serrat)
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 crazy_shorties. | |
-author(joan_manuel). | |
-export [new/0, teach/1, direct/1]. | |
new() -> | |
Child = spawn_link(fun live/0), | |
timer:send_interval(1000, Child, tick), | |
Child. | |
live() -> | |
live(#{direction => undefined, carried_stuff => [], age_in_seconds => 0}). | |
live(State) -> | |
NewState = | |
receive | |
{compare, Parent} -> | |
ParentInfo = erlang:process_info(Parent), | |
ChildInfo = erlang:process_info(self()), | |
Similarities = ParentInfo -- ChildInfo, | |
Parent ! {satisfied, Similarities}, | |
State; | |
{gesture, Gesture, Context} -> | |
Stuff = context:get_nearby(Context), | |
child:shake_with(Gesture, Stuff), | |
State; | |
{stand_up, _Time, _Traditions} -> | |
child:open_eyes(full_width), | |
child:stand_up(), | |
State; | |
{shout, Stuff} -> | |
child:learn(Stuff), | |
State; | |
{god, God} -> | |
State#{carried_stuff := [God | maps:get(carried_stuff, State)]}; | |
{learn, Word} -> | |
State#{carried_stuff := [Word | maps:get(carried_stuff, State)]}; | |
{grudge, Grudge} -> | |
State#{carried_stuff := [Grudge | maps:get(carried_stuff, State)]}; | |
{future, Future} -> | |
State#{carried_stuff := [Future | maps:get(carried_stuff, State)]}; | |
{tale, Tale} -> | |
child:listen_to(Tale), | |
timer:sleep(1000), | |
State; | |
{go, Direction} -> | |
State#{direction := Direction}; | |
{drink, Milk, Context} -> | |
child:drink(Milk), | |
Frustrations = context:get_frustrations(Context), | |
State#{carried_stuff := [Frustrations | maps:get(carried_stuff, State)]}; | |
{listen, Song, Context} -> | |
child:listen_to(Song), | |
Frustrations = context:get_frustrations(Context), | |
State#{carried_stuff := [Frustrations | maps:get(carried_stuff, State)]}; | |
tick -> | |
try | |
State#{ | |
direction := child:choose_direction(), | |
age_in_seconds := maps:get(age_in_seconds, State) + 1 | |
} | |
catch | |
_:Error -> | |
State#{ | |
carried_stuff := [Error | maps:get(carried_stuff, State)], | |
age_in_seconds := maps:get(age_in_seconds, State) + 1 | |
} | |
end | |
end, | |
case child:is_adult(State) of | |
true -> | |
{links, [ParentPid]} = erlang:process_info(self(), links), | |
ParentPid ! good_bye, | |
unlink(ParentPid), | |
adult:live(State); | |
false -> | |
live(NewState) | |
end. | |
teach(Child) -> | |
Child ! {shout, "quit pestering with the ball!"}, | |
Child ! {shout, "don't say that!"}, | |
Child ! {shout, "don't do that!"}, | |
Child ! {shout, "don't touch that!"}. | |
direct(Child) -> | |
Child ! {go, lists:nth(rand:uniform(4), [up, down, left, right])}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment