Skip to content

Instantly share code, notes, and snippets.

View aoiroaoino's full-sized avatar
🏠
Working from home

Naoki Aoyama aoiroaoino

🏠
Working from home
View GitHub Profile
@aoiroaoino
aoiroaoino / gist:9308248
Created March 2, 2014 15:29
for文で三角形
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
@aoiroaoino
aoiroaoino / ParsingExample.scala
Created June 7, 2014 10:51
「Scalaプログラミング入門」から引用。
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 {
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
}
trait User {
val name: String
}
trait Password {
val key: Option[String]
}
//-------------------------------
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)
}
}
@aoiroaoino
aoiroaoino / LensSample.scala
Last active August 29, 2015 14:07
Lens Sample in Scalaz
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
@aoiroaoino
aoiroaoino / MyLens.scala
Last active August 29, 2015 14:07
MyLens
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
// $ 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)
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)
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))))))))