Paste: trim-keep

Author: hsuh
Mode: factor
Date: Thu, 29 Oct 2009 15:39:00
Plain Text |
! I needed to know what trim removed on each side on the sequence; can this be better written?

: find-idx ( seq quot -- idx ) find drop ; inline
: find-last-idx ( seq quot -- idx ) find-last drop ; inline

: trim-idx ( seq quot -- idx-head idx-last )
    [ not ] compose ! Negate quot
    [ find-idx ]
    [ find-last-idx ] 2bi ; inline

:: (trim-keep) ( seq a b -- middle before after )
    a b 1 + seq subseq 
    seq a head
    seq b 1 + tail ;

: trim-keep ( seq quot -- middle before after )
    dupd trim-idx (trim-keep) ; inline

Annotation: trim-keep

Author: blei
Mode: factor
Date: Thu, 29 Oct 2009 21:09:16
Plain Text |
: cut-slice-head ( seq quot -- before after )
    dupd [ not ] compose find drop over length or cut-slice ;
    inline

: cut-slice-tail ( seq quot -- before after )
    dupd [ not ] compose find-last drop 0 or 1 + cut-slice ;
    inline

: trim-keep-slice ( seq quot -- middle before after )
    [ cut-slice-head ] keep cut-slice-tail swapd ; inline

: trim-keep ( seq quot -- middle before after )
    over [ trim-keep-slice ] dip [ like ] curry tri@ ; inline

New Annotation

Summary:
Author:
Mode:
Body: