kth-object-impl ( seq k nth-quot exchange-quot quot: ( x y -- ? ) -- elt )


Vocabulary
math.statistics.private

Inputs
seqan object
kan object
nth-quotan object
exchange-quotan object
quota quotation with stack effect ( x y -- ? )


Outputs
eltan object


Definition


:: kth-object-impl
( seq k nth-quot exchange-quot quot: ( x y -- ? ) -- elt )
k seq bounds-check 2drop
0 :> i! 0 :> j! 0 :> l! 0 :> x! seq length 1 - :> m!
[ l m < ] [
k seq nth-unsafe x! l i! m j!
[ i j <= ] [
[ i seq nth-quot call x quot call ] [ i 1 + i! ]
while
[ x j seq nth-quot call quot call ] [ j 1 - j! ]
while i j <=
[ i j seq exchange-quot call i 1 + i! j 1 - j! ]
when
] do while j k < [ i l! ] when k i < [ j m! ] when
] while k seq nth-unsafe ; inline