[erlang-questions] Re: The If expression

James Aimonetti james.aimonetti@REDACTED
Fri Apr 23 01:25:16 CEST 2010


Thanks Richard, the feedback is much appreciated. I have a couple
different places where your multi-generator syntax would indeed be
useful.

James

On Thu, Apr 22, 2010 at 6:14 PM, Richard O'Keefe <ok@REDACTED> wrote:
>
> 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]
>    end.
>
> 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
> (http://www.erlang.org/eeps/eep-0019.html)
>
> to_couchbeam(Fields, Values) ->
>    [Pair || F <- Fields && V <- Values,
>             Pair = {to_couchbeam_key(F), to_couchbeam_value(V)},
>             Pair =/= {<<"_rev">>,        undefined}].
>
> (THAT IS NOT LEGAL ERLANG RIGHT NOW.)
>
> 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, Values)).
>
> seems like the best that can be done.
>
>
>


More information about the erlang-questions mailing list