-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- This doesn't include derivation macros yet - I've tried to keep api changes to a minimum - The various `data` types `apply`/`un` methods in scala3 use `.isInstanceOf` directly - this might be slower than the scala2 `fastCast` macro
- Loading branch information
Showing
50 changed files
with
867 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
version = "2.7.5" | ||
style = defaultWithAlign | ||
maxColumn = 80 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
modules/core/src/main/scala-3/higherkindness/droste/data/Attr.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package higherkindness.droste | ||
package data | ||
|
||
import cats.Comonad | ||
import cats.Eval | ||
import cats.Functor | ||
|
||
import cats.syntax.functor._ | ||
|
||
import prelude._ | ||
|
||
object Attr { | ||
def apply[F[_], A](head: A, tail: F[Attr[F, A]]): Attr[F, A] = | ||
apply((head, tail)) | ||
def apply[F[_], A](f: (A, F[Attr[F, A]])): Attr[F, A] = f.asInstanceOf | ||
def un[F[_], A](f: Attr[F, A]): (A, F[Attr[F, A]]) = f.asInstanceOf | ||
def unapply[F[_], A](f: Attr[F, A]): Some[(A, F[Attr[F, A]])] = Some(un(f)) | ||
|
||
def algebra[F[_], A]: Algebra[AttrF[F, A, *], Attr[F, A]] = | ||
Algebra(fa => Attr(AttrF.un(fa))) | ||
|
||
def coalgebra[F[_], A]: Coalgebra[AttrF[F, A, *], Attr[F, A]] = | ||
Coalgebra(a => AttrF(Attr.un(a))) | ||
|
||
def fromCats[F[_]: Functor, A](cofree: cats.free.Cofree[F, A]): Attr[F, A] = | ||
ana(cofree)(_.tail.value, _.head) | ||
|
||
def unfold[F[_]: Functor, A](a: A)(coalgebra: A => F[A]): Attr[F, A] = | ||
ana(a)(coalgebra, identity) | ||
|
||
/** An inlined anamorphism to `Attr` with a fused map */ | ||
def ana[F[_]: Functor, A, C]( | ||
a: A | ||
)(coalgebra: A => F[A], f: A => C): Attr[F, C] = | ||
Attr(f(a), coalgebra(a).map(ana(_)(coalgebra, f))) | ||
} | ||
|
||
private[data] trait AttrImplicits { | ||
implicit final class AttrOps[F[_], A](attr: Attr[F, A]) { | ||
def tuple: (A, F[Attr[F, A]]) = Attr.un(attr) | ||
def head: A = tuple._1 | ||
def tail: F[Attr[F, A]] = tuple._2 | ||
|
||
def toCats(implicit ev: Functor[F]): cats.free.Cofree[F, A] = | ||
cats.free.Cofree(head, Eval.later(tail.map(_.toCats))) | ||
|
||
def forget(implicit ev: Functor[F]): Fix[F] = | ||
Fix(tail.map(_.forget)) | ||
} | ||
|
||
implicit def drosteAttrComonad[F[_]: Functor]: Comonad[Attr[F, *]] = | ||
new AttrComonad[F] | ||
} | ||
|
||
private[data] final class AttrComonad[F[_]: Functor] | ||
extends Comonad[Attr[F, *]] { | ||
def coflatMap[A, B](fa: Attr[F, A])(f: Attr[F, A] => B): Attr[F, B] = | ||
Attr.ana(fa)(_.tail, f) | ||
|
||
def extract[A](fa: Attr[F, A]): A = fa.head | ||
|
||
def map[A, B](fa: Attr[F, A])(f: A => B): Attr[F, B] = | ||
Attr(f(fa.head), fa.tail.map(_.map(f))) | ||
} |
Oops, something went wrong.