CharlesO

CharlesO

Need help with a Custom empty? function

I have this sample data structure below.

It is considered not empty if any of it’s Decimal values is greater than decimal<0>

Please how would you check for that?

My code below incorrectly returns false, when it should return true:

  def empty?(data) do
    if Enum.empty?(data) do
      true
    else
      Enum.any?(data, fn x ->
        Decimal.decimal?(x) && !(Decimal.cmp(x, Decimal.new(0)) == :eq)
      end)
    end
  end

data = %{"10100" => [{{1, 1, "employer-issuer-code"}, "PEC000000000000"}, {{1, 2, "instrument-code"}, "OTH20000000"}, {{1, 3, "instrument-rating"}, "00"}, {{1, 4, "coupon-rate"}, #Decimal<0.00>}, {{1, 5, "foreign-currency-type"}, "EUR"}, {{1, 6, "conversion-rate"}, #Decimal<0.0000>}, {{1, 7, "issue-amount-foreign-currency"}, #Decimal<0.00>}, {{1, 8, "face-value-foreign-currency"}, #Decimal<0.00>}, {{1, 9, "holding-units"}, #Decimal<0.0000>}, {{1, 10, "cost-foreign-currency"}, #Decimal<0.00>}, {{1, 11, "value-date"}, "31-Mar-2017"}, {{1, 12, "maturity-date"}, "31-Mar-2017"}, {{1, 13, "accrued-coupon-to-date-foreign-currency"}, #Decimal<0.00>}, {{1, 14, "market-value-foreign-currency"}, #Decimal<0.00>}, {{1, 15, "market-value-naira"}, #Decimal<0.00>}], "20100" => [{{1, 1, "employer-issuer-code"}, "PEC000000000000"}, {{1, 2, "instrument-code"}, "OTH20000000"}, {{1, 3, "instrument-rating"}, "00"}, {{1, 4, "coupon-rate"}, #Decimal<0.00>}, {{1, 5, "foreign-currency-type"}, "EUR"}, {{1, 6, "conversion-rate"}, #Decimal<0.0000>}, {{1, 7, "issue-amount-foreign-currency"}, #Decimal<0.00>}, {{1, 8, "face-value-foreign-currency"}, #Decimal<0.00>}, {{1, 9, "holding-units"}, #Decimal<0.0000>}, {{1, 10, "cost-foreign-currency"}, #Decimal<0.00>}, {{1, 11, "value-date"}, "31-Mar-2017"}, {{1, 12, "maturity-date"}, "31-Mar-2017"}, {{1, 13, "accrued-coupon-to-date-foreign-currency"}, #Decimal<0.00>}, {{1, 14, "market-value-foreign-currency"}, #Decimal<0.00>}, {{1, 15, "market-value-naira"}, #Decimal<0.00>}], "30100" => [{{1, 1, "employer-issuer-code"}, "PEC000000000000"}, {{1, 2, "instrument-code"}, "OTH20000000"}, {{1, 3, "instrument-rating"}, "00"}, {{1, 4, "coupon-rate"}, #Decimal<0.00>}, {{1, 5, "foreign-currency-type"}, "EUR"}, {{1, 6, "conversion-rate"}, #Decimal<0.0000>}, {{1, 7, "issue-amount-foreign-currency"}, #Decimal<0.00>}, {{1, 8, "face-value-foreign-currency"}, #Decimal<0.00>}, {{1, 9, "holding-units"}, #Decimal<0.0000>}, {{1, 10, "cost-foreign-currency"}, #Decimal<0.00>}, {{1, 11, "value-date"}, "31-Mar-2017"}, {{1, 12, "maturity-date"}, "31-Mar-2017"}, {{1, 13, "accrued-coupon-to-date-foreign-currency"}, #Decimal<0.00>}, {{1, 14, "market-value-foreign-currency"}, #Decimal<0.00>}, {{1, 15, "market-value-naira"}, #Decimal<0.00>}], "40104" => [{{1, 1, "employer-issuer-code"}, "PEC000000000000"}, {{1, 2, "instrument-code"}, "OTH20000000"}, {{1, 3, "instrument-rating"}, "00"}, {{1, 4, "coupon-rate"}, #Decimal<0.00>}, {{1, 5, "foreign-currency-type"}, "EUR"}, {{1, 6, "conversion-rate"}, #Decimal<0.0000>}, {{1, 7, "issue-amount-foreign-currency"}, #Decimal<0.00>}, {{1, 8, "face-value-foreign-currency"}, #Decimal<0.00>}, {{1, 9, "holding-units"}, #Decimal<0.0000>}, {{1, 10, "cost-foreign-currency"}, #Decimal<0.00>}, {{1, 11, "value-date"}, "31-Mar-2017"}, {{1, 12, "maturity-date"}, "31-Mar-2017"}, {{1, 13, "accrued-coupon-to-date-foreign-currency"}, #Decimal<0.00>}, {{1, 14, "market-value-foreign-currency"}, #Decimal<0.00>}, {{1, 15, "market-value-naira"}, #Decimal<0.00>}], "40153" => [{{1, 1, "employer-issuer-code"}, "PEC000000000000"}, {{1, 2, "instrument-code"}, "OTH20000000"}, {{1, 3, "instrument-rating"}, "00"}, {{1, 4, "coupon-rate"}, #Decimal<0.00>}, {{1, 5, "foreign-currency-type"}, "EUR"}, {{1, 6, "conversion-rate"}, #Decimal<0.0000>}, {{1, 7, "issue-amount-foreign-currency"}, #Decimal<0.00>}, {{1, 8, "face-value-foreign-currency"}, #Decimal<0.00>}, {{1, 9, "holding-units"}, #Decimal<0.0000>}, {{1, 10, "cost-foreign-currency"}, #Decimal<0.00>}, {{1, 11, "value-date"}, "31-Mar-2017"}, {{1, 12, "maturity-date"}, "31-Mar-2017"}, {{1, 13, "accrued-coupon-to-date-foreign-currency"}, #Decimal<0.00>}, {{1, 14, "market-value-foreign-currency"}, #Decimal<0.00>}, {{1, 15, "market-value-naira"}, #Decimal<0.00>}], "40191" => [{{1, 1, "employer-issuer-code"}, "PEC000000000000"}, {{1, 2, "instrument-code"}, "OTH20000000"}, {{1, 3, "instrument-rating"}, "00"}, {{1, 4, "coupon-rate"}, #Decimal<0.00>}, {{1, 5, "foreign-currency-type"}, "EUR"}, {{1, 6, "conversion-rate"}, #Decimal<0.0000>}, {{1, 7, "issue-amount-foreign-currency"}, #Decimal<0.00>}, {{1, 8, "face-value-foreign-currency"}, #Decimal<0.00>}, {{1, 9, "holding-units"}, #Decimal<0.0000>}, {{1, 10, "cost-foreign-currency"}, #Decimal<0.00>}, {{1, 11, "value-date"}, "31-Mar-2017"}, {{1, 12, "maturity-date"}, "31-Mar-2017"}, {{1, 13, "accrued-coupon-to-date-foreign-currency"}, #Decimal<0.00>}, {{1, 14, "market-value-foreign-currency"}, #Decimal<0.00>}, {{1, 15, "market-value-naira"}, #Decimal<0.00>}], "40192" => [{{1, 1, "employer-issuer-code"}, "PEC000000000000"}, {{1, 2, "instrument-code"}, "OTH20000000"}, {{1, 3, "instrument-rating"}, "00"}, {{1, 4, "coupon-rate"}, #Decimal<0.00>}, {{1, 5, "foreign-currency-type"}, "EUR"}, {{1, 6, "conversion-rate"}, #Decimal<0.0000>}, {{1, 7, "issue-amount-foreign-currency"}, #Decimal<0.00>}, {{1, 8, "face-value-foreign-currency"}, #Decimal<0.00>}, {{1, 9, "holding-units"}, #Decimal<0.0000>}, {{1, 10, "cost-foreign-currency"}, #Decimal<0.00>}, {{1, 11, "value-date"}, "31-Mar-2017"}, {{1, 12, "maturity-date"}, "31-Mar-2017"}, {{1, 13, "accrued-coupon-to-date-foreign-currency"}, #Decimal<0.00>}, {{1, 14, "market-value-foreign-currency"}, #Decimal<0.00>}, {{1, 15, "market-value-naira"}, #Decimal<0.00>}], "70100" => [{{1, 1, "employer-issuer-code"}, "PEC000000000000"}, {{1, 2, "instrument-code"}, "OTH20000000"}, {{1, 3, "instrument-rating"}, "00"}, {{1, 4, "coupon-rate"}, #Decimal<0.00>}, {{1, 5, "foreign-currency-type"}, "EUR"}, {{1, 6, "conversion-rate"}, #Decimal<0.0000>}, {{1, 7, "issue-amount-foreign-currency"}, #Decimal<0.00>}, {{1, 8, "face-value-foreign-currency"}, #Decimal<0.00>}, {{1, 9, "holding-units"}, #Decimal<0.0000>}, {{1, 10, "cost-foreign-currency"}, #Decimal<0.00>}, {{1, 11, "value-date"}, "31-Mar-2017"}, {{1, 12, "maturity-date"}, "31-Mar-2017"}, {{1, 13, "accrued-coupon-to-date-foreign-currency"}, #Decimal<0.00>}, {{1, 14, "market-value-foreign-currency"}, #Decimal<0.00>}, {{1, 15, "market-value-naira"}, #Decimal<0.00>}]}

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I’ll accept sharp in lieu of hostile.

“What I wrote should cover the fundamental idea, see if you can extend it further on your own, and post back here if you run into issues.”

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

There are ways of saying this that sound less hostile.

dimitarvp

dimitarvp

Point taken for the future. Thank you.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
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

Other popular topics Top

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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement