]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: move last2 to the sequences vocab
authorAlexander Ilin <alex.ilin@protonmail.com>
Mon, 19 Feb 2024 21:36:29 +0000 (22:36 +0100)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 20 Feb 2024 16:32:44 +0000 (10:32 -0600)
basis/compiler/tree/propagation/call-effect/call-effect.factor
core/sequences/sequences-docs.factor
core/sequences/sequences.factor
extra/git/git.factor
extra/lint/vocabs/vocabs.factor
extra/project-euler/002/002.factor
extra/ui/gadgets/charts/lines/lines.factor

index de8aeefd6c3c0962698ee00f3db23f5c1d0a763f..f36468b69ee5ddca435561c5224fd1620296855a 100644 (file)
@@ -131,9 +131,6 @@ M: composed add-quot-to-history
 
 M: quotation add-quot-to-history add-to-history ;
 
-: last2 ( seq -- penultimate ultimate )
-    2 tail* first2 ;
-
 : top-two ( #call -- effect value )
     in-d>> last2 [ value-info ] bi@
     literal>> swap ;
index 59f3392eb9b8b30ad622b42036bf2f979a29e10a..24fe03909925f5bcfc8a0bec889c64836f203b65 100644 (file)
@@ -909,6 +909,11 @@ HELP: last
 { $description "Outputs the last element of a sequence." }
 { $errors "Throws an error if the sequence is empty." } ;
 
+HELP: last2
+{ $values { "seq" sequence } { "penultimate" object } { "ultimate" object } }
+{ $description "Outputs the last two elements of a sequence." }
+{ $errors "Throws an error if the sequence has fewer than two elements." } ;
+
 HELP: pop*
 { $values { "seq" "a resizable mutable sequence" } }
 { $description "Removes the last element and shortens the sequence." }
@@ -1325,7 +1330,7 @@ HELP: tail*
 { head head* head-slice head-slice* } related-words
 { cut cut* cut-slice cut-slice* } related-words
 { unclip unclip-slice unclip-last unclip-last-slice } related-words
-{ first last but-last but-last-slice rest rest-slice } related-words
+{ first first2 last last2 but-last but-last-slice rest rest-slice } related-words
 
 HELP: shorter?
 { $values { "seq1" sequence } { "seq2" sequence } { "?" boolean } }
@@ -2051,7 +2056,7 @@ ARTICLE: "sequences-access" "Accessing sequence elements"
 "Extracting the last element:"
 { $subsections last ?last }
 "Unpacking sequences:"
-{ $subsections first2 first3 first4 }
+{ $subsections first2 first3 first4 last2 }
 { $see-also nth } ;
 
 ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
index b4af2c55f130537f6b5a24dc35729a0749aa67f1..7b3478b3724494810a11a87c9081d3df6fff848e 100644 (file)
@@ -843,6 +843,9 @@ PRIVATE>
     index-of-last
     over 0 < [ bounds-error ] [ nth-unsafe ] if ; inline
 
+: last2 ( seq -- penultimate ultimate )
+    2 tail* first2 ; inline
+
 <PRIVATE
 
 : last-unsafe ( seq -- elt )
index d594c760a27ed7e18b245b94578cb728e4b8f198..6a01069d55c80d2cb65d046e0419482c1a1649b1 100644 (file)
@@ -121,8 +121,6 @@ CONSTRUCTOR: <commit> commit ( tree parents author committer -- obj ) ;
 TUPLE: tree hash tree parents author committer gpgsig message ;
 CONSTRUCTOR: <tree> tree ( -- obj ) ;
 
-: last2 ( seq -- penultimate ultimate ) 2 tail* first2 ;
-
 : gmt-offset>duration ( string -- duration )
     3 cut [ string>number ] bi@
     [ hours ] [ minutes ] bi* time+ ;
index f13678935a87a68ba551d704f2c2b18caf026639..8eba730353bfbd1e9b7d97040b620efa431321bb 100644 (file)
@@ -73,8 +73,7 @@ DEFER: next-token
 
 ! Words for removing syntax that should be ignored
 : ends-with-quote? ( token -- ? )
-    2 tail* [ first CHAR: \ = not ] 
-            [ second CHAR: " =    ] bi and ;
+    last2 [ CHAR: \ = not ] [ CHAR: " = ] bi* and ;
 
 : end-string? ( token -- ? )
     dup length 1 = [ quotation-mark? ] [ ends-with-quote? ] if ;
index 86e5b7cf22ac27f5bdc36e2252491ca28728d7fc..d4da76b81d9580965d76659cc8bc4946354b4500 100644 (file)
@@ -24,7 +24,7 @@ IN: project-euler.002
 <PRIVATE
 
 : (fib-upto) ( seq n limit -- seq )
-    2dup <= [ [ suffix! dup 2 tail* sum ] dip (fib-upto) ] [ 2drop ] if ;
+    2dup <= [ [ suffix! dup last2 + ] dip (fib-upto) ] [ 2drop ] if ;
 
 PRIVATE>
 
index 33823e9aa74d6d5eaea165d6bffb7809110bbf1f..2fca644ac1e21486c72e8523c58585d45b5ecea3 100644 (file)
@@ -68,7 +68,6 @@ ALIAS: y second
 : calc-y ( slope x point -- y ) first2 [ - * ] dip + ;
 : calc-x ( slope y point -- x ) first2 swap [ - swap / ] dip + ;
 : y-at ( x point1 point2 -- y ) dupd calc-line-slope -rot calc-y ;
-: last2 ( seq -- penultimate ultimate ) 2 tail* first2 ;
 
 ! Due to the way adjusted-tail-slice works, the first element of
 ! pairs is <= xmin, and if the first is < xmin, then the second is