Last active
December 11, 2018 15:20
-
-
Save stasimus/4618e4bec6cfc6f30a16da20135d88e6 to your computer and use it in GitHub Desktop.
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
final def to(n: Int): List[Int] = n match { | |
case 0 => Nil | |
case n => n :: to(n - 1) | |
} |
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
def prod(list: List[Int]): Int = list match { | |
case Nil => 1 | |
case head :: tail => head * prod(tail) | |
} |
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
val sum: (Int, Int) => Int = _ + _ | |
val curried: Int => Int => Int = sum.curried | |
def sumIsIso(a: Int, b: Int) = { | |
assert(sum(a, b) == curried(a)(b)) | |
assert(sum(a, b) == Function.uncurried(curried)(a, b)) | |
} |
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
/** | |
* -Ywarn-value-discard | |
*/ | |
def intFunction(): Unit = { | |
3 | |
} | |
def stringFunction(): Unit = { | |
"3" | |
} | |
// Despite return types functions aren't isomorphic | |
assert(intFunction() == stringFunction()) |
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
abstract class Iso[T, U] { | |
def get(t: T): U | |
def reverseGet(u: U): T | |
} | |
class Int2String extends Iso[Int, String] { | |
override def get(t: Int): String = t.toString | |
override def reverseGet(u: String): Int = u.toInt | |
} |
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
type ListAnamorphism[U, T] = U => List[T] | |
def listAnamorphismFactory[U, T](func: T => Option[(U, T)]): ListAnamorphism[T, U] = { | |
in: T => | |
def ana(initial: T): List[U] = func(initial) match { | |
case None => Nil | |
case Some((current, next)) => current :: ana(next) | |
} | |
ana(in) | |
} | |
val listDestruct: ListAnamorphism[Int, Int] = listAnamorphismFactory { i: Int => if (i == 0) None else Some(i, i - 1) } |
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
type ListCatamorphism[T, U] = List[T] => U | |
def listCatamorphism[T, U](value: U, func: (T, U) => U): ListCatamorphism[T, U] = { | |
def cata(list: List[T]): U = { | |
list match { | |
case Nil => value | |
case head :: tail => func(head, cata(tail)) | |
} | |
} | |
cata | |
} |
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
type ListCatamorphism[T, U] = List[T] => U | |
def listCatamorphismFactory[T, U](value: U, func: (T, U) => U): ListCatamorphism[T, U] = { | |
def cata(list: List[T]): U = { | |
list match { | |
case Nil => value | |
case head :: tail => func(head, cata(tail)) | |
} | |
} | |
cata | |
} |
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
type ListHylomorphism[U] = U => U | |
def listHylomorphism[U, T](cata: ListCatamorphism[T, U], ana: ListAnamorphism[U, T]): ListHylomorphism[U] = cata.compose(ana) | |
val factorial = listHylomorphism(prod, listDestruct) | |
assert(factorial(3) == 6) |
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
def prod: List[Int] => Int = listCatamorphism[Int, Int](1, _ * _) |
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
class EitherInt2String extends Iso[Either[String, Int], String] { | |
override def get(t: Either[String, Int]): String = t match { | |
case Left(l) => l | |
case Right(r) => r.toString | |
} | |
override def reverseGet(u: String): Either[String, Int] = { | |
Try(u.toInt).toEither.left.map(_ => u) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment