전체 글
-
scala 에서 retry 구현하기scala/basic 2021. 11. 25. 18:11
문제 cats-effect 나 zio 등의 functional library 를 사용하면, 이미 구현되어 있는 retry 를 쉽게 사용할 수 있다. 혹은 retry library 를 활용할 수도 있다. 간단하게 쓸수 있도록 scala 표준 라이브러리만을 사용해 retry 를 구현해 보자. stackoverflow 를 참고할 수 있다. 코드 import scala.annotation.tailrec import scala.util.{Failure, Success, Try} object Util { @tailrec def retry[T](n: Int)(f: => T): Try[T] = if (n < 0) { Failure(new Exception(s"$n must be greater or equal to 0"..
-
Scalikejdbc 를 이용해 DB 데이터 읽기scala jdbc/scalikejdbc 2021. 11. 14. 21:35
문제 MySQL test.person 테이블 스키마와 들어 있는 데이터는 아래와 같다. CREATE table person ( name varchar(128), age INT ) name age a 10 b 20 test.person 에 저장된 데이터를 scalikejdbc 를 이용해 읽어보자 코드 build.sbt 에 아래 라이브러리를 추가한다. libraryDependencies ++= Seq( "org.scalikejdbc" %% "scalikejdbc" % "4.0.0", "org.scalikejdbc" %% "scalikejdbc-config" % "4.0.0", "org.postgresql" % "postgresql" % "42.5.0", "mysql" % "mysql-connector-jav..
-
scalatags 를 이용해 Email 로 전송할 Html 생성하기web/scalajs 2021. 11. 14. 20:50
문제 Email 로 전송할 Html String 을 생성하려고 한다. scalatags 를 이용해 typesafe 한 아래 모양의 Html 을 만들어 보자 코드 build.sbt 에 아래 라이브러리를 추가한다. libraryDependencies ++= Seq( "com.lihaoyi" %% "scalatags" % "0.9.4" ) Html 을 생성하는 코드를 아래와 같이 작성한다. import scalatags.Text import scalatags.Text.all._ object EmailHtml { case class Item(no: Int, engine: String, url: String) def main(args: Array[String]): Unit = { val items: Seq[Ite..
-
scalatest-3.0.5 의 property based testscala/scalatest 2021. 11. 14. 18:45
문제 정수 n 에 대해 n 의 마지막 자리수를 구하는 함수 n 이 5 의 배수인지를 체크하는 함수를 구현했다. object Calculator { def lastDigit(n: Int): Int = math.abs(n) % 10 def isDivisibleByFive(n: Int): Boolean = n % 5 == 0 } 잘 구현되었는지 체크를 위해 scalatest property based test 를 작성하자 특히, n 이 5의 배수일 경우 n 의 마지막 자리수가 0 또는 5 임을 체크하도록 하자. 코드 build.sbt 에 scalatest, scalacheck 라이브러리를 추가한다. libraryDependencies ++= Seq( "org.scalatest" %% "scalatest" % ..
-
scala enumeratum enum 사용하기scala/basic 2021. 11. 13. 23:20
enumeratum 을 사용해 보자 build.sbt 에 라이브러리를 추가한다. libraryDependencies ++= Seq( "com.beachape" %% "enumeratum" % "1.7.0", "com.beachape" %% "enumeratum-play-json" % "1.7.0" ) 간단한 경우 import enumeratum._ import play.api.libs.json.Json import scala.collection.immutable sealed abstract class Country extends EnumEntry object Country extends Enum[Country] with PlayJsonEnum[Country] { override def values: im..
-
wsl2 에서 windows localhost 접근하기wsl2 2021. 11. 13. 00:18
문제 windows intellij 에서 아래 처럼 서버를 port 9000 으로 실행해 두었다. wsl2 에서 localhost 로 접속을 시도했을 때 아래처럼 Connection refused 가 발생한다. 해결 wsl2 내에서는 localhost 가 아니라 {wsl2 hostname}.local 을 사용해야 한다. 현재 hostname 이 AL01929730 이니 AL01929730.local 로 사용해야 한다. 주의: windows 에서 실행된 서버가 Listening for HTTP on 0.0.0.0:9000 처럼 외부에서 접속가능하도록 구성된 상태여야 한다. (127.0.0.1:9000 처럼 실행되어서는 안된다.) 설명 반대로 wsl2 ubuntu 터미널에서 실행된 서버를 windows 에서..
-
docker 로 mysql 설치하고 접속하기docker 2021. 11. 7. 20:05
mysql 설치 & 실행 아래 script 로 mysql DB 설치&실행 #!/bin/bash MYSQL_DIR={MY_MYSQL_DIR} docker run -d \ --cap-add=sys_nice \ --name mysql \ -v $MYSQL_DIR/data:/var/lib/mysql \ -v $MYSQL_DIR/conf:/etc/mysql/conf.d \ -p 3306:3306 \ -e TZ=Asia/Seoul \ -e MYSQL_ROOT_PASSWORD={MY_PASSWORD} \ mysql:8.0.26 $MYSQL_DIR/conf/mysql.cnf 파일을 생성해 둔다. 예제) [mysqld] mysql 에 접속해 보기 docker exec -it mysql mysql -u root -p{MY..
-
docker 로 postgresql 설치하고 접속하기docker 2021. 11. 7. 19:12
postgresql 설치 & 실행 아래 script 로 postgresql DB 설치&실행 #!/bin/bash POSTGRESQL_DIR={MY_POSTGRES_DIR} docker run -d --name postgres \ -e POSTGRES_USER=postgres \ -e POSTGRES_PASSWORD={MY_PASSWORD} \ -e POSTGRES_DB=postgres \ -v $POSTGRESQL_DIR/conf/postgresql.conf:/etc/postgresql/postgresql.conf \ -v $POSTGRESQL_DIR/data:/var/lib/postgresql/data \ -p 5432:5432 \ postgres -c 'config_file=/etc/postgresq..