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

Repeatedly func #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/matryoshka/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,8 @@ package object matryoshka {
*
* @group algtrans
*/
@tailrec
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like this to be a function, but the problem is that we need this to be tail recursive.

We could actually rewrite this as a coalgebra, like:

final def repeatedlyƒ[A](f: A => Option[A]): Coalgebra[A \/ ?, A] = f(expr) \/> expr

final def repeatedly[A](f: A => Option[A]): A => A =
  _.ana[Nu](repeatedlyƒ(f)).unsafePerformSync

We have some stack-safety tests of Partial that make me think this works, although no performance promises.

But then maybe we can rewrite stuff to use repeatedlyƒ instead of repeatedly, and track partiality through more of the code.

@edmundnoble – thoughts?

final def repeatedly[A](f: A => Option[A])(expr: A): A =
final def repeatedly[A]: (A => Option[A]) => A => A =
(f: A => Option[A]) => (expr: A) =>
f(expr) match {
case None => expr
case Some(e) => repeatedly(f)(e)
Expand Down
3 changes: 3 additions & 0 deletions core/shared/src/main/scala/matryoshka/patterns/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ package object patterns {
AlgebraM[M, CoEnv[A, F, ?], B] =
ginterpretM[Id, M, F, A, B](f, φ)

def ginterpret[W[_], F[_], A, B](f: A => Id[B], φ: GAlgebra[W, F, B]): GAlgebra[W, CoEnv[A, F, ?], B] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏾

ginterpretM[W, Id, F, A, B](f, φ)

def ginterpretM[W[_], M[_], F[_], A, B](f: A => M[B], φ: GAlgebraM[W, M, F, B]):
GAlgebraM[W, M, CoEnv[A, F, ?], B] =
_.run.fold(f, φ)
Expand Down