laboshinl

laboshinl

Hi all,
Could please someone explain me why am I getting incorrect result with Flow.group_by()

Showing Posts 1 to 9

NobbZ

NobbZ

Because you have neither told us what your code looks like, nor what incorrect results you get and what makes them incorrect in your opinion.

I tend to say, that the result is correct but your assumption does not match your

laboshinl

laboshinl OP

Sorry, I thought I will be able to add details after creation of topic.
I have the following data:

  {609628859800,
   %{
     caddr: {192, 168, 129, 26},
     client: [%{len: 6, pos: 2101578, ...}, %{len: 6, ...}],
     cport: 53283,
     proto: 6,
     saddr: {...},
     ...
   }},
  {15020939,
   %{
     caddr: {192, 168, 129, ...},
     client: [%{len: 35, ...}, %{...}],
     cport: 52158,
     proto: 17,
     ...
   }},
   ...

And two methods:

1)       data
          |> Enum.group_by(fn ({x,y}) -> y.proto end)
          |> Enum.map(fn ({x, y}) -> %{label: x, count: length(y)} end)
          |> Enum.to_list
          |> IO.inspect

That gives me correct result:

        [%{count: 919, label: 6}, %{count: 191, label: 17}]
2)       data
          |> Flow.from_enumerable()
          |> Flow.group_by(fn ({x,y}) -> y.proto end)
          |> Flow.map(fn ({x, y}) -> %{label: x, count: length(y)} end)
          |> Enum.to_list
          |> IO.inspect

That gives me:

     [
      %{count: 90, label: 6},
      %{count: 20, label: 17},
      %{count: 829, label: 6},
      %{count: 171, label: 17}
    ]
NobbZ

NobbZ

I have no way to test right now, but have you tried to partition after the group_by?

This is just q guess though.

laboshinl

laboshinl OP

Invoking Flow.patrition after group_by changes nothing

Invoking it before gives more sparse result

[
  %{count: 104, label: 6},
  %{count: 24, label: 17},
  %{count: 98, label: 6},
  %{count: 21, label: 17},
  %{count: 116, label: 6},
  %{count: 22, label: 17},
  %{count: 140, label: 6},
  %{count: 26, label: 17},
  %{count: 116, label: 6},
  %{count: 21, label: 17},
  %{count: 123, label: 6},
  %{count: 24, label: 17},
  %{count: 103, label: 6},
  %{count: 28, label: 17},
  %{count: 119, label: 6},
  %{count: 25, label: 17}
]
NobbZ

NobbZ

Then I have no clue.

shanesveller

shanesveller

I would try including Flow.reduce/3 after a partition:

File.stream!("path/to/some/file")
|> Flow.from_enumerable()
|> Flow.flat_map(&String.split(&1, " "))
|> Flow.partition()
|> Flow.reduce(fn -> %{} end, fn word, acc ->
  Map.update(acc, word, 1, & &1 + 1)
end)
|> Enum.to_list()
laboshinl

laboshinl OP

Yep, custom reduce function work fine,
however I’m trying to figure out whats wrong with my group_by() which is also a reduce function

lackac

lackac

You need to call partition/2 before group_by/3. The usual purpose of partitioning before a reduce operation is to ensure that events that belong together are handled by the same process. This is important to know when using group_by/3 since you want to group. The examples in the Flow docs deal with this in detail.

In your case what is probably missing is choosing the right key to partition by. Take a look at the docs of partition/2. You will need something like:

|> Flow.partition(key: &elem(&1, 1).proto)
laboshinl

laboshinl OP

Thank you much, guys!
Got it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews