an erlang monad?

Luke Gorrie luke@REDACTED
Wed Mar 2 22:54:54 CET 2005


"Ulf Wiger \(AL/EAB\)" <ulf.wiger@REDACTED> writes:

> I have had reason to do some rather complex code using
> a 'dict' dictionary. I find myself constantly wishing
> for a library function (and eventually writing it 
> myself) that does the following:
> 
> with(Dict, Actions) ->
>     lists:foldl(fun(F,D) ->
>                         F(D)
>                 end, Dict, Actions).
> 
> That is, fold over a list of functions that update
> the dictionary. This, I believe, essentially amounts
> to a monad in other languages. ;-) Even if this is 
> just me misunderstanding monads (like everyone else),
> I find the function very useful.
> 
> What this does is basically free you from the absurd
> D1, D2, D3, etc. gymnastics, where you will eventually,
> inevitably mess up the numbering.
> 
> An example from my 'builder' module on Jungerl:
> 
>     Dict = with(Dict0,
>                 [fun(D) -> read_options_file(D) end,
>                  fun(D) -> store_options(Options, D) end,
>                  fun(D) -> post_process_options(D) end,
      ...

But surely the state monad is just a poor man's process dictionary..

    D = make_ref(),
    put(D, mk_dict(default_options() ++ Options)),
    read_options_file(D),
    store_options(Options, D),
    post_process_options(D),
    ...
    erase(D).





More information about the erlang-questions mailing list