[erlang-questions] programming tip: a note on encapsulation

Anthony Shipman als@REDACTED
Wed Nov 18 10:15:47 CET 2009


On Wed, 18 Nov 2009 07:56:55 pm Joe Armstrong wrote:
> We change the server code so that it reads:
>
>     F = make_interface(Pid, Root),
>     Mod:handle(Uri, Args, F),
>     ...
>
> Where
>
> make_interface(Pid, Root) ->
>    fun(root) -> Root;
>       (X)    -> lib_db:rpc(Pid, X)
>    end.

That's a common pattern used with foldl et al e.g

folder(A, B) ->
    fun(Arg, Result) -> 
        Arg + Result + A + B
    end

lists:foldl(folder(1, 2), 0, [2,4])

-- 
Anthony Shipman                    Mamas don't let your babies 
als@REDACTED                   grow up to be outsourced.


More information about the erlang-questions mailing list