Skip to content

Instantly share code, notes, and snippets.

@msbaek
msbaek / MobileResponseTimeSpec.scala
Last active August 29, 2015 14:16
mean, standard deviation
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics
import org.scalatest.{FlatSpec, Matchers}
import scala.io.Source
class MobileResponseTimeSpec extends FlatSpec with Matchers {
val FILE_NAME = "/Users/msbaek/Downloads/res_time.txt"
val NINETY = 90.0
val THREE_SECONDS = 3.0
val TEN_SECONDS = 10.0
@msbaek
msbaek / CrawlAndSaveSpec.scala
Last active August 29, 2015 14:18
sdss crawl, parse and persist using sorm
import models.{DB, SdssRecord}
import org.joda.time.LocalDate
import org.specs2.mutable.Specification
import play.api.libs.ws.{WS, WSResponse}
import play.api.test.WithApplication
import scala.concurrent.Future
import scala.util.{Failure, Success}
import scala.util.matching.Regex.Match
import akka.actor.ActorSystem
import models.{DB, SdssRecord}
import org.joda.time.LocalDate
import org.specs2.mutable.Specification
import play.api.libs.ws.{WS, WSResponse}
import play.api.test.WithApplication
import spray.http.{HttpRequest, HttpResponse}
import akka.actor.ActorSystem
import spray.http.{HttpRequest, HttpResponse}
@msbaek
msbaek / crawler.scala
Created April 30, 2015 08:56
http get map
import scala.io.Source
object crawler extends App {
val DAUM_URL = "http://m.search.daum.net/search?w=tot&q=%s"
val URL_EXPRESSION = """http://[A-Za-z0-9-_]+.[A-Za-z0-9-_:%&?/.=]+""".r
val queries = Seq("이효리", "날씨", "다음")
(queries map getHttp).flatten foreach println
@msbaek
msbaek / ScalaSymbols.scala
Last active August 29, 2015 14:20
scala symbols
/** **********************************
* -> "in" generator
* ***********************************/
for (arg <- List("one", "two", "three"))
println(arg)
/** **********************************
* ::: 리스트를 붙일 때
* **********************************/
List(1, 2) ::: List(3, 4, 5)
@msbaek
msbaek / ListMethods.scala
Last active August 29, 2015 14:20
some list methods and usages
// the empty list
List()
Nil
// 원소를 갖는 리스트 생성
List("Cool", "tools", "rule")
// 3개의 값으로 새로운 List 생성
val thrill = "Will" :: "fill" :: "until" :: Nil
@msbaek
msbaek / Listing-3.scala
Last active August 29, 2015 14:20
Printing formatted character counts for the lines of a url
// Listing 3.11 Printing formatted character counts for the lines of a url
import scala.io.Source
def widthOfLength(s: String) = s.length.toString.length
val lines = Source.fromURL("https://gist.githubusercontent.com/msbaek/e830f66f5165fbbb844e/raw/d48da60bb3479f5f708367e677a492462afcf66e/gistfile1.scala", "utf8").getLines().toList
val longestLine = lines.reduceLeft(
(a, b) => if(a.length > b.length) a else b
)
val maxWidth = widthOfLength(longestLine)
@msbaek
msbaek / LoggingTest.java
Created June 4, 2015 05:29
logging using slf4j
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggingTest {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Test
public void foo() {
logger.error("error for {} message for {}", 123, "hello");
/**
* foldLeft
*/
List(1, 2, 3, 4, 5).foldLeft(0) { (totalSofar, current) =>
totalSofar + current
}
List(1, 2, 3, 4, 5).foldLeft(List[Int]())((b, a) => a :: b)
val rr : Range = 1 to 10
rr.foldLeft(0)(_ + _) // sum
rr.foldLeft(1)(_ * _) // product
@msbaek
msbaek / partialFunction.scala
Created June 5, 2015 05:50
Partial Function
/**
* partial function
* 타입 A의 모든 값에 대해서 정의되지 않은 부분 함수 <--> 전체함수
*/
val fraction = new PartialFunction[Int, Int] {
override def isDefinedAt(x: Int): Boolean = x != 0
override def apply(v1: Int): Int = 42 / v1
}