prelude: add lextract

main
magical 2023-12-19 04:50:01 +00:00
parent 89dcdeb553
commit 7baf6aa596
1 changed files with 22 additions and 0 deletions

View File

@ -56,3 +56,25 @@ proc transpose strings {
} }
return $out 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
}