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> import scala.language.experimental.macros | |
import scala.language.experimental.macros | |
scala> import scala.reflect.macros.Context | |
import scala.reflect.macros.Context | |
scala> def for_impl(c: Context) = c.universe.reify(println("*\n**\n***\n****\n***\n**\n*")) | |
for_impl: (c: scala.reflect.macros.Context)c.universe.Expr[Unit] | |
scala> def for文 = macro for_impl |
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 scala.util.parsing.combinator._ | |
trait RunParser { | |
this: RegexParsers => | |
type RootType | |
def root: Parser[RootType] | |
def run(in: String): ParseResult[RootType] = parseAll(root, in) | |
} | |
object CalcSkel extends JavaTokenParsers with RunParser { |
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)))))))) | |
OlderNewer