sequences.extra: Fix min-by and max-by.

maximum and minimum run 2x as many calls to the quotation as are actually necessary; fix this eventually.
db4
Doug Coleman 2012-05-07 15:02:44 -07:00
parent 0d346a0268
commit f03a7f10bf
2 changed files with 8 additions and 2 deletions

View 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

View 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