donnieparka

donnieparka

Function call expecting list works with a tuple

defmodule Navigator do
  def navigate(dir) do
    expanded_dir = Path.expand(dir)

    go_through([expanded_dir])
  end

  def go_through([]), do: nil

  def go_through([content | rest]) do
    print_and_navigate(content, File.dir?(content))

    go_through(rest)
  end

  def print_and_navigate(_dir, false), do: nil

  def print_and_navigate(dir, true) do
    IO.puts(dir)

    children_dirs = File.ls!(dir)

    go_through(expand_dirs(children_dirs, dir))
  end

  def expand_dirs([], relativeto), do: []

  def expand_dirs([dir | dirs], relative_to) do
    expanded_dir = Path.expand(dir, relative_to)

    [expanded_dir | expand_dirs(dirs, relative_to)]
  end
end

in:

def print_and_navigate(dir, true) do
   IO.puts(dir)

   children_dirs = File.ls!(dir)

   go_through(expand_dirs(children_dirs, dir))
 end

children_dirs is a tuple and expand_dirs expects a list so why do i not get an error? do functions pattern match incoming arguments before throwing errors of arguments compatibility? where can i read more about where and when pattern matching happens?

Marked As Solved

dimitarvp

dimitarvp

It doesn’t seem to be. File.ls! returns a list.

Also Liked

LostKobrakai

LostKobrakai

The problem is more that not just behaviour around errors changes, but also around successes.

  • {:ok, result} becomes the unwraped result
  • {:error, error} becomes the error being raised

This is such a common convention it is even documented: Naming conventions — Elixir v1.18.2

But in the docs for this function it’s not really explained well.

dimitarvp

dimitarvp

What is confusing? You are using the so-called bang function – File.ls! – that does not return tuples; it either returns the values you requested, or raises a runtime error.

You seem to be mistaking it for the non-bang function – File.ls.

Check the @specs more closely.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement