Skip to content

Instantly share code, notes, and snippets.

View mather's full-sized avatar
🐻
friendly bear

Eisuke Kuwahata mather

🐻
friendly bear
View GitHub Profile
@mather
mather / TraitLazyExam.scala
Last active August 29, 2015 14:25
Scalaのトレイトで抽象メンバとそれを参照する`lazy val`のあるときに、`lazy val`を参照する側にも常に`lazy`であることを要求してしまう例
trait Hoge {
protected val hoge: String
lazy val l = hoge.length
}
trait Piyo {
this: Hoge =>
val p = l
}
import Data.List
main = do
l <- fmap toInt $ getLine -- 標準入力からLを読み込む
n <- fmap toInt $ getLine -- 標準入力からNを読み込む(使わない
bars <- fmap (fmap toInt . lines) $ getContents -- 残りのリストを取得
print (sum $ fmap (findTripleSum l) $ tails bars) -- tails で部分リストを順番に調べる
-- Int化
toInt :: [Char] -> Int
@mather
mather / elemCount.hs
Last active April 27, 2016 00:26
複数のアプローチでリスト内の同一要素の数を数える
module ElemCount where
import Data.List
-- | for empty list
--
-- >>> elemCountOrd []
-- []
--
-- | count the same element
@mather
mather / Calculator.scala
Created February 23, 2015 02:32
Calculator Sample
// "sealed" : Declare that no other subclass except this file. This helps compiler to verify loss of pattern.
sealed trait Expression {
def a: Int
def b: Int
}
case class Addition(a: Int, b: Int) extends Expression
case class Subtraction(a: Int, b: Int) extends Expression
case class Multiply(a: Int, b: Int) extends Expression
case class Division(a: Int, b: Int) extends Expression
class A
class B extends A
class C extends A
def isType[T <: A](a: A) = a.isInstanceOf[T]
// <console>:9: warning: abstract type T is unchecked since it is eliminated by erasure
// def isType[T <: A](a: A) = a.isInstanceOf[T]
// ^
// isType: [T <: A](a: A)Boolean
@mather
mather / Hoge.scala
Last active February 25, 2016 04:08
import scala.reflect.ClassTag
object Hoge {
def main(args: Array[String]) = {
val s = new Hoge[StringSample]
val i = new Hoge[IntSample]
s.detectType(StringSample("hogehoge")) //=> detected!
s.detectType(IntSample(1))
i.detectType(StringSample("hogehoge"))
i.detectType(IntSample(2)) //=> detected!
@mather
mather / mandelbrot.scala
Last active August 29, 2015 14:04
http://justindomke.wordpress.com/2008/11/29/mandelbrot-in-scala/ のマンデルブロ集合を描画するプログラムのリファクタ
import java.io._
import java.lang.Math
import scala.annotation.tailrec
/**
* ref: http://justindomke.wordpress.com/2008/11/29/mandelbrot-in-scala/
*/
object mandelbrot {
// tiny complex number class including syntactic sugar for basic operations
@mather
mather / file0.scala
Created July 29, 2014 02:46
IgnoreCaseなパーサコンビネータ ref: http://qiita.com/mather314/items/0535f237f007c13942dc
trait SampleParsers extends RegexParsers {
/** 文字列に".i"を付けると大文字小文字を区別せずマッチさせるようにする拡張 */
implicit class IgnoreCaseString(s: String) {
def i: Parser[String] = ("""(?i)\Q""" + s + """\E""").r
}
def keyword: Parser[String] = "keyword:".i
def value: Parser[String] = "[a-zA-Z0-9]+".r
def parameter: Parser[(String,String)] = keyword ~ value ^^ { case k~v => (k,v) }
}
@mather
mather / Dockerfile
Last active August 29, 2015 14:04
Scala REPL by Docker
FROM debian:latest
MAINTAINER mather <mather314@gmail.com>
RUN apt-get update
RUN apt-get autoremove -y
RUN apt-get install -y wget openjdk-7-jre
RUN wget http://downloads.typesafe.com/scala/2.11.1/scala-2.11.1.deb
RUN dpkg -i scala-2.11.1.deb
import akka.actor.{ExtendedActorSystem, Extension, ExtensionId}
class SampleExtentionImpl(system: ExtendedActorSystem) extends Extension {
val hoge = "HOGE"
}
object SampleExtension extends ExtensionId[SampleExtensionImpl] {
def createExtension(system: ExtendedActorSystem) = new SampleExtensionImpl(system)
}