]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: making supremum/infimum generic
authorJohn Benediktsson <mrjbq7@gmail.com>
Sat, 9 Sep 2023 20:36:39 +0000 (13:36 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 9 Sep 2023 20:36:39 +0000 (13:36 -0700)
core/ranges/ranges.factor
core/sequences/sequences.factor

index f5a4608436bcd65cc08b841816a6c5ced27ca63b..2ac7e4b9647251edff13632409067a01b1bf9810 100644 (file)
@@ -37,6 +37,12 @@ M: range sum
     [ drop 0 ]
     [ swap [ first-unsafe ] [ last-unsafe ] bi + * 2 / ] if-zero ;
 
+M: range infimum
+    dup step>> 0 > [ first ] [ last ] if ;
+
+M: range supremum
+    dup step>> 0 > [ last ] [ first ] if ;
+
 <PRIVATE
 
 : twiddle ( a b -- a b step ) 2dup > -1 1 ? ; inline
index f69d2c708f99b22ec7e552de92d7977751cec93c..737f9a85d673cc5cc314b098c451575b50209c2f 100644 (file)
@@ -1139,9 +1139,15 @@ M: repetition sum [ elt>> ] [ length>> ] bi * ; inline
 
 : product ( seq -- n ) 1 [ * ] binary-reduce ;
 
-: infimum ( seq -- elt ) [ ] [ min ] map-reduce ;
-
-: supremum ( seq -- elt ) [ ] [ max ] map-reduce ;
+GENERIC: infimum ( seq -- elt )
+M: object infimum [ ] [ min ] map-reduce ;
+M: iota infimum first ;
+M: reversed infimum seq>> infimum ;
+
+GENERIC: supremum ( seq -- elt )
+M: object supremum [ ] [ max ] map-reduce ;
+M: iota supremum last ;
+M: reversed supremum seq>> supremum ;
 
 : map-sum ( ... seq quot: ( ... elt -- ... n ) -- ... n )
     [ 0 ] 2dip [ dip + ] curry [ swap ] prepose each ; inline