dirif
function dirif( path, f ) --> iter
Description
Creates a Lua iterator over relevant entries in a directory. The relevance will be determined with the function f. Raises an error if path is not a directory.
Parameters
- path
-
A path to directory in the filesystem.
- f
-
Function to identify the relevant entries. Must take one parameter and return true or false.
Return Values
- iter
-
Lua iterator over the relevant entries.
Code
local lfs = require( "lfs" ) --ZREQ-lfs --ZFUNC-dirif-v1 local function dirif( path, f ) --> iter local function yieldentry( path ) for entry in lfs.dir( path ) do if f( entry ) then coroutine.yield( entry ) end end end return coroutine.wrap( function() yieldentry( path ) end ) end return dirif
Examples
local t = require( "taptest" ) local collectk = require( "collectk" ) local dirif = require( "dirif" ) local isdodd = require( "isdodd" ) local mkdirtree = require( "mkdirtree" ) local notf = require( "notf" ) local rmdirtree = require( "rmdirtree" ) -- setup t( mkdirtree{ [ "tmp_dirif" ] = { [ "d" ] = {}, [ "f" ] = "" } }, true ) -- test entries = collectk( dirif( "tmp_dirif", notf( isdodd ) ) ) table.sort( entries ) t( #entries, 2 ) t( entries[ 1 ], "d" ) t( entries[ 2 ], "f" ) -- teardown t( rmdirtree( "tmp_dirif" ), true ) t()