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

Backwards and Reverse #2638

Closed
dcastro opened this issue Nov 23, 2018 · 3 comments
Closed

Backwards and Reverse #2638

dcastro opened this issue Nov 23, 2018 · 3 comments

Comments

@dcastro
Copy link
Contributor

dcastro commented Nov 23, 2018

I was wondering what the maintainers think about adding something like Haskell's Control.Applicative.Backwards and Data.Functor.Reverse to cats.
Or maybe something like this already exists in cats and I missed it.

Backwards

Backwards is a newtype wrapper around any f a.
If that f has an instance for Applicative, then so does Backwards f.

With good ol' applicative composition, this code will print "1" and "2", in that order.

data Test = Test Int String
  deriving Show

let x = do putStrLn "1"; pure 1 :: IO Int
let y = do putStrLn "2"; pure "2" :: IO String

Test <$> x <*> y

But by wrapping the effects in Backwards, "2" is printed before "1":

forwards $ Test <$> Backwards x <*> Backwards y

Reverse

Reverse is a newtype wrapper around any f a.
If that f has an instance for Foldable, then so does Reverse f.
If that f has an instance for Traversable, then so does Reverse f.

Good ol' traversal will apply the effects to each element of a list, going left to right:

λ> traverse print [1,2,3]
1
2
3

But by wrapping the f a in Reverse, the effects of the traversal are observed in right to left fashion.

traverse print (Reverse [1,2,3])
3
2
1

If this is deemed a good fit for cats, I wouldn't mind giving it a go.

@chuwy
Copy link

chuwy commented Nov 25, 2018

Regardless of whether this should belong to cats core or not, I would like to have it in Scala.

@LukaJCB
Copy link
Member

LukaJCB commented Nov 25, 2018

We have newts for these kinds of things. But maybe with #1800 and opaque types on the horizon, it might make sense to rethink the separation and consider moving them into the cats repo. :)

@dcastro
Copy link
Contributor Author

dcastro commented Dec 3, 2018

@LukaJCB So, should this go in newts for now, and maybe be moved into cats later?

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

3 participants