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 / 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
@aoiroaoino
aoiroaoino / file0.scala
Created November 26, 2012 12:33
match式にてcase classのチェック漏れを検出 ref: http://qiita.com/items/30d02a5306514df67de0
sealed abstract class Season
case class Spring() extends Season
case class Summer() extends Season
case class Fall() extends Season
case class Winter() extends Season
// FallとWinterを忘れたパターンマッチ
scala> def checkSeason(s: Season) = s match {
| case Spring() => "Spring!"
| case Summer() => "Summer!"
import scala.util.parsing.combinator._
abstract class AST
case class Add(left: AST, right: AST) extends AST
case class Sub(left: AST, right: AST) extends AST
case class Multi(left: AST, right: AST) extends AST
case class Div(left: AST, right: AST) extends AST
case class Num(num: Int) extends AST
class OperationParsers extends RegexParsers {
//
// ちょっと冗長なクラスとメソッドの宣言
//
class Human(name: string) {
def say(partner: String): String = {
return "Hello " + partner
}
}
//
@aoiroaoino
aoiroaoino / gist:5547397
Last active December 17, 2015 03:59
val と var
//
// Scalaの変数宣言は二種類あります。
//
val name: String = "Aoino"
var num: Int = 5
//
// valは再代入不可な変数です。(value)
//
scala> val name = "Aoino"
!SLIDE
# test
!SLIDE
## 第三回社内Scala勉強会
!SLIDE
(個人的には第一回ですけど...)
!SLIDE
## 第一回社内Scala勉強会
!SLIDE
@aoiroaoino
aoiroaoino / splitN.scala
Created August 2, 2013 13:04
任意の要素数でListを区切るメソッド
def splitN[A](list: List[A], index: Int): List[List[A]] = if(list.length == 0) List() else if(index < 1) List(list) else List(list.take(index)) ::: splitN[A](list.drop(index), index)
scala> splitN(List(1,2,3,4,5,6,7,8,9,0), 3)
rest0: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9), List(0))
scala> splitN(List("Red", "Green", "Yellow", "Blue"), 1)
res1: List[List[java.lang.String]] = List(List(Red), List(Green), List(Yellow), List(Blue))
@aoiroaoino
aoiroaoino / MonoidFoldLeft.scala
Last active December 28, 2015 00:39
しゅごいぃぃぃいいい
trait Monoid[A] {
def mzero: A
def mappend(a: A, b: A): A
}
trait FoldLeft[F[_]] {
def foldLeft[A, B](xs: F[A], b: B, f: (B, A) => B): B
}
object Monoid {