prelude: add lextract
parent
89dcdeb553
commit
7baf6aa596
22
prelude.tcl
22
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue