2

如何使用现有的组合器重构此代码,以便正则表达式将成为部分应用的参数,并且生成的引用将具有与相同的堆栈效果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
     ;
4

1 回答 1

2

首先,在原始代码中,它看起来像x一个directory-entry. 如果x要求保留为 a directory-entry,则无法将正则表达式重构为参数,毕竟无处可放!如果x允许更改为嵌入正则表达式的字符串,或者是集合或对象,则可以将正则表达式作为单个参数的一部分 -- x

以下解决方案假设x可以更改为以“dir-entry”和“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* ;
于 2015-04-29T22:28:58.100 回答