Memoize

BasicWerk   EC Support   Technique   Facebook  

20140613111929_factor_regexp_fry_tri

factor_regexp_fry_tri

 

以前作った re-replace-first をカリー化(fry)と tri を使って分かりやすくしてみた。

 

最初のバージョン

http://basicwerk.com/memoize/20140610123934_factor_regexp.html

 

SYMBOL を使ったバージョン

http://basicwerk.com/memoize/20140611161945_factor_SYMBOLS.html

 

3 番目の NEW が今回のバージョンである。

 
! Copyright (C) 2014 Shin Nakamura.
! See http://factorcode.org/license.txn for BSD license.
USING:  kernel locals math 
        regexp regexp.private 
        sequences.private 
        accessors sequences 
        lexer parser words words.symbol
        namespaces fry ;
IN: my-regexp
 
! OLD
: re-replace-first% ( string regexp replacement -- result )
    rot rot over rot rot first-match
    dup tuple?
    [
        dup from>>
        over to>>
        rot drop
        rot
        replace-slice
    ]
    [   
        drop
        nip
    ] if
;
 
! OLD2
: re-replace-first%% ( string regexp replacement -- result )
    SYMBOLS: str re rep ;
    rep set re set str set
    str get re get first-match
    dup tuple?
    [
        rep get swap
        dup from>>
        swap to>>
        str get
        replace-slice
    ]
    [   
        drop
        str get
    ] if
;
 
! NEW
: re-replace-first ( string regexp replacement -- result )
    '[ _ dup _ _ ] call ! dup str, for "else"
    ! -> str str re rep
    '[ _ _ first-match _ ] call
    ! -> str Tuple|f rep
    '[ _ dup tuple? _ swap ] call
    ! -> str Tuple|f rep bool
    ! "if" eats bool
    [
        swap ! -> str rep Tuple
        [ from>> ] [ to>> ] [ seq>> ] tri
        ! -> str rep(new) from to str(seq)
        replace-slice
        ! -> str result
        nip
        ! -> result
    ]
    [   
        ! str f rep
        2drop
        ! -> str
    ] if
;
 

 


© Shin Nakamura/BasicWerk 2014