USING: locals kernel sequences math math.functions math.order accessors combinators ; IN: binary-chop : bisect-slice ( seq -- slice slice ) dup length 2 /i cut-slice ; DEFER: chop :: check-right ( data query -- int ) data [ f ] [ dup first query <=> { { +gt+ [ drop f ] } { +lt+ [ rest-slice query chop ] } [ drop from>> ] } case ] if-empty ; : chop ( seq int -- int ) [ bisect-slice ] dip [ check-right ] keep swap dup [ 2nip ] [ drop swap [ drop f ] [ swap chop ] if-empty ] if ;