Many of these types have methods defined directly on them, which allow for such combining, e.g.
Many of these types have methods defined directly on them,
which allow for such combining, e.g. ++
on List, but the
value of having a Semigroup
type class available is that these
compose, so for instance, we can say
Map("foo" -> Map("bar" -> 5)).combine(Map("foo" -> Map("bar" -> 6), "baz" -> Map())) Map("foo" -> List(1, 2)).combine(Map("foo" -> List(3,4), "bar" -> List(42)))
which is far more likely to be useful than
Map("foo" -> Map("bar" -> 5)) ++ Map("foo" -> Map("bar" -> 6), "baz" -> Map()) Map("foo" -> List(1, 2)) ++ Map("foo" -> List(3,4), "bar" -> List(42))
since the first version uses the Semigroup's combine
operation, it will merge
the map's values with combine
.
Now that you've learned about the Semigroup
instance for Int
try to
guess how it works in the following examples:
There is inline syntax available for Semigroup
.
There is inline syntax available for Semigroup
. Here we are
following the convention from scalaz, that |+|
is the
operator from Semigroup
.
You'll notice that instead of declaring one
as Some(1)
, I chose
Option(1)
, and I added an explicit type declaration for n
. This is
because there aren't type class instances for Some or None, but for
Option.
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
Please use 'an [Exception] should be thrownBy { ... }' syntax instead
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.
A semigroup for some given type A has a single operation (which we will call
combine
), which takes two values of type A, and returns a value of type A. This operation must be guaranteed to be associative. That is to say that:must be the same as
for all possible values of a,b,c.
There are instances of
Semigroup
defined for many types found in the scala common library. For example,Int
values are combined using addition by default but multiplication is also associative and forms anotherSemigroup
.import cats.Semigroup