Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit b8320329e18e9713934e5727fb07491e57e97185 @etrepum committed May 14, 2012
Showing with 58 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +5 −0 rebar.config
  3. +9 −0 src/markov.app.src
  4. +38 −0 src/markov.erl
6 .gitignore
@@ -0,0 +1,6 @@
+/.eunit
+/ebin
+/TEST-*.xml
+/erl_crash.dump
+/deps
+/doc
5 rebar.config
@@ -0,0 +1,5 @@
+%% -*- mode: erlang -*-
+{erl_opts, [warnings_as_errors, debug_info]}.
+{cover_enabled, true}.
+{eunit_opts, [verbose, {report, {eunit_surefire,[{dir,"."}]}}]}.
+{clean_files, ["*.eunit", "ebin/*.beam"]}.
9 src/markov.app.src
@@ -0,0 +1,9 @@
+%% -*- mode: erlang -*-
+{application, markov,
+ [{description, ""},
+ {vsn, git},
+ {registered, []},
+ {applications, [kernel,
+ stdlib]},
+ %%{mod, {markov_app, []}}
+ {env, []}]}.
38 src/markov.erl
@@ -0,0 +1,38 @@
+-module(markov).
+-export([new/0, input/2, fetch/2]).
+
+new() ->
+ append({start, start}, stop, gb_trees:empty()).
+
+input(L, T) ->
+ input(L, {start, start}, T).
+
+input([C | Rest], {A, B}, T) ->
+ input(Rest, {B, C}, append({A, B}, C, T));
+input([], {A, B}, T) ->
+ append({A, B}, stop, T).
+
+append(K, V, T) ->
+ case gb_trees:lookup(K, T) of
+ none ->
+ gb_trees:insert(K, {1, [V]}, T);
+ {value, {Count, Others}} ->
+ gb_trees:update(K, {1 + Count, [V | Others]}, T)
+ end.
+
+fetch(Max, T) ->
+ fetch({start, start}, erlang:now(), Max, T).
+
+fetch(K={_A, B}, RState, Max, T) when Max > 0 ->
+ case choose(gb_trees:lookup(K, T), RState) of
+ {stop, _} ->
+ [];
+ {C, RState1} ->
+ [C | fetch({B, C}, RState1, Max - 1, T)]
+ end.
+
+choose({value, {Count, L}}, RState) ->
+ {N, RState1} = random:uniform_s(Count, RState),
+ {lists:nth(N, L), RState1};
+choose(none, RState) ->
+ {stop, RState}.

0 comments on commit b832032

Please sign in to comment.
Something went wrong with that request. Please try again.