我正在尝试编写一个工具,它将从File.ls
获取文件/文件夹列表,添加它们的完整路径,并根据它们是否是目录来过滤它们。
这有效:
directory_contents = File.ls!(directory_path)
contents_with_full_paths = Enum.map directory_contents, fn(item) -> Path.join(directory_path, item) end
only_dirs = Enum.filter contents_with_full_paths, fn(item) -> File.dir?(item) end
这不是:
directory_contents = File.ls!(directory_path)
directory_contents
|> Enum.map fn(item) -> Path.join(directory_path, item) end
|> Enum.filter fn(item) -> File.dir?(item) end
它抛出
** (Protocol.UndefinedError) protocol Enumerable not implemented for #Function<1.10194857/1 in RepoFinder.subdirectories_in/1>, only anonymous functions of arity 2 are enumerable. This protocol is implemented for: Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Range, Stream
(elixir) lib/enum.ex:3213: Enumerable.Function.reduce/3
(elixir) lib/enum.ex:1847: Enum.filter/2
这是为什么?那两个实现不应该以基本相同的方式工作吗?