전체 글
-
mhtml - non-reacitve 적용하기web/mhtml 2021. 8. 12. 18:46
monadic html 에서는 reactive functional style 이 기본으로 오히려 non-reactive 하게 만들기가 어려운 것 같다. 그런데 가끔씩 non-reactive 하게 구현할 필요가 있다. submit 버튼을 누를 때 화면 변화가 있도록 만들어 보자. 첫번째 코드 import mhtml._ import org.scalajs.dom import scala.scalajs.js import scala.xml.Elem object MainView { case class Info(query: String, deivce: String) def view() = { val submitVar: Var[Unit] = Var(()) val queryVar: Var[String] = Var("") ..
-
mhtml - Seq[A] 를 화면에 보여주기web/mhtml 2021. 8. 12. 14:38
monadic html 가이드 를 보면 scala.xml.Group 을 이용해 Seq[Node] 를 mount 할 수 있다고 하는데, 아래와 같이 코딩하는 것도 잘 동작했다. fomantic-ui List 를 활용했다. 코드 def showList(main: Seq[String], sub: Seq[String]): Elem = Main {main.map(x => {x})} Sub {sub.map(x => {x})} 출력 결과 예시 Main People News Sub Banner
-
mhtml - ajax 호출web/mhtml 2021. 8. 11. 19:51
코드 import mhtml._ import mhtml.future.syntax.FutureToRxSyntax import org.scalajs.dom.ext.Ajax import scala.concurrent.ExecutionContext.Implicits.global import scala.util.Success def crawl(url: String): Rx[Option[String]] = Ajax.get(url).toRx.map { case Some(Success(request)) => Some(request.responseText) case _ => None } 설명 ajax 결과를 toRx 를 이용해 Rx 로 만들면, mhtml Rx 이해하기 - Ajax 호출시 경험 처럼 처리가 복잡해지는 것..
-
-
Scala State Monadscala/basic 2021. 8. 3. 12:12
문제 State Monad 를 만들고, 이를 사용해 다음을 계산하라. 골프 공을 쳐서 처음에는 20m, 두번째는 10m, 세번째는 15m 를 보냈다. 그런데 시작라인에서 3m 를 더 나온 위치에 공을 두고 쳤다면 최종 골프공은 시작라인에서 어느정도 떨어져 있을까? 3 + 20 + 10 + 15 = 48 코드 case class State[S, A](run: S => (S, A)) { def flatMap[B](f: A => State[S, B]): State[S, B] = State { s0 => val (s, a) = run(s0) // 현재 상태의 run 을 적용하고 f(a).run(s) // 이후에 f 를 적용해 S => (S, A) 함수를 갱신함 } // 기존 run 과는 다른 새롭게 정의된 S ..
-
피보나치(fibonacci) 수열을 Scala Stream, ZStream 으로 각각 구현zio/zio1 2021. 8. 3. 00:47
문제 피보나치(fibonacci) 수열 앞 10개를 출력하는 프로그램을 아래 2가지 방법으로 각각 구현하라 scala stream 을 이용해 구현 ZStream 을 이용해 구현 코드 scala stream 을 이용한 구현 object ScalaStreamFibo { def main(args: Array[String]): Unit = { def fibonacci(h: Long, n: Long): Stream[Long] = h #:: fibonacci(n, h + n) fibonacci(0, 1).take(10).foreach(println) } } ZStream 을 이용한 구현 import zio._ import zio.stream.ZStream object ZStreamFibo extends App { ..