otuv

otuv

Hi,

Simple thing but I cannot seem to find it anywhere. How do I do a case with greater than?

As in:

case Kernel.length my_list do
0 →
{ :ok, :no_data }
> 0 →
{ :ok, my_list }
_ →
{ :error, :some_general_error }

Or should I structure my code some other way?

Showing Posts 1 to 4

axelson

axelson

Scenic Core Team

You’d want to use a guard in this case. Which would look like this

case Kernel.length(my_list) do
  0 ->
    { :ok, :no_data }
  n when n > 0 ->
    { :ok, my_list }
  _ ->
    { :error, :some_general_error }

Although Kernel.length/1 would never return a negative value so you’d probably be better off with Enum.empty?/1 for the code you’ve listed.

7stud

7stud

You can use guards:

case length(my_list) do
  len when len == 0 ->
    { :ok, :no_data }
  len when len > 0 ->
    { :ok, my_list }
  _ ->
    { :error, :some_general_error }
end

Or, you can use multiple function definitions:

defmodule My do

  def proceed([]), do: {:ok, :no_data}
  def proceed(x) when is_list(x), do: {:ok, x}
  def proceed(_), do: { :error, :not_a_list }

end

~/elixir_programs$ iex my.exs
    Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
    Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> My.proceed [1,2,3]
{:ok, [1, 2, 3]}

iex(2)> My.proceed({1, 2})
{:error, :not_a_list}

iex(3)> My.proceed []
{:ok, :no_data}
peerreynders

peerreynders

Or should I structure my code some other way?

defmodule Demo do

  def check_list(list) do
    case list do
      [] ->
        {:ok, :no_data}
      [_|_] ->
        {:ok, list}
      _ ->
        {:error, :some_general_error}
    end
  end

end

IO.inspect(Demo.check_list(1))
IO.inspect(Demo.check_list([]))
IO.inspect(Demo.check_list([1]))
IO.inspect(Demo.check_list([1,2]))
$ elixir demo.exs
{:error, :some_general_error}
{:ok, :no_data}
{:ok, [1]}
{:ok, [1, 2]}
$
otuv

otuv OP

I decided to go for the empty? way. Thanks.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews