> Erlang中文手册 > substitute_negations/2 替换列表里的键并对值取反

proplists:substitute_negations/2

替换列表里的键并对值取反

用法:

substitute_negations(Negations, List) -> List

内部实现:

-spec substitute_negations(Negations, ListIn) -> ListOut when
      Negations :: [{Key, Key}],
      Key :: term(),
      ListIn :: [term()],
      ListOut :: [term()].

substitute_negations(As, Props) ->
    [substitute_negations_1(As, P) || P 
    if is_atom(P), P =:= Key ->
	    property(Key1, false);
       tuple_size(P) >= 1, element(1, P) =:= Key ->
	    case P of
		{_, true} ->
		    property(Key1, false);
		{_, false} ->
		    property(Key1, true);
		_ ->
		    %% The property is supposed to be a boolean, so any
		    %% other tuple is interpreted as 'false', as done in
		    %% 'get_bool'.
		    property(Key1, true)
	    end;		    
       true ->
	    substitute_negations_1(As, P)
    end;
substitute_negations_1([], P) ->
    P.

替换列表里的键并对值取反。列表里的每一个条目,如果参数 Negations 是由 {K1, K2} 这种相关联的形式组成的列表,那么如果条目是 {K1, true} 的话,它将会被替换为 {K2, false},否则,它将会被替换为 {K2, true},即改变项的名字并对值的布尔属性(可由 proplists:get_bool/2 获得)取反。如果同样的 K1 不止一次出现在参数 Negations,那么只有只有第一个会被使用。

proplists:substitute_negations([{k1, a1}], [{k1, 1}, {k1, false}, {k2, 2}, {k1, true}, k1, {k4, 4}]).
proplists:substitute_negations([{k1, a1}, {k2, a2}], [{k1, 1}, {k1, false}, {k2, 2}, {k1, true}, k1, {k4, 4}]).