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
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))))))))
@aoiroaoino
aoiroaoino / Either3OpticsSample.scala
Created September 3, 2015 11:37
Either3Optics sample code.
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] =
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