Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for more context in groupWhile #123

Open
peterszerzo opened this issue Jun 18, 2019 · 3 comments
Open

Proposal for more context in groupWhile #123

peterszerzo opened this issue Jun 18, 2019 · 3 comments

Comments

@peterszerzo
Copy link

peterszerzo commented Jun 18, 2019

I recently came across the need for a version of groupWhile where the grouping function needs more context than just two adjacent elements.

Use-case

I am rendering the following messages in a chat-like layout:

messages =
  [ { receivedAt = 99, author = "Simon" }
  , { receivedAt = 100, author = "Maria" }
  , { receivedAt = 110, author = "Maria" }
  , { receivedAt = 120, author = "Maria" }
  , { receivedAt = 130, author = "Maria" }
  ]

Messages should be rendered closer together when they have a different author, but also if more than 15 time units elapse within a single group. The current groupWhile would put Maria's messages in the same group despite there being 30 time units between the first and the last message.

Proposed change to groupWhile

(or function under new name)

In the following type definition, both the previous member in the group as well as elements in the group before it show up in the grouping function (in the (List a, a) tuple):

groupWhile : ((List a, a) -> a -> Bool) -> List a -> List (a, List a)

Using this, I can group my messages for rendering as follows:

groupWhile
  (\( beforeInGroup, previous ) current ->
    previous.author == current.author &&
      (List.head beforeInGroup
        |> Maybe.withDefault previous
        |> .receivedAt
        |> (\receivedAt -> abs (receivedAt - current.receivedAt) < 15)
      )
  )
  messages
@Chadtech
Copy link
Collaborator

Hey @peterszerzo

I think this isnt such a good idea for groupWhile. It does make it more general, and therefore cover more usecases (which is good) but I think it also makes it more complicated, and so it makes it harder to use in the normal use case of comparing two adjacent items.

Maybe it would be worthwhile as a separate function? Im not sure. It would need an original name too, if we went down that road.

@peterszerzo
Copy link
Author

Thanks for looking at the issue, @Chadtech - I agree with your reasoning, especially if there isn't much interest. I'm considering publishing a library that combines helpers from multiple -extra libraries and a few other things useful in large applications (like a functor for undo-redo), maybe I'll just include a version of groupWhile (with a different name) there.

@Chadtech
Copy link
Collaborator

Thanks for the well written proposal tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants