From fe4be254dad24419cdcef41efaeac79b327c23a9 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sat, 19 May 2012 11:26:01 -0700 Subject: [PATCH] sequences.extras: move min-by/max-by to math.compare. --- extra/math/compare/compare-tests.factor | 15 ++++++++++++++- extra/math/compare/compare.factor | 8 ++++++++ extra/sequences/extras/extras-tests.factor | 19 ++++--------------- extra/sequences/extras/extras.factor | 12 ------------ 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/extra/math/compare/compare-tests.factor b/extra/math/compare/compare-tests.factor index 5b30af0e63..cedea4984c 100644 --- a/extra/math/compare/compare-tests.factor +++ b/extra/math/compare/compare-tests.factor @@ -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 [ -1 ] [ -1 5 absmin ] unit-test @@ -14,3 +16,14 @@ IN: math.compare.tests [ 0 ] [ 1 3 negmin ] unit-test [ -3 ] [ 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 diff --git a/extra/math/compare/compare.factor b/extra/math/compare/compare.factor index f7f66da37c..08c25e4a34 100644 --- a/extra/math/compare/compare.factor +++ b/extra/math/compare/compare.factor @@ -14,3 +14,11 @@ IN: math.compare : negmin ( a b -- x ) 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 + + diff --git a/extra/sequences/extras/extras-tests.factor b/extra/sequences/extras/extras-tests.factor index 49b3a7e26d..e33819e580 100644 --- a/extra/sequences/extras/extras-tests.factor +++ b/extra/sequences/extras/extras-tests.factor @@ -3,21 +3,10 @@ sequences.extras strings 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 - -[ 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 +[ 4 ] [ 5 iota [ ] supremum-by ] unit-test +[ 0 ] [ 5 iota [ ] infimum-by ] unit-test +[ { "foo" } ] [ { { "foo" } { "bar" } } [ first ] supremum-by ] unit-test +[ { "bar" } ] [ { { "foo" } { "bar" } } [ first ] infimum-by ] unit-test { V{ 0 1 2 3 4 5 6 7 8 9 } } [ V{ } clone diff --git a/extra/sequences/extras/extras.factor b/extra/sequences/extras/extras.factor index 07ca0c3e22..b565894fc1 100644 --- a/extra/sequences/extras/extras.factor +++ b/extra/sequences/extras/extras.factor @@ -33,18 +33,6 @@ IN: sequences.extras : reduce-from ( ... seq identity quot: ( ... prev elt -- ... next ) i -- ... result ) [ 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 ) [ [ first dup ] dip call ] 2keep [ dupd call pick dupd max over =