From 4ef0a49c96ba21a2b88d4e49119825b8801e9a06 Mon Sep 17 00:00:00 2001 From: Alexander Ilin Date: Mon, 19 Feb 2024 22:36:29 +0100 Subject: [PATCH] sequences: move last2 to the sequences vocab --- .../tree/propagation/call-effect/call-effect.factor | 3 --- core/sequences/sequences-docs.factor | 9 +++++++-- core/sequences/sequences.factor | 3 +++ extra/git/git.factor | 2 -- extra/lint/vocabs/vocabs.factor | 3 +-- extra/project-euler/002/002.factor | 2 +- extra/ui/gadgets/charts/lines/lines.factor | 1 - 7 files changed, 12 insertions(+), 11 deletions(-) diff --git a/basis/compiler/tree/propagation/call-effect/call-effect.factor b/basis/compiler/tree/propagation/call-effect/call-effect.factor index de8aeefd6c..f36468b69e 100644 --- a/basis/compiler/tree/propagation/call-effect/call-effect.factor +++ b/basis/compiler/tree/propagation/call-effect/call-effect.factor @@ -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 ; diff --git a/core/sequences/sequences-docs.factor b/core/sequences/sequences-docs.factor index 59f3392eb9..24fe039099 100644 --- a/core/sequences/sequences-docs.factor +++ b/core/sequences/sequences-docs.factor @@ -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" diff --git a/core/sequences/sequences.factor b/core/sequences/sequences.factor index b4af2c55f1..7b3478b372 100644 --- a/core/sequences/sequences.factor +++ b/core/sequences/sequences.factor @@ -843,6 +843,9 @@ PRIVATE> index-of-last over 0 < [ bounds-error ] [ nth-unsafe ] if ; inline +: last2 ( seq -- penultimate ultimate ) + 2 tail* first2 ; inline + commit ( tree parents author committer -- obj ) ; TUPLE: tree hash tree parents author committer gpgsig message ; CONSTRUCTOR: tree ( -- obj ) ; -: last2 ( seq -- penultimate ultimate ) 2 tail* first2 ; - : gmt-offset>duration ( string -- duration ) 3 cut [ string>number ] bi@ [ hours ] [ minutes ] bi* time+ ; diff --git a/extra/lint/vocabs/vocabs.factor b/extra/lint/vocabs/vocabs.factor index f13678935a..8eba730353 100644 --- a/extra/lint/vocabs/vocabs.factor +++ b/extra/lint/vocabs/vocabs.factor @@ -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 ; diff --git a/extra/project-euler/002/002.factor b/extra/project-euler/002/002.factor index 86e5b7cf22..d4da76b81d 100644 --- a/extra/project-euler/002/002.factor +++ b/extra/project-euler/002/002.factor @@ -24,7 +24,7 @@ IN: project-euler.002 diff --git a/extra/ui/gadgets/charts/lines/lines.factor b/extra/ui/gadgets/charts/lines/lines.factor index 33823e9aa7..2fca644ac1 100644 --- a/extra/ui/gadgets/charts/lines/lines.factor +++ b/extra/ui/gadgets/charts/lines/lines.factor @@ -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 -- 2.34.1