]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extra: Fix min-by and max-by.
authorDoug Coleman <doug.coleman@gmail.com>
Mon, 7 May 2012 22:02:44 +0000 (15:02 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Mon, 7 May 2012 22:04:37 +0000 (15:04 -0700)
maximum and minimum run 2x as many calls to the quotation as are actually necessary; fix this eventually.

extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index dfee932f388b1c5272b41e754f514cbb50874276..564e2d2ea0c7446e55785af3080d121d7c315b63 100644 (file)
@@ -4,7 +4,13 @@ tools.test ;
 IN: sequences.extras.tests
 
 [ 1 ] [ 1 2 [ ] min-by ] unit-test
+[ 1 ] [ 2 1 [ ] min-by ] unit-test
+[ 42.0 ] [ 42.0 1/0. [ ] min-by ] unit-test
+[ 42.0 ] [ 1/0. 42.0 [ ] min-by ] unit-test
 [ 2 ] [ 1 2 [ ] max-by ] unit-test
+[ 2 ] [ 2 1 [ ] max-by ] unit-test
+[ 1/0. ] [ 42.0 1/0. [ ] max-by ] unit-test
+[ 1/0. ] [ 1/0. 42.0 [ ] max-by ] unit-test
 [ "12345" ] [ "123" "12345" [ length ] max-by ] unit-test
 [ "123" ] [ "123" "12345" [ length ] min-by ] unit-test
 
index 8f20fcfc673b2ea03a7afcec76916804a1854468..31e16c9c9d8f36f241e62f695589ae35c651c6fa 100644 (file)
@@ -28,10 +28,10 @@ IN: sequences.extras
     2dup [ < ] with find drop over length or swap insert-nth ;
 
 : max-by ( obj1 obj2 quot: ( obj -- n ) -- obj1/obj2 )
-    [ bi@ [ max ] keep eq? not ] curry most ; inline
+    [ bi@ dupd max = ] curry most ; inline
 
 : min-by ( obj1 obj2 quot: ( obj -- n ) -- obj1/obj2 )
-    [ bi@ [ min ] keep eq? not ] curry most ; inline
+    [ bi@ dupd min = ] curry most ; inline
 
 : maximum ( seq quot: ( ... elt -- ... x ) -- elt )
     [ dup ?first ] dip [ max-by ] curry reduce ; inline