-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
163 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package fpspeedrun | ||
|
||
trait Iso[T, U] { | ||
def wrap(x: T): U | ||
def unwrap(x: U): T | ||
} |
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,63 @@ | ||
package fpspeedrun | ||
|
||
import scala.language.higherKinds | ||
import syntax.semigroup._ | ||
|
||
trait Monoid[T] extends SemiGroup[T] { | ||
def empty: T | ||
} | ||
|
||
object Monoid { | ||
def apply[T](implicit i: Monoid[T]) = i | ||
|
||
object Laws { | ||
def identity[T : Monoid](x: T): Boolean = { | ||
(Monoid[T].empty |+| x) == x && | ||
(x |+| Monoid[T].empty) == x | ||
} | ||
|
||
def associativity[T : Monoid](x: T, y: T, z: T): Boolean = { | ||
((x |+| y) |+| z) == (x |+| (y |+| z)) | ||
} | ||
} | ||
|
||
/** | ||
* Функция, похожая на SemiGroup#combineList, но для Monoid. | ||
* В отличие от предыдущей, возвращает не Option[T], а сам T. | ||
* Для пустого списка, возвращает "единицу" в понятиях моноида. | ||
*/ | ||
def foldList[T : Monoid](list: List[T]): T = { | ||
list.foldLeft(Monoid[T].empty) { | ||
case (sum, next) => sum |+| next | ||
} | ||
} | ||
|
||
/** | ||
* #foldList, но с крутым синтаксисом и возможностью через параметр типа передавать | ||
* желаемое поведение. | ||
* Паттерн для синтаксиса называется: partial type application | ||
*/ | ||
def foldListVia[U[_]] = new FoldListVia[U] | ||
class FoldListVia[U[_]] { | ||
def apply[T](list: List[T])(implicit iso: Iso[T, U[T]], monoid: Monoid[U[T]]): T = { | ||
val r = list.foldLeft(monoid.empty) { | ||
(acc, next) => acc |+| iso.wrap(next) | ||
} | ||
iso.unwrap(r) | ||
} | ||
} | ||
|
||
// monoid instances | ||
implicit val defaultMonoidInt: Monoid[Int] = new Monoid[Int] { | ||
override def empty: Int = 0 | ||
override def combine(x: Int, y: Int): Int = x+y | ||
} | ||
implicit val sumMonoidInt: Monoid[Sum[Int]] = new Monoid[Sum[Int]] { | ||
override def empty: Sum[Int] = Sum(0) | ||
override def combine(x: Sum[Int], y: Sum[Int]): Sum[Int] = Sum(x.x + y.x) | ||
} | ||
implicit val prodMonoidInt: Monoid[Prod[Int]] = new Monoid[Prod[Int]] { | ||
override def empty: Prod[Int] = Prod(1) | ||
override def combine(x: Prod[Int], y: Prod[Int]): Prod[Int] = Prod(x.x * y.x) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package fpspeedrun | ||
import syntax.semigroup._ | ||
|
||
trait SemiGroup[T] { | ||
def combine(x: T, y: T): T | ||
} | ||
|
||
object SemiGroup { | ||
object Laws { | ||
def associativity[T : SemiGroup](x: T, y: T, z: T): Boolean = { | ||
((x |+| y) |+| z) == (x |+| (y |+| z)) | ||
} | ||
} | ||
|
||
implicit val combineString: SemiGroup[String] = (x: String, y: String) => x + y | ||
|
||
|
||
def combineList[T : SemiGroup](list: List[T]): Option[T] = { | ||
list.reduceOption(_ |+| _) | ||
} | ||
|
||
def combineListVia[U[_]] = new CombineListVia[U] | ||
|
||
// partial type application | ||
class CombineListVia[U[_]] { | ||
def apply[T](list: List[T])(implicit iso: Iso[T, U[T]], sg: SemiGroup[U[T]]): Option[T] = | ||
list.reduceOption((x, y) => iso.unwrap(iso.wrap(x) |+| iso.wrap(y))) | ||
} | ||
} | ||
|
||
|
||
final case class Sum[T](x: T) extends AnyVal | ||
final case class Prod[T](x: T) extends AnyVal | ||
|
||
object Sum { | ||
implicit def sumIso[T]: Iso[T, Sum[T]] = new Iso[T, Sum[T]] { | ||
override def wrap(x: T): Sum[T] = Sum(x) | ||
override def unwrap(x: Sum[T]): T = x.x | ||
} | ||
implicit val combineInt: SemiGroup[Sum[Int]] = | ||
(x: Sum[Int], y: Sum[Int]) => Sum(x.x + y.x) | ||
} | ||
|
||
object Prod { | ||
implicit def prodIso[T]: Iso[T, Prod[T]] = new Iso[T, Prod[T]] { | ||
override def wrap(x: T): Prod[T] = Prod(x) | ||
override def unwrap(x: Prod[T]): T = x.x | ||
} | ||
implicit val mulCombineInt: SemiGroup[Prod[Int]] = | ||
(x: Prod[Int], y: Prod[Int]) => Prod(x.x * y.x) | ||
} |
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