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

Alternate Semigroup instance: SearchResult #80

Open
k0ral opened this issue Oct 30, 2017 · 2 comments
Open

Alternate Semigroup instance: SearchResult #80

k0ral opened this issue Oct 30, 2017 · 2 comments

Comments

@k0ral
Copy link

k0ral commented Oct 30, 2017

Consider the following data type:

-- | Either a, or b, or both a and b
data SearchResult a b = Scanned a | Found b | ScannedAndFound a b

-- | Accumulate 'a's from left to right, until one 'b' is found
instance Semigroup a => Semigroup (SearchResult a b) where
  ScannedAndFound a b <> _ = ScannedAndFound a b
  Found b <> _ = Found b
  Scanned a <> Scanned a' = Scanned (a <> a')
  Scanned a <> Found b = ScannedAndFound a b
  Scanned a <> ScannedAndFound a' b = ScannedAndFound (a <> a') b

The type definition is identical to These, but the Semigroup semantics differ. Do you think it could/should be integrated to these library, e.g. as a newtype over These ?

Rationale: as the name suggests, this type is useful in search algorithms to return both the search result and a semigroup of traversed items.

@phadej
Copy link
Collaborator

phadej commented Oct 30, 2017

This is different from These a (First b) as This part turns into First too, when there is first That found. In current form it's a bit ad-hoc, I wonder if it can be generalised

@phadej phadej changed the title Alternate Semigroup instance Alternate Semigroup instance: SearchResult Jan 26, 2019
@phadej
Copy link
Collaborator

phadej commented Jan 26, 2019

I'm still undecided. Maybe if you make a module Data.These.SearchResult. I guess most instances can be derived using GeneralizedNewtypeDeriving.

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

No branches or pull requests

2 participants