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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
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
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