views:

91

answers:

3

How can I delete a tuple with key from a list?

Ex:

TupleList = [ {apple, {0,0,0}}, {orange, {0,0,0}}, {bannana, {0,0,0}}]

Then I need to delete the tuple whos key matches orange.

So I should get back

[ {apple, {0,0,0}},  {bannana, {0,0,0}}]

Im looking for a BIF instead of a function as I am using right now.

Thanks and Regards

A: 

solution accomplished:

[NewHold] = [{N, X} || {N, X} <- Hold, N =/= Who],
This is not BIF.
Hynek -Pichi- Vychodil
`NewList = [T || {K, _} = T <- List, K =/= DelKey].` might give better performance, as it does not rebuild the tuple.
Zed
+1  A: 
proplists:delete(orange, TupleList).
scatterbrain
This is not BIF.
Hynek -Pichi- Vychodil
+2  A: 

There is not BIF for this. There is lists:keydelete/3 which is not BIF in contrast of lists:member/2, lists:reverse/2, lists:keymember/3, lists:keysearch/3 and lists:keyfind/3 which are BIFs. Anyway lists:keydelete/3 performs better than proplists:delete/2 because proplists are little bit more tricky. If you would be interested in performance, your own crafted local function or probably nif would be better solution as well as list comprehension (which should perform same as local function).

Hynek -Pichi- Vychodil
Agreeing with this reply. I'd also consider choosing a different representation for the data structure, depending on how many elements it contains, rather than a BIF: ordering the tuples in the first place could help, or maybe a tree representation.
I GIVE TERRIBLE ADVICE
Using NIF is probably the most complex way of doing it. Use either one of the builtin modules, dict, gb_trees, ets (or orddict if you really want it as a list) or design your own special structure. Depends on what interface you want.
rvirding
Why is it that my function is worse than what you wrote? [NewHold] = [{N, X} || {N, X} <- Hold, N =/= Who], Or is it the same thing?
@user417896: Aside that it should be `NewHold = [{N, X} || {N, X} <- Hold, N =/= Who]` it simply is not BIF and BIF is what you asked for. This lists comprehension is equivalent of non tail recursive local function `delete([], _)->[]; delete([{N, X}|T], Who) when N=/=Who -> [{N,X}|delete(T,Who)]; delete([_|T], Who) -> delete(T, Who).` Version `delete([],_)->[];delete([{Who,_}=L|T], Who)->[L|delete(T,Who)];delete([_|T], Who)->delete(T, Who).` would be faster but negligible. But anyway there is not BIF for it.
Hynek -Pichi- Vychodil