Clojure

Function to return last n items of a seq

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: Release 1.1
  • Component/s: None
  • Labels:
    None
  • Approval:
    Ok

Description

My fork on github of clojure had a last-n branch that is an unacceptable breaking change by returning a seq even for just a single item. A different solution is needed.

Activity

Hide
Assembla Importer added a comment -

chouser@n01se.net said: [file:cgeCSqBO4r3PxueJe5afGb]: last with one arg is unchanged. last with an extra count arg returns a seq

Show
Assembla Importer added a comment - chouser@n01se.net said: [file:cgeCSqBO4r3PxueJe5afGb]: last with one arg is unchanged. last with an extra count arg returns a seq
Hide
Assembla Importer added a comment -

chouser@n01se.net said: Attached a patch that defined 'last' with one arg as it has been. 'last' with an extra count arg returns a seq.

Show
Assembla Importer added a comment - chouser@n01se.net said: Attached a patch that defined 'last' with one arg as it has been. 'last' with an extra count arg returns a seq.
Hide
Assembla Importer added a comment -

chouser@n01se.net said: [file:cqqgugEomr3ONjeJe5aVNr]: bail early if end of seq found while setting up lead

Show
Assembla Importer added a comment - chouser@n01se.net said: [file:cqqgugEomr3ONjeJe5aVNr]: bail early if end of seq found while setting up lead
Hide
Assembla Importer added a comment -

chouser@n01se.net said: I've attached an improved patch [file:last-n-v2.diff]

Both the old and new patches avoid retaining the head, as demonstrated by the first expr blowing the heap, but second not:

(last 3 (map count (doall (take 1e5 (iterate #(str % 'x) "y")))))
(last 3 (map count (take 1e5 (iterate #(str % 'x) "y"))))t

However, the v2 patch also gives up walking the 'lead' seq forward as soon as it reaches the end of the input seq (this is how 'drop' behaves). You can observe the differnce by seeing that with v2 this returns immediately:

(last 1e10 [1 2 3 4 5])

Show
Assembla Importer added a comment - chouser@n01se.net said: I've attached an improved patch [file:last-n-v2.diff] Both the old and new patches avoid retaining the head, as demonstrated by the first expr blowing the heap, but second not: (last 3 (map count (doall (take 1e5 (iterate #(str % 'x) "y"))))) (last 3 (map count (take 1e5 (iterate #(str % 'x) "y"))))t However, the v2 patch also gives up walking the 'lead' seq forward as soon as it reaches the end of the input seq (this is how 'drop' behaves). You can observe the differnce by seeing that with v2 this returns immediately: (last 1e10 [1 2 3 4 5])
Hide
Assembla Importer added a comment -

chouser@n01se.net said: For small n's and large collections implementing IPersistentStack, 'last' could be much efficient by using 'pop' and 'reverse', though this would mean it wouldn't always use the seq of the collection.

Show
Assembla Importer added a comment - chouser@n01se.net said: For small n's and large collections implementing IPersistentStack, 'last' could be much efficient by using 'pop' and 'reverse', though this would mean it wouldn't always use the seq of the collection.
Hide
Assembla Importer added a comment -

richhickey said: I'm not sure I get the reverse bit - reverse is fast for stacks? Certainly we could do better for colls implementing Indexed or RandomAccess. I guess all that means is that the doc shouldn't dictate linear time, and should instead say maybe in best time complexity supported by the data structure.

Show
Assembla Importer added a comment - richhickey said: I'm not sure I get the reverse bit - reverse is fast for stacks? Certainly we could do better for colls implementing Indexed or RandomAccess. I guess all that means is that the doc shouldn't dictate linear time, and should instead say maybe in best time complexity supported by the data structure.
Hide
Assembla Importer added a comment -

richhickey said: faster for last 1 I should say. Seems more and more like last 1 and last N shouldn't be combined.

Show
Assembla Importer added a comment - richhickey said: faster for last 1 I should say. Seems more and more like last 1 and last N shouldn't be combined.
Hide
Assembla Importer added a comment -

chouser@n01se.net said: [file:b9iKQQUMir3OzreJe5aVNr]: implement take-last as its own function

Show
Assembla Importer added a comment - chouser@n01se.net said: [file:b9iKQQUMir3OzreJe5aVNr]: implement take-last as its own function
Hide
Assembla Importer added a comment -

chouser@n01se.net said: (In [[r:0ba40a260162330836b97f6d1bf3466404a63b55]]) Implement take-last Fixes #151

Branch: master

Show
Assembla Importer added a comment - chouser@n01se.net said: (In [[r:0ba40a260162330836b97f6d1bf3466404a63b55]]) Implement take-last Fixes #151 Branch: master

People

  • Assignee:
    Unassigned
    Reporter:
    Anonymous
Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: