Skip to content

Commit

Permalink
Remove use of old alpha annotation, improve Semigroups
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilquist committed Oct 12, 2023
1 parent f77f2a0 commit b0212c0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 21 deletions.
12 changes: 6 additions & 6 deletions core/shared/src/main/scala/leopards/Apply.scala
Expand Up @@ -16,12 +16,12 @@

package leopards

import scala.annotation.alpha
import scala.annotation.targetName

trait Apply[F[_]] extends Functor[F], Semigroupal[F]:
extension [A, B](ff: F[A => B])
@alpha("ap")
def <*>(fa: F[A]): F[B]
def ap[A, B](ff: F[A => B], fa: F[A]): F[B]

extension [A, B](ff: F[A => B]) inline def <*>(fa: F[A]): F[B] = ap(ff, fa)

extension [T <: NonEmptyTuple](tuple: T)(using toMap: Tuple.IsMappedBy[F][T])
inline def mapN[B](f: Tuple.InverseMap[T, F] => B): F[B] =
Expand Down Expand Up @@ -51,8 +51,8 @@ trait Apply[F[_]] extends Functor[F], Semigroupal[F]:
override def product[B](fb: F[B]): F[(A, B)] =
fa.map(a => (b: B) => (a, b)) <*> fb

@alpha("productL") def <*[B](fb: F[B]): F[A] =
@targetName("productL") def <*[B](fb: F[B]): F[A] =
fa.map2(fb)((a, _) => a)

@alpha("productR") def *>[B](fb: F[B]): F[B] =
@targetName("productR") def *>[B](fb: F[B]): F[B] =
fa.map2(fb)((_, b) => b)
5 changes: 2 additions & 3 deletions core/shared/src/main/scala/leopards/FlatMap.scala
Expand Up @@ -19,8 +19,7 @@ package leopards
trait FlatMap[F[_]] extends Apply[F]:
extension [A](fa: F[A]) def flatMap[B](f: A => F[B]): F[B]

extension [A, B](ff: F[A => B])
override def <*>(fa: F[A]): F[B] =
ff.flatMap(f => fa.map(f))
def ap[A, B](ff: F[A => B], fa: F[A]): F[B] =
ff.flatMap(f => fa.map(f))

extension [A](ffa: F[F[A]]) def flatten: F[A] = ffa.flatMap(identity)
14 changes: 5 additions & 9 deletions core/shared/src/main/scala/leopards/Semigroup.scala
Expand Up @@ -16,15 +16,11 @@

package leopards

import scala.annotation.alpha

trait Semigroup[A]:
extension (x: A)
@alpha("combine")
def |+|(y: A): A
def combine(x: A, y: A): A
extension (x: A) inline def |+|(y: A): A = combine(x, y)

object Semigroup:
given Semigroup[Int] with
extension (x: Int)
@alpha("combine")
def |+|(y: Int) = x + y
export leopards.intMonoid
export leopards.stdListMonoid
export leopards.stdOptionMonoid
21 changes: 21 additions & 0 deletions core/shared/src/main/scala/leopards/instances/int.scala
@@ -0,0 +1,21 @@
/*
* 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 intMonoid: Monoid[Int] with
def combine(x: Int, y: Int) = x + y
def empty = 0
4 changes: 4 additions & 0 deletions core/shared/src/main/scala/leopards/instances/list.scala
Expand Up @@ -24,3 +24,7 @@ given stdListInstances: Monad[List] with Traverse[List] with
fa.foldRight(G.pure(Nil: List[B]))((a, acc) => f(a).map2(acc)(_ :: _))
def foldLeft[B](b: B)(f: (B, A) => B): B = fa.foldLeft(b)(f)
def foldRight[B](b: B)(f: (A, B) => B): B = fa.foldRight(b)(f)

given stdListMonoid[A]: Monoid[List[A]] with
def empty = Nil
def combine(x: List[A], y: List[A]) = x ++ y
4 changes: 4 additions & 0 deletions core/shared/src/main/scala/leopards/instances/option.scala
Expand Up @@ -24,3 +24,7 @@ given stdOptionInstances: Monad[Option] with Traverse[Option] with
fa.fold(G.pure(None: Option[B]))(a => f(a).map(Some(_)))
def foldLeft[B](b: B)(f: (B, A) => B): B = fa.fold(b)(a => f(b, a))
def foldRight[B](b: B)(f: (A, B) => B): B = fa.fold(b)(a => f(a, b))

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))
5 changes: 2 additions & 3 deletions core/shared/src/main/scala/leopards/instances/try.scala
Expand Up @@ -26,6 +26,5 @@ given stdTryInstances: ApplicativeError[Try, Throwable] with
override def map[B](f: A => B): Try[B] = fa.map(f)
override def handleError(f: Throwable => A): Try[A] = fa.recover[A] { case e => f(e) }

extension [A, B](ff: Try[A => B])
override def <*>(fa: Try[A]): Try[B] =
ff.flatMap(f => fa.map(f))
def ap[A, B](ff: Try[A => B], fa: Try[A]): Try[B] =
ff.flatMap(f => fa.map(f))

0 comments on commit b0212c0

Please sign in to comment.