missing lists functions

Tony Rogvall tony@REDACTED
Sat Mar 18 11:09:01 CET 2006


Hi list!

I am missing some lists function.

lists:keysearch_and_delete(Key,Pos,List)

	Like keysearch but also deletes the element if found.
	return
		false				- when not found
		{value,Value,List'}	- when found the List' is the new list without  
the element

lists:replace_or_insert(Key, Pos, List, New)

	Like keyreplace but also add the element to the end of list if
	element is not found.

	return List' where item is item replace or added to the list (head  
or tail).

(possibly a sorted version of replace_or_insert could be nice)

If this is common enough perhaps some library addition could be done ?


I tend to write code that looking like this:

Delete case:
	case lists:keysearch(Key, Pos, List) of
		false ->
			recurse(State);
		{value,{_,Value}} ->
			List1 = lists:keydelete(Key, Pos, List),
			recurse(State#state { list = List1})
	end.

"Reduce to"

	case lists:keysearch_and_delete(Key,Pos,List) of
		false ->
			recurse(State);
		{value,{_,Value}, List1} ->
			recurse(State#state { list = List1 }
	end.
	

Update case:
	case lists:keysearch(Key, Pos, List) of
		false ->	
			List1 = [{Key,NewValue} | List],
			recurse(State#state { list = List1 });
		{value,{_,Value}} ->
			Do some thing
			List1 = lists:keyreplace(Key, Pos, List, {Key,NewValue}),
			recurse(State#state { list = List1})
	end.

Reduce to

	List1 = lists:keysearch_or_insert(Key, Pos, List, {Key,NewValue}),
	recurse(State#state { list = List1}).

/Tony






	



More information about the erlang-questions mailing list