Skip to content

Instantly share code, notes, and snippets.

@denismaster
Last active December 17, 2017 20:27
Show Gist options
  • Save denismaster/41e362f432f59520036d526e961a24d3 to your computer and use it in GitHub Desktop.
Save denismaster/41e362f432f59520036d526e961a24d3 to your computer and use it in GitHub Desktop.
ПСЯПР шестая
object Main extends App {
def identity[T](value: T): T = value
class Box[T] {
self =>
var value: Option[T] = None
def save(arg: T): Unit = value = Some(arg)
def get: T = value.get
def map[R](f: T => R): Box[R] = new Box[R]{
override def get: R = f(self.get)
}
def contraMap[R](g: R => T): Box[R] = new Box[R] {
override def save(arg: R): Unit = self.save(g(arg))
}
def xmap[R](f: T => R, g: R => T): Box[R] = new Box[R]{
override def save(arg: R): Unit = self.save(g(arg))
override def get: R = f(self.get)
}
}
def testCovariative(): Unit = {
println("***Testing Covariative Functor***")
val box = new Box[Int]
box.save(51)
val toBinaryFunc: Int => String = Integer.toBinaryString
val mappedBox = box map toBinaryFunc
println("Example use value:" + mappedBox.get)
val identityBox: Box[Int] = box map identity
println("Pass Identity Rule:" + (identityBox.get == box.get))
val squareFunc: Int => Int = (t) => t * t
val squaredBinaryBox = box map (squareFunc andThen toBinaryFunc)
println("Pass Composition Rule:" + (squaredBinaryBox.get == "101000101001"))
}
def testContravariative(): Unit = {
println("\n***Testing Contravariative Functor***")
val box = new Box[Int]
box.save(0)
val parseFunc= (s:String)=>Integer.parseInt(s,16)
val contraMappedBox = box contraMap parseFunc
contraMappedBox.save("AF")
println("Example use value:" + box.get)
val identityBox: Box[Int] = box contraMap identity
identityBox.save(0)
println("Pass Identity Rule:" + (box.get==0))
val squareFunc: Int => Int = (t) => t * t
val squaredBinaryBox = box contraMap (parseFunc andThen squareFunc)
val squaredBinaryBox2 = box.contraMap(squareFunc).contraMap(parseFunc)
squaredBinaryBox.save("A")
val result1 = box.get
squaredBinaryBox2.save("A")
val result2 = box.get
println("Pass Composition Rule:" + (result1==result2))
}
def testInvariative(): Unit = {
println("\n***Testing Invariative Functor***")
val box = new Box[String]
box.save("101")
val toBinaryFunc: Int => String = Integer.toBinaryString
val fromBinaryFunc: String=>Int = i=>Integer.parseInt(i,2)
val xBox = box xmap (fromBinaryFunc,toBinaryFunc)
println("Example use value:" + xBox.get)
val identityBox: Box[String] = box xmap (identity, identity)
identityBox.save("2")
println("Pass Identity Rule:" + (identityBox.get=="2"))
val b1: Box[Int] = box.xmap(fromBinaryFunc, toBinaryFunc).xmap(i=>i+3,i=>i-3);
val b2: Box[Int] = box xmap (fromBinaryFunc andThen (i=>i+3),((i:Int)=>i-3) andThen toBinaryFunc);
b1.save(10)
val result1 = b1.get
b2.save(10)
val result2 = b2.get
println("Pass Composition Rule:" + (result1==result2))
}
testCovariative()
testContravariative()
testInvariative()
}
@denismaster
Copy link
Author

Не готова еще

@denismaster
Copy link
Author

Теперь работает

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment