write a code snippet using effect-ts library to crawl a filesystem directory
- User
write a code snippet using effect-ts library to crawl a filesystem directory
Assistant
Here's a code snippet using the effect-ts library to crawl a filesystem directory:
import * as T from 'effect-ts' import * as fs from 'fs' const crawlDir: T.Effect<Error, void> = T.chain( T.effectAsync(() => fs.readdir('.', { withFileTypes: true })), (dirents) => { const effects: T.Effect<Error, void>[] = [] for (const dirent of dirents) { if (dirent.isDirectory()) { effects.push(crawlDir(dirent.name)) } else if (dirent.isFile()) { effects.push(T.effect(() => console.log(dirent.name))) } } return T.parallel(effects) } ) T.run(crawlDir)This does the following:
- Uses
fs.readdir()to get the directory entries - Checks if each entry is a directory or file
- If it's a directory, recursively crawls it
- If it's a file, prints its name
- Uses
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.