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 table = Map( "%one%" -> "1", "%two%" -> "2", "%three%" -> "3" ) | |
// ↓もっとカッコ良く書けないかな?? | |
def replaceSpecialWords(target: String) = { | |
var str = target | |
table.keys.foreach { key => | |
str = str.replaceAll(key, table(key)) | |
} | |
str | |
} |
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
trait User { | |
val name: String | |
} | |
trait Password { | |
val key: Option[String] | |
} | |
//------------------------------- |
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
import scalaz._, Scalaz._ | |
type Field = Vector[Vector[Option[String]]] | |
object Field { | |
def update(x: Int, y: Int): PLens[Field, Option[String]] = { | |
PLens.vectorNthPLens(y) >=> PLens.vectorNthPLens(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
import scalaz._, Scalaz._ | |
case class Position(x: Int, y: Int) | |
case class Panel(position: Position, obj: String) | |
val px = Lens.lensu[Position, Int]( (pos, _x) => pos.copy(x = _x), _.x) | |
/* | |
val px = Lens.lensu[Position, Int] ( | |
(pos: Position, tmpX: Int) => pos.copy(x = tmpX), | |
(pos: Position) => pos.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
scala> :paste | |
object MyLens { | |
trait Lens[A, B] { | |
self => | |
def get(a: A): B | |
def set(a: A, b: B): A | |
def modify(f: B => B, a: A): A |
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
// $ sbt console | |
import akka.actor.{Actor, ActorSystem, Props} | |
import akka.contrib.throttle.Throttler._ | |
import akka.contrib.throttle.TimerBasedThrottler | |
import scala.concurrent.duration._ | |
class PrintActor extends Actor { | |
def receive = { | |
case x => println(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
trait Monoid[A] { | |
def zero: A | |
def append(x: A, y: A): A | |
} | |
trait MonoidFunctions { | |
implicit class MonoidFunction[A: Monoid](x: A) { | |
val m = implicitly[Monoid[A]] | |
def |+|(a: A) = m.append(x, a) |
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
object EDCBA { | |
case class A(value: Int) | |
case class B(value: Option[A]) | |
case class C(value: B) | |
case class D(value: Option[C]) | |
case class E(value: D) | |
val edcba: Option[E] = Some(E(D(Some(C(B(Some(A(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
trait Either3Optics { | |
final def pLeft3[A, B, C, D]: PPrism[Either3[A, B, C], Either3[D, B, C], A, D] = | |
PPrism[Either3[A, B, C], Either3[D, B, C], A, D] { | |
case Left3(a) => \/-(a) | |
case Middle3(b) => -\/(Middle3(b)) | |
case Right3(c) => -\/(Right3(c)) | |
}(Left3.apply) | |
final def left3[A, B, C]: Prism[Either3[A, B, C], A] = |
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
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> import akka.actor.ActorSystem | |
import akka.actor.ActorSystem | |
scala> import akka.stream.ActorMaterializer | |
import akka.stream.ActorMaterializer |