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

Add operators for fmap and friends #13

Closed
bsima opened this issue Jun 14, 2017 · 4 comments
Closed

Add operators for fmap and friends #13

bsima opened this issue Jun 14, 2017 · 4 comments

Comments

@bsima
Copy link

bsima commented Jun 14, 2017

Late-night haskell hacking has me thinking:

> :t ($)
($) :: (a -> b) -> a -> b
> :t (<|)
(<|) :: (a -> b) -> a -> b
> :t (<$>)
(<$>) :: Functor f => (a -> b) -> f a -> f b
> :t fmap
fmap :: Functor f => (a -> b) -> f a -> f b
> let (<<|) = fmap
> :t (<<|)
(<<|) :: Functor f => (a -> b) -> f a -> f b

My proposal is to add functions (<<|) and (|>>) which do the equivalent of (<|) and (|>) but over functors.

So the example here would go from:

(+) <$> Just 3 <*> Just 4

to:

(+) <<| Just 3 <*> Just 4

which IMHO shows the structure of the operation much better than the original. (Disclaimer: Haskell novice here, may have gotten some terminology wrong.)

@bsima
Copy link
Author

bsima commented Jun 14, 2017

> :t flip fmap
flip fmap :: Functor f => f a -> (a -> b) -> f b
> :t (|>)
(|>) :: a -> (a -> b) -> b

so I think (|>>) = flip fmap should work...

@tfausak
Copy link
Owner

tfausak commented Jun 14, 2017

Thanks for the suggestion! I've stuck with "plain" functions for now because it's not clear to stop. If (<$>) is (<<|) in Flow, what would (=<<) be, (<<<|)?

($)   ::              (a ->   b) ->   a ->   b
(<$>) :: Functor f => (a ->   b) -> f a -> f b
(=<<) :: Monad m   => (a -> m b) -> m a -> m b

Furthermore I generally avoid the applicative style and prefer do notation. ApplicativeDo makes this especially nice. I would write your example as:

do
  x <- Just 3
  y <- Just 4
  pure <| x + y -- or `pure (x + y)` or `pure $ x + y`

@tfausak
Copy link
Owner

tfausak commented Jul 22, 2017

I don't think I'll implement higher-order operators in Flow. If that's something you want, take a look at Data.Functor.Monadic.

@infinity0
Copy link

FunctorMonadic wasn't as nice as I wanted it to be so I've gone ahead and uploaded https://hackage.haskell.org/package/op-0.1.0.0/docs/Control-Op.html

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

3 participants