<< Back to previous view

[CLJ-925] rand-nth throws on empty list Created: 05/Feb/12  Updated: 13/Jun/16  Resolved: 14/Aug/12

Status: Closed
Project: Clojure
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Minor
Reporter: Jozef Wagner Assignee: Unassigned
Resolution: Completed Votes: 0
Labels: None

Attachments: File patch.diff    
Patch: Code


rand-nth throws when given an empty list.

Solution is to either call seq on parameter, or to supply nil as a not-found parameter in call to nth. Patch fir latter fix included.

Comment by Andy Fingerhut [ 23/Mar/12 2:38 AM ]

Changing Patch attribute to value "Code", since I've been told that is what it ought to be for a ticket with a patch ready for screening.

Comment by Michael Nygard [ 22/Jul/15 5:59 PM ]

This is marked as "Completed," but the reported behavior still exists in 1.7.0. Was the fix rejected?

Comment by Andy Fingerhut [ 22/Jul/15 6:18 PM ]

From looking at revision history, it seems that rand-nth has never been changed since it was added. Perhaps no fix was ever committed for it, maybe by accident.

Comment by Alex Miller [ 13/Jun/16 10:25 PM ]

Since I just happened to run across this and future finders... Rich closed this (should have been declined really) because this is working as designed. Returning a random nth element requires there to be at least one element. Returning nil would mean returning a value that is not part of the original collection, which violates the contract.

Perhaps we'll soon have a spec for this that checks #(not (empty? %)).

Generated at Wed Sep 20 02:40:27 CDT 2017 using JIRA 4.4#649-r158309.