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

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
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
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement