[erlang-questions] Re: The If expression

Richard O'Keefe <>
Fri Apr 23 00:14:40 CEST 2010

On Apr 23, 2010, at 2:28 AM, James Aimonetti wrote:

> This thread is well-timed as I have an issue I wanted some help on.
> I have two lists, one of keys and one of values. I want to zip them  
> together, but I have one specific key/value combo that I want to  
> exclude from the zipped list. For context, this is creating an  
> appropriate {proplist} for couchbeam.
> to_couchbeam(Fields, Values) ->
> { lists:flatten( lists:zipwith(fun to_couchbeam_zipper/2, Fields,  
> Values) ) }.
> to_couchbeam_zipper(F, V) ->
> K = to_couchbeam_key(F),
> Val = to_couchbeam_value(V),
> case Key == <<"_rev">> andalso Val == undefined of
>   true -> [];
>   false -> {K, Val}
> end.

to_couchbeam([], []) ->
to_couchbeam([Field|Fields], [Value|Values]) ->
     Key = to_couchbeam_key(Field),
     Val = to_couchbeam_value(Value),
     Tail = to_couchbeam(Fields, Values)
     case {Key,Val}
       of {<<"_rev">>, undefined} -> Tail
        ; Pair                    -> [Pair|Tail]

What have 'true' or 'false' to do with it?
It's <<"_rev">> and 'undefined' you want to watch out
for, so it's they that belong in the case.
Why build a list including stuff you don't want,
and then strip it out?

What we really want here is EEP 19

to_couchbeam(Fields, Values) ->
     [Pair || F <- Fields && V <- Values,
              Pair = {to_couchbeam_key(F), to_couchbeam_value(V)},
              Pair =/= {<<"_rev">>,        undefined}].


If you are willing to build a list and then squish out the
element you don't want, then

to_couchbeam(Fields, Values) ->
     P = fun (K,V) -> {to_couchbeam_key(K), to_couchbeam_value(V)} end,
     lists:delete({<<"_rev">>,undefined}, lists:zipwith(P, Fields,  

seems like the best that can be done.

More information about the erlang-questions mailing list