Details

Type: Enhancement

Status: Resolved

Priority: Trivial

Resolution: Completed

Affects Version/s: None

Fix Version/s: None

Component/s: None

Labels:

Patch:Code

Approval:Accepted
Description
Hi,
Came upon this as I was trying to improve performance. The implementation of sortednumbers? (only used by permutations) is:
(defn sortednumbers?
"Returns true iff s is a sequence of numbers in nondecreasing order"
[s]
(and (every? number? s)
(every? (partial apply <=) (partition 2 1 s))))
<= and similar operators are variadic, so partitioning into pairs is unneeded.
(apply <= s) is a lot faster, but breaks for empty sequences, so an additional check is needed.
The implementation can be changed to:
(and (every? number? s)
(or (empty? s) (apply <= s)))
I benched this to be 10 to 15 times faster. A regression test with test.check was done to verify that the behaviour was not changed under any input.
A patch is also included.
Cheers,
Daniel
Thanks for the submission. I don't know whether Mark would be interested in taking the test.check regression test you developed, too, but if you would be willing to share it as a patch, it might be considered for inclusion as well.