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

Semigroup Result instance? #96

Open
hdgarrood opened this issue Nov 25, 2018 · 2 comments
Open

Semigroup Result instance? #96

hdgarrood opened this issue Nov 25, 2018 · 2 comments
Labels
type: enhancement A new feature or addition.

Comments

@hdgarrood
Copy link
Contributor

How would we feel about the following?

instance semigroupResult :: Semigroup Result where
  Success <> r = r
  Failed str <> _ = Failed str

instance monoidResult :: Monoid Result where
  mempty = Success

This is a use case I have in mind

  log "fromRotationMatrix avoids instability issues when the quaternion has one very small component"
  quickCheck \(VerySmallNum e) a b c ->
    let
      testWith p' =
        let
          p = Rotation.fromQuaternion p'
          q = unsafePartial (Rotation.fromRotationMatrix (Rotation.toRotationMatrix p))
        in
          rApproxEq p q
          <?> show { p, q }
    in
      fold
       [ testWith (Quaternion e a b c)
       , testWith (Quaternion a e b c)
       , testWith (Quaternion a b e c)
       , testWith (Quaternion a b c e)
       ]

Of course I could rewrite this test to call quickCheck four times instead, but I think the instance makes sense, and I think it would be nice to allow writing tests this way. Additionally, if generating random values for a given type is expensive, being able to combine Result values in this way could let you cut down the number of random values you need to generate, since you don't need to call quickCheck as many times.

@garyb
Copy link
Member

garyb commented Nov 25, 2018

Seems reasonable to me!

@i-am-the-slime
Copy link

i-am-the-slime commented Apr 10, 2019

I came here asking for a Monoid instance. mempty could be Success, no?
Edit:
I just read the original post and it's there already.

@JordanMartinez JordanMartinez added the type: enhancement A new feature or addition. label Dec 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A new feature or addition.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants