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>}]}

Please don’t paste huge iex dumps. Just explain your problem and requirements.

Quick prototype:

defmodule Something do
  def decimal_zero(), do: Decimal.new(0.0)

  def empty?(nil), do: true

  def empty?(%Decimal{} = d) do
    Decimal.cmp(d, decimal_zero()) == :eq  
  end

  def empty?(list) when is_list(list) do
    Enum.any?(list, &(not empty?(&1))
  end
end

You can extend this to tuples and maps if you need support for those.

Thanks. This might not work. The structure I have is quite deep.

You were given a good start. You can do the rest easily.

There are ways of saying this that sound less hostile.

1 Like

What would you propose? I was a bit sharp, but hostile… hm.

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.”

3 Likes

Point taken for the future. Thank you.

1 Like