You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+11Lines changed: 11 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
8
8
## [Unreleased]
9
9
10
+
## [1.1.0] - 2021-07-23
11
+
### Added
12
+
-`neutral` can be used as generic `neutral` value. With this every Semigroup is automatically a Monoid.
13
+
-`neutral(type)` now defaults to returning `neutral` instead of throwing a not-implemented-error.
14
+
15
+
### Changed
16
+
-`pure(Writer, value)` now initializes the accumulator to `TypeClasses.neutral` instead of `Option()`, making it strictly more general with regard to `TypeClasses.combine`.
17
+
18
+
### Removed
19
+
-`reduce_monoid`, `foldl_monoid` and `foldr_monoid` can now only be called as `reduce_monoid(iterable; [init])`. The older variant `reduce_monoid(combine_function, iterable; [init])` was a left over from previous thrown-away iterations.
In case you only have a Semigroup, just wrap it into `Option`, the default `TypeClasses.pure` implementation for writer will use `Option()` internally.
663
+
In case you only have a Semigroup, no problem, as the default `TypeClasses.pure` implementation for writer will use `neutral` as the accumulator, which combines with everything.
Alternative | `TypeClasses.orelse` | alias `⊘` (\oslash)
126
126
127
-
A **Semigroup** just supports `combine`, a **Monoid** in addition supports `neutral`.
127
+
A **Semigroup** just supports `combine`, a **Monoid** in addition supports `neutral`. We define the generic neutral element `neutral` which is neutral to everything, hence every Semigroup is actually a Monoid in Julia. Hence `TypeClasses.neutral` is both a function which returns the neutral element (defaulting to `neutral`), as well as the generic neutral element itself.
128
128
129
129
Sometimes, the type itself has an obvious way of combining multiple values, like for `String` or `Vector`. Other times, the `combine` is forwarded to inner elements in case it is needed.
Copy file name to clipboardExpand all lines: src/TypeClasses/MonoidAlternative.jl
+23-26Lines changed: 23 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -2,17 +2,24 @@
2
2
# ==================
3
3
4
4
"""
5
-
neutral(::Type{T})::T
5
+
neutral
6
+
neutral(::Type)
7
+
neutral(_default_return_value) = neutral
6
8
7
-
Neutral element for `⊕`, also called "identity element".
9
+
Neutral element for `⊕`, also called "identity element". `neutral` is a function which can give you
10
+
the neutral element for a concrete type, or alternatively you can use it as a singleton value which combines with everything.
11
+
12
+
By default `neutral(type)` will return the generic `neutral` singleton. You can override it for your specific type to have a more specific neutral value.
8
13
9
14
We decided for name `neutral` according to https://en.wikipedia.org/wiki/Identity_element. Alternatives seem inappropriate
10
15
- "identity" is already taken
11
16
- "identity_element" seems to long
12
17
- "I" is too ambiguous
13
18
- "unit" seems ambiguous with physical units
14
19
15
-
# Following Laws should hold
20
+
21
+
Following Laws should hold
22
+
--------------------------
16
23
17
24
Left Identity
18
25
@@ -23,7 +30,7 @@ Right Identity
23
30
⊕(t::T, neutral(T)) == t
24
31
"""
25
32
function neutral end
26
-
neutral(T::Type) =error("neutral($T) not defined")
33
+
neutral(T::Type) = neutral# we use the singleton type itself as the generic neutral value
0 commit comments