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-entryxが のままである必要がある場合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 に答える