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 Function as Monoid #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,22 @@ instance Monad ((->) r) where | |
|
||
</details> | ||
|
||
<details> | ||
<summary>Function as Semigroup and Monoid</summary> | ||
|
||
```haskell | ||
-- | @since 4.9.0.0 | ||
instance Semigroup b => Semigroup (a -> b) where | ||
f <> g = \x -> f x <> g x | ||
stimes n f e = stimes n (f e) | ||
|
||
-- | @since 2.01 | ||
instance Monoid b => Monoid (a -> b) where | ||
mempty _ = mempty | ||
``` | ||
|
||
</details> | ||
|
||
<details><summary>"Komposition" of Functions (Kleisli Arrows)</summary> | ||
|
||
```haskell | ||
|
@@ -84,9 +100,10 @@ f = fn .> f3 .> f2 .> f1 | |
``` | ||
|
||
```haskell | ||
-- Functor Law | ||
f <$> id == f | ||
-- Functor Laws | ||
id <$> f == f | ||
f <$> id == f | ||
(f <$> g) <$> h = f <$> (g <$> h) | ||
``` | ||
|
||
</details> | ||
|
@@ -131,6 +148,43 @@ f = f1 =<< f2 | |
f = f2 >>= f1 | ||
``` | ||
|
||
```haskell | ||
-- Monad Laws | ||
pure a >>= f = f a | ||
f >>= pure = f | ||
(f >>= g) >>= h = f >>= (\x -> g x >>= h) | ||
``` | ||
|
||
</details> | ||
|
||
<details><summary>Function as Semigroup</summary> | ||
|
||
```haskell | ||
f x = f1 x <> f2 x | ||
f = f1 <> f2 | ||
stevemao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
```haskell | ||
f x = f1 x <> f2 x <> f3 x <> fn x | ||
f = f1 <> f2 <> f3 <> fn | ||
``` | ||
|
||
</details> | ||
|
||
<details><summary>Function as Monoid</summary> | ||
|
||
```haskell | ||
f x = f1 x <> mempty | ||
f = f1 <> mempty | ||
Comment on lines
+177
to
+178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if it's useful for composition :). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shineli1984 Yeah I wonder if there's any real life examples/usage of using this one? |
||
``` | ||
|
||
```haskell | ||
-- Monoid Laws | ||
mempty <> f = f | ||
f <> mempty = f | ||
(f <> g) <> h = f <> (g <> h) | ||
``` | ||
|
||
</details> | ||
|
||
<details><summary>"Komposition" of Functions (Kleisli Arrows)</summary> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m wondering if it’s better to write
mempty = const mempty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just copy paste, same as other code under GHC Source Code