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
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!" |
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._ | |
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 { |
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の変数宣言は二種類あります。 | |
// | |
val name: String = "Aoino" | |
var num: Int = 5 | |
// | |
// valは再代入不可な変数です。(value) | |
// | |
scala> val name = "Aoino" |
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
// | |
// ちょっと冗長なクラスとメソッドの宣言 | |
// | |
class Human(name: string) { | |
def say(partner: String): String = { | |
return "Hello " + partner | |
} | |
} | |
// |
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
!SLIDE | |
# test |
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
!SLIDE | |
## 第三回社内Scala勉強会 | |
!SLIDE | |
(個人的には第一回ですけど...) | |
!SLIDE | |
## 第一回社内Scala勉強会 | |
!SLIDE |
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
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)) | |
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 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 { |
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 { |
OlderNewer