Paste: chop

Author: Arrogant
Mode: factor
Date: Wed, 1 Sep 2010 07:19:11
Plain Text |
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
  ;

New Annotation

Summary:
Author:
Mode:
Body: