I've got the following Elixir code:
defmodule Envvar do
def exists?(env) do :os.getenv(env) != false end
def get(env) do
if exists?(env) do
{env, :os.getenv(env)}
else
{env, nil}
end
end
end
Two things:
- Am I using the
?
correctly here? I'm assuming that any predicate function should have the?
on the end of the name by convention. - Is there a way to code the get function as a pattern match rather than a
if
/else
? I'm asking here because I'm guessing that a pattern match would be more idiomatic Elixir code.
2 Answers 2
Using the ? for a boolean predicate is fine. At least it is what I would do in Ruby and Elixir.
You won't be able to call exists?(env) in a guard clause so pattern matching does not win you much here.
You are calling :os.getenv() twice unneccessarily. I'm not sure why you just want to convert 'false' to 'nil', here is a way:
defmodule Envvar do
def get(env)
do_get(:os.getenv(env))
end
defp do_get(false)
{:env, nil}
end
defp do_get(result)
{:env,result}
end
end
-
\$\begingroup\$ That's an interesting take on the pattern matching that I was looking for. Definitely seems a bit more idiomatic to me. \$\endgroup\$Onorio Catenacci– Onorio Catenacci2014年03月07日 21:49:07 +00:00Commented Mar 7, 2014 at 21:49
-
\$\begingroup\$ By the way, I want the exists? function as a separate function--I would have done a defp if I didn't want to expose it outside of the module. So I'm not really calling :os.getenv() twice unnecessarily. \$\endgroup\$Onorio Catenacci– Onorio Catenacci2014年03月07日 22:00:49 +00:00Commented Mar 7, 2014 at 22:00
-
\$\begingroup\$ @OnorioCatenacci - actually in this solution calling
get
without callingexists?
will call:os.getenv()
less than with callingexists?
. No matter ifenv
exists or not -:os.getenv()
is called exactly once. \$\endgroup\$Uri Agassi– Uri Agassi2014年08月14日 11:40:15 +00:00Commented Aug 14, 2014 at 11:40
Is it necessary to return nil or is false enough? They both evaluate to false when using boolean operators.
If false is good enough:
defmodule Envvar do
def get(env) do
{:env, :os.getenv(env)}
end
end
If the return value has to be nil, I would just convert the second element and leave the first one intact.
defmodule Envvar do
def get(env) do
{:env, false_to_nil(:os.getenv(env)) }
end
defp false_to_nil(false) do
nil
end
defp false_to_nil(v) do
v
end
end
Explore related questions
See similar questions with these tags.