Maxximiliann

Maxximiliann

Filtering a map against a list of values

trades = 
%{
  "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2" => [
    %{
      asset: "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2",
      last_price: 0.09019287,
      priced_in: "WAVES",
      volume: 13918.3182330112
    },
    %{
      asset: "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2",
      last_price: 0.1313214,
      priced_in: "DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p",
      volume: 9986.99466236054
    },
    %{
      asset: "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2",
      last_price: 1.268e-5,
      priced_in: "8LQW8f7P5d5PZM7GtZEBgaqRPGSzS3DfPuiXrURJ4AJS",
      volume: 9786.93694331709
    }
  ],
  "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu" => [
    %{
      asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu",
      last_price: 192.01,
      priced_in: "WAVES",
      volume: 137852.319057268
    },
    %{
      asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu",
      last_price: 279.152165,
      priced_in: "DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p",
      volume: 77018.2552159809
    },
    %{
      asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu",
      last_price: 0.02768054,
      priced_in: "8LQW8f7P5d5PZM7GtZEBgaqRPGSzS3DfPuiXrURJ4AJS",
      volume: 38723.9182969945
    },
    %{
      asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu",
      last_price: 459.22,
      priced_in: "Ft8X1v1LTa1ABafufpaCWyVj8KkaxUWE6xBhW6sNFJck",
      volume: 513.413421879477
    }
  ],
  "4LHHvYGNKJUg5hj65aGD5vgScvCBmLpdRFtjokvCjSL8" => [
    %{
      asset: "4LHHvYGNKJUg5hj65aGD5vgScvCBmLpdRFtjokvCjSL8",
      last_price: 0.05171444,
      priced_in: "WAVES",
      volume: 14110.9296573005
    },
    %{
      asset: "4LHHvYGNKJUg5hj65aGD5vgScvCBmLpdRFtjokvCjSL8",
      last_price: 7.5e-6,
      priced_in: "8LQW8f7P5d5PZM7GtZEBgaqRPGSzS3DfPuiXrURJ4AJS",
      volume: 9722.17123718049
    },
    %{
      asset: "4LHHvYGNKJUg5hj65aGD5vgScvCBmLpdRFtjokvCjSL8",
      last_price: 0.075459,
      priced_in: "DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p",
      volume: 8482.23199810624
    }
  ]
}
defmodule Remove_Blacklisted_Assets do		

	def filter_excluded_assets(number_of_assets_to_be_excluded, accumulator) when accumulator == number_of_assets_to_be_excluded do

		excluded = ['DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p', '8LQW8f7P5d5PZM7GtZEBgaqRPGSzS3DfPuiXrURJ4AJS', '2tR9EKvDPMThfrFSb96CSBGYNwNPtzwkGcdhQjv4dVoj']
		number_of_assets_to_be_excluded = length(excluded)			
				
		Enum.filter(trades, fn map -> map.priced_in != Enum.fetch!(excluded, 0) end)
			|> Enum.into(%{})
			|> IO.inspect
	end



	def filter_excluded_assets(number_of_assets_to_be_excluded, accumulator) do

		excluded = ['DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p', '8LQW8f7P5d5PZM7GtZEBgaqRPGSzS3DfPuiXrURJ4AJS', '2tR9EKvDPMThfrFSb96CSBGYNwNPtzwkGcdhQjv4dVoj']
		number_of_assets_to_be_excluded = length(excluded)

		Enum.filter(trades, fn map -> map.priced_in != Enum.fetch!(excluded,(number_of_assets_to_be_excluded - accumulator)) end)
			|> IO.inspect
			
		filter_excluded_assets(number_of_assets_to_be_excluded, accumulator + 1)
	end
	
			
end

Desired output:

trades = 
%{
  "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2" => [
    %{
      asset: "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2",
      last_price: 0.09019287,
      priced_in: "WAVES",
      volume: 13918.3182330112
    }
  ],
  "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu" => [
    %{
      asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu",
      last_price: 192.01,
      priced_in: "WAVES",
      volume: 137852.319057268
    },
    %{
      asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu",
      last_price: 459.22,
      priced_in: "Ft8X1v1LTa1ABafufpaCWyVj8KkaxUWE6xBhW6sNFJck",
      volume: 513.413421879477
    }
  ],
  "4LHHvYGNKJUg5hj65aGD5vgScvCBmLpdRFtjokvCjSL8" => [
    %{
      asset: "4LHHvYGNKJUg5hj65aGD5vgScvCBmLpdRFtjokvCjSL8",
      last_price: 0.05171444,
      priced_in: "WAVES",
      volume: 14110.9296573005
    }
    }
  ]
}
iex(1)> Remove_Blacklisted_Assets.filter_excluded_assets(3,0)
** (ArgumentError) you attempted to apply :priced_in on {"3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2", [%{asset: "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2", last_price: 0.09019287, priced_in: "WAVES", volume: 13888.8199642112}, %{asset: "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2", last_price: 1.268e-5, priced_in: "8LQW8f7P5d5PZM7GtZEBgaqRPGSzS3DfPuiXrURJ4AJS", volume: 9792.16173281305}, %{asset: "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2", last_price: 0.1313214, priced_in: "DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p", volume: 9610.63424570971}]}.

How do I get Enum.filter to drill down to the map where priced_in is located?

Also, is the repetition of the excluded lists and the number_of_asssets_to_be_excluded functions in each function best practice?

Marked As Solved

kokolegorille

kokolegorille

It is wrong to think ‘A’ is equal to “A”, they are not. You might encounter some bad surprises if You think so :slight_smile:

Please try to understand the solution, because You should be able to write it, as descriptive as it is.

iex> excluded = ["DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p", "8LQW8f7P5d5PZM7GtZEBgaqRPGSzS3DfPuiXrURJ4AJS", "2tR9EKvDPMThfrFSb96CSBGYNwNPtzwkGcdhQjv4dVoj"]
iex> trades 
|> Enum.reduce(%{}, fn {k, l}, acc -> 
  Map.put(acc, k, Enum.reject(l, & &1.priced_in in excluded)) 
end)
%{
  "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2" => [
    %{
      asset: "3QvxP6YFBKpWJSMAfYtL8Niv8KmmKsnpb9uQwQpg8QN2",
      last_price: 0.09019287,
      priced_in: "WAVES",
      volume: 13918.3182330112
    }
  ],
  "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu" => [
    %{
      asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu",
      last_price: 192.01,
      priced_in: "WAVES",
      volume: 137852.319057268
    },
    %{
      asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu",
      last_price: 459.22,
      priced_in: "Ft8X1v1LTa1ABafufpaCWyVj8KkaxUWE6xBhW6sNFJck",
      volume: 513.413421879477
    }
  ],
  "4LHHvYGNKJUg5hj65aGD5vgScvCBmLpdRFtjokvCjSL8" => [
    %{
      asset: "4LHHvYGNKJUg5hj65aGD5vgScvCBmLpdRFtjokvCjSL8",
      last_price: 0.05171444,
      priced_in: "WAVES",
      volume: 14110.9296573005
    }
  ]
}

Often You will realize You can write it as a one line command.

Also Liked

kokolegorille

kokolegorille

I prefer to use Enum.reduce when I would otherwise use Enum.map followed by Enum.into. It’s an iteration less.

Enum.reduce is worth learning how to use it… It takes an enumerable, and reduce it to a single value.

In this case, the value is a map.

All of your module recursive code could be replaced by

Enum.reject(l, & &1.priced_in in excluded)

It takes a list, and rejects the items whose price_in is in excluded. In this case reject is more appropriate than filter.

Map.put(acc, k, Enum.reject(l, & &1.priced_in in excluded)) 

It takes a map, and returns a map with k set to the new list.

trades |> Enum.reduce(%{}, fn {k, l}, acc -> 

It takes a map as enumerable (trades), and set acc to the initial value of an empty map. It loops through trades, each item is a tuple of key and value. In this case value is a list.

Each iteration will return a map, which is the updated accumulator, with the key set to the new list.

Finally, it returns the final accumulator, which is what You want :slight_smile:

The Enum module is probably the most used module, and it’s worth knowing all its functions.

ericgray

ericgray

Just to add what @kokolegorille has already explained usually when you need an accumulator or you need to pass around some state Enum.reduce is usually the right tool for the job.

Matter of fact everything in the Enum module is implemented with Enum.reduce except for zip. Jose Valim explains why and when you should use Enum.reduce in one of his Twitch Videos.
When you get a chance you should watch them, you’ll learn a lot.

kokolegorille

kokolegorille

There are many errors…

When You use Enum on a map, You receive a tuple of key and value…

Enum.filter(trades, fn {_key, map} -> map.priced_in

It’s what the error message tells You…

Not only this, but the map, is not a map, it’s a list of map.

You are not using number_of_assets_to_be_excluded, then You set it to a new value.

What are You trying to do? If You want to test if a value is in a list, use Enum.member? or in.

Maxximiliann

Maxximiliann

Thanks for the recommendation. His videos are great! :slight_smile:

Where Next?

Popular in Questions Top

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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

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
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
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement