sequences.extras: move min-by/max-by to math.compare.

db4
John Benediktsson 2012-05-19 11:26:01 -07:00
parent 667ebe942a
commit fe4be254da
4 changed files with 26 additions and 28 deletions

View File

@ -1,4 +1,6 @@
USING: kernel math math.compare math.functions tools.test ; USING: kernel math math.compare math.functions sequences
tools.test ;
IN: math.compare.tests IN: math.compare.tests
[ -1 ] [ -1 5 absmin ] unit-test [ -1 ] [ -1 5 absmin ] unit-test
@ -14,3 +16,14 @@ IN: math.compare.tests
[ 0 ] [ 1 3 negmin ] unit-test [ 0 ] [ 1 3 negmin ] unit-test
[ -3 ] [ 1 -3 negmin ] unit-test [ -3 ] [ 1 -3 negmin ] unit-test
[ -1 ] [ -1 3 negmin ] unit-test [ -1 ] [ -1 3 negmin ] unit-test
[ 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

@ -14,3 +14,11 @@ IN: math.compare
: negmin ( a b -- x ) : negmin ( a b -- x )
0 min min ; 0 min min ;
: max-by ( obj1 obj2 quot: ( obj -- n ) -- obj1/obj2 )
[ bi@ dupd max = ] curry most ; inline
: min-by ( obj1 obj2 quot: ( obj -- n ) -- obj1/obj2 )
[ bi@ dupd min = ] curry most ; inline

View File

@ -3,21 +3,10 @@ sequences.extras strings tools.test ;
IN: sequences.extras.tests IN: sequences.extras.tests
[ 1 ] [ 1 2 [ ] min-by ] unit-test [ 4 ] [ 5 iota [ ] supremum-by ] unit-test
[ 1 ] [ 2 1 [ ] min-by ] unit-test [ 0 ] [ 5 iota [ ] infimum-by ] unit-test
[ 42.0 ] [ 42.0 1/0. [ ] min-by ] unit-test [ { "foo" } ] [ { { "foo" } { "bar" } } [ first ] supremum-by ] unit-test
[ 42.0 ] [ 1/0. 42.0 [ ] min-by ] unit-test [ { "bar" } ] [ { { "foo" } { "bar" } } [ first ] infimum-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
[ 4 ] [ 5 iota [ ] maximum ] unit-test
[ 0 ] [ 5 iota [ ] minimum ] unit-test
[ { "foo" } ] [ { { "foo" } { "bar" } } [ first ] maximum ] unit-test
[ { "bar" } ] [ { { "foo" } { "bar" } } [ first ] minimum ] unit-test
{ V{ 0 1 2 3 4 5 6 7 8 9 } } [ { V{ 0 1 2 3 4 5 6 7 8 9 } } [
V{ } clone V{ } clone

View File

@ -33,18 +33,6 @@ IN: sequences.extras
: reduce-from ( ... seq identity quot: ( ... prev elt -- ... next ) i -- ... result ) : reduce-from ( ... seq identity quot: ( ... prev elt -- ... next ) i -- ... result )
[ swap ] 2dip each-from ; inline [ swap ] 2dip each-from ; inline
: max-by ( obj1 obj2 quot: ( obj -- n ) -- obj1/obj2 )
[ bi@ dupd max = ] curry most ; inline
: min-by ( obj1 obj2 quot: ( obj -- n ) -- obj1/obj2 )
[ bi@ dupd min = ] curry most ; inline
: maximum ( seq quot: ( ... elt -- ... x ) -- elt )
[ dup ?first ] dip [ max-by ] curry reduce ; inline
: minimum ( seq quot: ( ... elt -- ... x ) -- elt )
[ dup ?first ] dip [ min-by ] curry reduce ; inline
: supremum-by ( seq quot: ( ... elt -- ... x ) -- elt ) : supremum-by ( seq quot: ( ... elt -- ... x ) -- elt )
[ [ first dup ] dip call ] 2keep [ [ [ first dup ] dip call ] 2keep [
dupd call pick dupd max over = dupd call pick dupd max over =