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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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

We're in Beta

About us Mission Statement