From 7baf6aa5964c5b3fa3f332a0908fdd15323e304c Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Tue, 19 Dec 2023 04:50:01 +0000 Subject: [PATCH] prelude: add lextract --- prelude.tcl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/prelude.tcl b/prelude.tcl index 9028684..64c41e2 100644 --- a/prelude.tcl +++ b/prelude.tcl @@ -56,3 +56,25 @@ proc transpose strings { } return $out } + +# extracts one or more indexes from a strided list +# e.g. +# % set l {0 1 2 a b c x y z} +# % lextract $l 3 0 +# 0 a x +# % lextract $l 3 {1 2} +# 1 2 b c y z +# +# equivalent to [lmap {a b c} $lst {list $b $c}] except that +# you don't have to name the list elements +proc lextract {lst stride index} { + set i 0 + set out {} + if {$stride <= 0} { error "stride must be positive: $stride" } + for {set i 0} {$i < [llength $lst]} {incr i $stride} { + foreach j $index { + lappend out [lindex $lst $i+$j] + } + } + return $out +}