sequences.extras: move min-by/max-by to math.compare.
							parent
							
								
									667ebe942a
								
							
						
					
					
						commit
						fe4be254da
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 =
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue