Map, filter and reduce

Hey there.

What is thing? Why do you still have the Enum.map(&Enum.to_list/1) line? Did you run the line I suggested to help you understand what calling Enum.map on a map results in?

The issue here is that I think you’re simply combining bits of code people are giving you without understanding what each piece is doing, and what the values are at each point. Start at the very very beginning

query = user_params
|> Enum.map(fn x -> IO.inspect(x) end)

Figure out what that does, and what it means. Until you do that all this other stuff isn’t gonna help.

1 Like

I did earlier ,

user_param = [%{"age" => 19}, %{"name" => "matt213"}, %{"sex" => "male"}]
  user_param
  |> Enum.map(&Enum.to_list/1)
  |> Enum.reduce(User, fn thing, query ->
IO.inspect thing 
  end)

output:-

[{"name", "matt213"}]
[{"sex", "male"}]

user_params = [%{"age" => 19,"name" => "matt213"}]

 user_params
  |> Enum.map(&Enum.to_list/1)
  |> Enum.reduce(User, fn thing, query ->
IO.inspect thing
  end)

output
[{"age", 19}, {"name", "matt213"}]

hence i updated my codebase by putting for each loop

for {key, value} <- thing do

    field = String.to_existing_atom(key)
    search = "%#{value}%"
    from q in query, where: like(field(q, ^field), ^search)

end

Enum.map(&Enum.to_list/1)
and reduce in any language require some sort of array to loop that is why it is there and mapping is for conversion array like structure for key value pairs

1 Like

You’re skipping the most important step here.

iex(1)> %{"age" => 19,"name" => "matt213"} |> Enum.map(fn x -> IO.inspect(x) end)
{"age", 19}
{"name", "matt213"}
[{"age", 19}, {"name", "matt213"}]

You can Enum.map directly over a map, and then each item is a key value pair. This is true with a reduce as well.

Doing a for loop inside reduce won’t help by the way, you end up just returning a list of queries instead of reducing into a single query.

To bring this all to a close the easiest way to do this is:

user_params = %{"age" => 19,"name" => "matt213"}

query =
  user_params
  |> Enum.reduce(User, fn {key, value}, query ->
    field = String.to_existing_atom(key)
    search = "%#{value}%"
    from q in query, where: like(field(q, ^field), ^search)
  end)

users = Repo.all(query)
1 Like

my userparams is
user_params = [%{"age" => 19,"name" => "matt213"}]
not like this
user_params = %{"age" => 19,"name" => "matt213"}

1 Like

This is what got me working on just a map. Why is there a wrapping list?

1 Like

my bad from start

before i asked for query,I had hardcoded the values for earlier payload method hence i was getting error in my earlier version of map i tried,

 user_params
  |> Enum.reduce(User, fn {k,x}, query ->
IO.inspect(k)
IO.inspect(x)
  end)

but it was throwing error since i forgot to remove [] from earlier payload
legacy payload
[%{"age" => 19}, %{"name" => "matt213"}, %{"sex" => "male"}]
updated harcoded payload for evaluation purpose
user_params = [%{"age" => 19,"name" => "matt213"}]
actual payload
user_params = %{"age" => 19,"name" => "matt213"}

hence, by which you have suggested by that time i forgot to remove [] from harcoded payload params

Really apologize for inconvenience in context with so much time and intellectual programming power put into ! :101: would be really carefull moving forword !

1 Like

Glad we could get things sorted out :slight_smile:

2 Likes

Hi ,
what modification is required to put date range in below code base

payload=%{"payload" => %{"PageNum" => 1, "PageSize" => 5,
 "daterange" => %{"enddate" => "2017-02-10", "startdate" => "2017-02-02"},
 "searchparam" => %{"fullname" => "poli"}}}

searchparam=payload["searchparam"]
pageSize=payload["PageSize"]
pageNum=payload["PageNum"]
daterange=payload["daterange"]
startdate=daterange["startdate"]
enddate=daterange["enddate"]
next_offset = pageSize*pageNum;


query =
  searchparam
  |> Enum.reduce(User, fn {key, value}, query ->
    field = String.to_existing_atom(key)
    search = "%#{value}%"
    from q in query,where: like(field(q, ^field), ^search),offset: ^next_offset,limit: ^pageSize
  end)
IO.inspect query
users = Repo.all(query)
1 Like

Hi,

I resolved my query by adding below code snippets

{:ok, formattedstartdate}=Timex.parse(startdate, "%Y-%m-%d", :strftime)
{:ok, formattedenddate}=Timex.parse(enddate, "%Y-%m-%d", :strftime)
stardust=from q in query,where: q.inserted_at >=^formattedstartdate,
where: q.inserted_at <=^formattedenddate,offset: ^next_offset,limit: ^pageSize
users = Repo.all(stardust)
2 Likes