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( "tapered" ) local dirif = require( "dirif" ) -- util functions local collectk = require( "collectk" ) local isdodd = require( "isdodd" ) local mkdirtree = require( "mkdirtree" ) local notf = require( "notf" ) local rmdirtree = require( "rmdirtree" ) -- setup t.ok( mkdirtree{ [ "tmp_dirif" ] = { [ "d" ] = {}, [ "f" ] = "" } } ) -- test entries = collectk( dirif( "tmp_dirif", notf( isdodd ) ) ) table.sort( entries ) t.is( #entries, 2 ) t.is( "d", entries[ 1 ] ) t.is( "f", entries[ 2 ] ) -- teardown t.ok( rmdirtree( "tmp_dirif" ) ) t.done()