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 foldMap and Monoid[Map[K, V]] and fix Monoid[Option[A]] #20

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ target
.vscode
project/metals.sbt
project/project
.scala-build
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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)