Skip to content

Commit

Permalink
Merge pull request #20 from typelevel/topic/foldmap
Browse files Browse the repository at this point in the history
Add foldMap and Monoid[Map[K, V]] and fix Monoid[Option[A]]
  • Loading branch information
mpilquist committed Oct 16, 2023
2 parents 05e2260 + 8ec89d4 commit 6e06c1a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ target
.vscode
project/metals.sbt
project/project
.scala-build
4 changes: 3 additions & 1 deletion build.sbt
Expand Up @@ -29,5 +29,7 @@ lazy val root = tlCrossRootProject
lazy val core = crossProject(JVMPlatform, JSPlatform)
.settings(
name := "spotted-leopards",
libraryDependencies += "org.scalameta" %%% "munit-scalacheck" % "0.7.29"
libraryDependencies += "org.scalameta" %%% "munit-scalacheck" % "0.7.29",
Compile / console / scalacOptions := (Compile / console / scalacOptions).value
.filterNot(_.startsWith("-Wunused"))
)
8 changes: 8 additions & 0 deletions core/shared/src/main/scala/leopards/Monoid.scala
Expand Up @@ -18,3 +18,11 @@ package leopards

trait Monoid[A] extends Semigroup[A]:
def empty: A

extension (as: IterableOnce[A])
def combineAll: A =
as.foldMap(identity)(using this)

extension [A](as: IterableOnce[A])
def foldMap[B](f: A => B)(using m: Monoid[B]): B =
as.iterator.foldLeft(m.empty)((acc, a) => acc |+| f(a))
1 change: 1 addition & 0 deletions core/shared/src/main/scala/leopards/Semigroup.scala
Expand Up @@ -24,3 +24,4 @@ object Semigroup:
export leopards.intMonoid
export leopards.stdListMonoid
export leopards.stdOptionMonoid
export leopards.mapMergeMonoid
24 changes: 24 additions & 0 deletions core/shared/src/main/scala/leopards/instances/map.scala
@@ -0,0 +1,24 @@
/*
* Copyright 2019 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package leopards

given mapMergeMonoid[A, B: Semigroup]: Monoid[Map[A, B]] with
val empty = Map.empty
def combine(x: Map[A, B], y: Map[A, B]) =
y.foldLeft(x):
case (acc, (k, v)) =>
acc.updatedWith(k)(_ |+| Some(v))
8 changes: 7 additions & 1 deletion core/shared/src/main/scala/leopards/instances/option.scala
Expand Up @@ -27,4 +27,10 @@ given stdOptionInstances: Monad[Option] with Traverse[Option] with

given stdOptionMonoid[A: Semigroup]: Monoid[Option[A]] with
def empty = None
def combine(x: Option[A], y: Option[A]) = x.flatMap(xx => y.map(yy => xx |+| yy))
def combine(x: Option[A], y: Option[A]) =
x match
case None => y
case Some(xx) =>
y match
case None => x
case Some(yy) => Some(xx |+| yy)

0 comments on commit 6e06c1a

Please sign in to comment.