Skip to content

Instantly share code, notes, and snippets.

@savonarola
Created March 18, 2009 13:26
Show Gist options
  • Save savonarola/81115 to your computer and use it in GitHub Desktop.
Save savonarola/81115 to your computer and use it in GitHub Desktop.
% a_model.erl
-module(a_model).
-export([init/1,handle/2]).
-behaviour(model).
init({X,Y}) ->
{ok,{X,Y}}.
handle(Self,get_x) ->
{X,_Y} = model:state(Self),
{ok,X};
handle(Self,get_y) ->
{_X,Y} = model:state(Self),
{ok,Y};
handle(Self,{set_x,NewX}) ->
{X,Y} = model:state(Self),
{ok, X, {NewX, Y}};
handle(Self,{set_y,NewY}) ->
{X,Y} = model:state(Self),
{ok, Y, {X, NewY}};
handle(Self, get_y_square) ->
{_NewSelf, Y} = model:call(Self,get_y),
{ok, Y*Y}.
% b_module.erl
-module(b_model).
-export([init/1,handle/2]).
-behaviour(model).
init({X,Y,Z}) ->
{ok, Z, {a_model,{X,Y}}}.
handle(Self,get_z) ->
Z = model:state(Self),
{ok,Z};
handle(Self,{set_z,NewZ}) ->
OldZ = model:state(Self),
{ok, OldZ, NewZ};
handle(_Self,get_y) ->
{ok,1000}.
%Eshell V5.6.5 (abort with ^G)
%1> M =model:new(b_model,{1,2,3}).
%{{b_model,3},{a_model,{1,2}}}
%2> {_,Res} = model:call(M,get_x), Res.
%1
%3> {_,Res1} = model:call(M,get_z), Res1.
%3
%4> {_,Res2} = model:call(M,get_y_square), Res2.
%1000000
%5>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment