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

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement