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 Function as Monoid #2

Merged
merged 3 commits into from May 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 56 additions & 2 deletions README.md
Expand Up @@ -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
Copy link
Member

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

Copy link
Member Author

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

```

</details>

<details><summary>"Komposition" of Functions (Kleisli Arrows)</summary>

```haskell
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's useful for composition :).
But it is indeed a good brain exercise.

Copy link
Member Author

@stevemao stevemao May 23, 2020

Choose a reason for hiding this comment

The 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>
Expand Down