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 ;
{ $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." }
{ 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 } }
"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-of-last
over 0 < [ bounds-error ] [ nth-unsafe ] if ; inline
+: last2 ( seq -- penultimate ultimate )
+ 2 tail* first2 ; inline
+
<PRIVATE
: last-unsafe ( seq -- elt )
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+ ;
! 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 ;
<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>
: 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