erlang小函数

assq(Key, List) ->
    case lists:keysearch(Key, 1, List) of
{value, {Key, Val}} -> {value, Val};
_ -> false
    end.
    
get_option(Key, Options) ->
    case lists:keysearch(Key, 1, Options) of
{value, {_Key, Value}} -> 
        Value;
_ ->
   throw({error, {not_found, Key}})
    end.
get_option(Key, Options, Default) ->
    case lists:keysearch(Key, 1, Options) of
{value, {_Key, Value}} -> 
   Value;
_ -> 
   Default
    end.
set_option(Key, Val, Opts) ->
    keyreplaceadd(Key, 1, Opts, {Key, Val}).
keyreplaceadd(Key, Pos, List, New) ->
    case lists:keysearch(Key, Pos, List) of
{value, _} -> lists:keyreplace(Key, Pos, List, New);
_ -> [New | List]
    end.
posted @ 2011-01-06 14:55  麦飞  阅读(460)  评论(0编辑  收藏  举报