Skip to content

Listing directories #4038

Answered by toots
Radio-biscuit asked this question in Q&A
Jul 18, 2024 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

So, list.iter does not produce any result, it justs applies the function given as argument and discards its result.

You can do one of two things:

  1. Direct filter

This is the most straight forward:

directories = list.filter(
  file.is_directory,
  file.ls(absolute=true, "/directory")
)
  1. Use a reducer/fold

This is the most advanced/functional way. Hard to understand at first but very powerful. You can use list.fold for that, it accumulates the result of applying the reduce function over and over:

basedir = "/directory"

directories = list.fold(
  fun (directories, f) -> begin
    f = path.concat(basedir, f)
    if file.is_directory(f) then
      [f, ...directories]
    else
      director…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@Radio-biscuit
Comment options

@toots
Comment options

Answer selected by toots
@Radio-biscuit
Comment options

@toots
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants