Skip to content

Commit bf35649

Browse files
jmitchellJosé Valim
authored andcommitted
Use faster HashDict.drop/2, ListDict.take/2 and ListDict.drop/2 implementations
1 parent 08211b7 commit bf35649

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

lib/elixir/lib/hash_dict.ex

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ defmodule HashDict do
287287
end
288288

289289
@doc """
290-
Splits a dict into two dicts,
291-
one containing entries with key in the keys list,
290+
Splits a dict into two dicts,
291+
one containing entries with key in the keys list,
292292
and another containing entries with key not in keys.
293293
Returns a 2-tuple of the new dicts.
294294
"""
@@ -301,7 +301,7 @@ defmodule HashDict do
301301
dict_fold dict, acc, fn({ k, v }, { take, drop }) ->
302302
if Enum.member?(keys, k) do
303303
{ put(take, k, v), drop }
304-
else
304+
else
305305
{ take, put(drop, k, v) }
306306
end
307307
end
@@ -321,16 +321,13 @@ defmodule HashDict do
321321
end
322322

323323
@doc """
324-
Returns a new dict with only the entries
324+
Returns a new dict with only the entries
325325
which key is not in keys
326326
"""
327-
def drop(dict, keys) when keys == [] do
328-
dict
329-
end
327+
def drop(dict, []), do: dict
330328

331-
def drop(dict, keys) do
332-
{ _, non_members } = split(dict, keys)
333-
non_members
329+
def drop(dict, [key|keys]) do
330+
drop(delete(dict, key), keys)
334331
end
335332

336333
## Dict-wide functions

lib/elixir/lib/list_dict.ex

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ defmodule ListDict do
141141
end
142142

143143
@doc """
144-
Splits a dict into two dicts,
145-
one containing entries with key in the keys list,
144+
Splits a dict into two dicts,
145+
one containing entries with key in the keys list,
146146
and another containing entries with key not in keys.
147147
Returns a 2-tuple of the new dicts.
148148
"""
@@ -155,7 +155,7 @@ defmodule ListDict do
155155
Enum.reduce dict, acc, fn({ k, v }, { take, drop }) ->
156156
if Enum.member?(keys, k) do
157157
{ [{k,v}|take], drop }
158-
else
158+
else
159159
{ take, [{k,v}|drop] }
160160
end
161161
end
@@ -165,26 +165,16 @@ defmodule ListDict do
165165
Returns a new dict with only the entries
166166
which key is in keys
167167
"""
168-
def take(_, keys) when keys == [] do
169-
new()
170-
end
171-
172168
def take(dict, keys) do
173-
{ members, _ } = split(dict, keys)
174-
members
169+
lc { k, _ } = tuple inlist dict, :lists.member(k, keys), do: tuple
175170
end
176171

177172
@doc """
178-
Returns a new dict with only the entries
173+
Returns a new dict with only the entries
179174
which key is not in keys
180175
"""
181-
def drop(dict, keys) when keys == [] do
182-
dict
183-
end
184-
185176
def drop(dict, keys) do
186-
{ _, non_members } = split(dict, keys)
187-
non_members
177+
lc { k, _ } = tuple inlist dict, not :lists.member(k, keys), do: tuple
188178
end
189179

190180
@doc """

0 commit comments

Comments
 (0)