Skip to content

Commit

Permalink
filter distances before sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
poke1024 committed Sep 18, 2016
1 parent e0d6226 commit ee2e423
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mathics/builtin/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -4209,15 +4209,16 @@ def nearest(x):

py_distances = [(_to_real_distance(d), i) for i, d in enumerate(distances.leaves)]

if py_r is not None:
py_distances = [(d, i) for d, i in py_distances if d <= py_r]

def pick():
if py_n is None:
candidates = sorted(py_distances)
else:
candidates = heapq.nsmallest(py_n, py_distances)

for d, i in candidates:
if py_r is not None and d > py_r:
break
yield repr_p[i]

return Expression('List', *list(pick()))
Expand Down

0 comments on commit ee2e423

Please sign in to comment.