如何使用引号的部分应用程序重构代码



如何使用现有的组合子来重构此代码,使regex成为要部分应用的参数,并且生成的引号将具有与ls (x -- )相同的堆栈效果?

USING: io.directories locals sequences accessors math 
  prettyprint kernel io.files.info io.directories.hierarchy 
  combinators.short-circuit regexp
  ;
IN: flac
:: job ( step path -- ) 
     path 
     [ [ step call ] each ]
     with-directory-entries
     ; inline
:: lsc ( x c -- ) x c call [ x . ] when ; inline
:: ls ( x -- )
     x 
     [ { 
         [ directory? ] 
         [ name>> directory-tree-files 
           [ ".*[.]flac" <regexp> matches? ] 
           filter length 0 = 
         ]
       } 
       1&&
     ] 
     lsc
     ;

首先,在原始代码中,看起来xdirectory-entry。如果x需要保持为directory-entry,那么就不可能将正则表达式重构为参数,毕竟没有地方放它!如果允许x更改为,比如嵌入正则表达式的字符串,或者集合或对象,则可以将正则表达式作为单个参数x的一部分。

以下解决方案假设x可以更改为一个元组对象,其中"dir entry"one_answers"regex"作为槽:

:: ls ( x -- )
    x
    [ dir-entry>> directory? ].
    [ 
        dir-entry>> name>> directory-tree-files 
        [ [ x regex>> <regexp> matches? ] any? ] [  drop x dir-entry>> ] when .
    ] smart-when* ;

相关内容

最新更新