scala

How to subtract two consecutive element in a list in Scala?

心不动则不痛 提交于 2021-02-10 04:13:47
问题 I would like to subtract two consecutive element in a list with numbers in Scala. For example : I have this list : val sortedList = List(4,5,6) I would like to have an output list like diffList =(1, 1) where 5-4 = 1 and 6-5 = 1 . I tried the following code: var sortedList = List[Int]() var diffList = List[Int]() for (i <- 0 to (sortedList.length - 1) ;j <- i + 1 to sortedList.length - 1) { val diff = (sortedList(j) - sortedList(i)) diffList = diffList :+ diff } I have the following result for

Relation between Function1 and Reader Monad

纵饮孤独 提交于 2021-02-10 04:13:27
问题 although i understand the implementation of the reader monad of which i give 2 of the most prominent way to do it below case class Reader[R, A](run: R => A) def readerMonad[R] = new Monad[({type f[x] = Reader[R,x]})#f] { def unit[A](a: => A): Reader[R, A] = Reader(_ => a) override def flatMap[A,B](st: Reader[R, A])(f: A => Reader[R, B]): Reader[R, B] = Reader(r => f(st.run(r)).run(r)) } or more simply case class Reader[R, A](run: R => A) { def map[B](f: A => B): Reader[R, B] = Reader(r => f

Testing json data using Specs2

落花浮王杯 提交于 2021-02-09 18:37:28
问题 I added(In built.sbt) matcher-extra :- "org.specs2" %% "specs2" % "2.3.4" % "test", "org.specs2" % "specs2-matcher-extra_2.10" % "2.3-scalaz-7.1.0-M3", the ("/" the symbols are not resolving) My example test case for Json is looking like below:- package specs.model import org.specs2.mutable.Specification import org.specs2.matcher.JsonMatchers class Json extends Specification with JsonMatchers { "Json Matcher" should { "1st field" in { val json = """{"name":"sagar"}""" json must /("name" ->

“error: can't find main class scala.tools.nsc.MainGenericRunner” when running scala in windows

江枫思渺然 提交于 2021-02-09 12:01:15
问题 after downloaded scala 2.10.2 for windows, and run scala I met such error: "错误: 找不到或无法加载主类 scala.tools.nsc.MainGenericRunner" which means "error: can't find or load main class scala.tools.nsc.MainGenericRunner". So I check the scala.bat for reasons, and I found such function: :set_home set _BIN_DIR= for %%i in (%~sf0) do set _BIN_DIR=%_BIN_DIR%%%~dpsi echo in set_home: %_BIN_DIR% set _SCALA_HOME=%_BIN_DIR%.. goto :eof After this function the _SCALA_HOME become D:\program files\scala\files

Treat index.html as default file in Akka HTTP's getFromDirectory

元气小坏坏 提交于 2021-02-09 06:59:29
问题 Assuming I have a folder foo with an index.html file in it and the following minimal (but most likely not functional) server code for Akka HTTP below: object Server extends App { val route: Route = pathPrefix("foo") { getFromDirectory("foo") } Http().bindAndHandle(route, "0.0.0.0", 8080) } index.html will correctly be served if I open http://localhost:8080/foo/index.html in a browser, but not if I open http://localhost:8080/foo or http://localhost:8080/foo/ . If this is even possible, how can

Treat index.html as default file in Akka HTTP's getFromDirectory

谁说我不能喝 提交于 2021-02-09 06:58:48
问题 Assuming I have a folder foo with an index.html file in it and the following minimal (but most likely not functional) server code for Akka HTTP below: object Server extends App { val route: Route = pathPrefix("foo") { getFromDirectory("foo") } Http().bindAndHandle(route, "0.0.0.0", 8080) } index.html will correctly be served if I open http://localhost:8080/foo/index.html in a browser, but not if I open http://localhost:8080/foo or http://localhost:8080/foo/ . If this is even possible, how can

Intersection and merge/join two maps in Scala

青春壹個敷衍的年華 提交于 2021-02-09 05:31:52
问题 Let's say I have two maps that look something like this. val m1 = Map(1 -> "One", 2 -> "Two", 3 -> "Three") val m2 = Map(2 -> 2.0, 3 -> 3.0, 4 -> 4.0) I want to get the intersection based on the keys and return a tuple that represents the merged values. The result would look like this. Map(2 -> (Two,2.0), 3 -> (Three,3.0)) I suppose I can resort to something like val merged = m1 collect { case (key, value) if m2.contains(key) => key -> (value, m2(key)) } But is there no "more idiomatic" way

Intersection and merge/join two maps in Scala

丶灬走出姿态 提交于 2021-02-09 05:29:30
问题 Let's say I have two maps that look something like this. val m1 = Map(1 -> "One", 2 -> "Two", 3 -> "Three") val m2 = Map(2 -> 2.0, 3 -> 3.0, 4 -> 4.0) I want to get the intersection based on the keys and return a tuple that represents the merged values. The result would look like this. Map(2 -> (Two,2.0), 3 -> (Three,3.0)) I suppose I can resort to something like val merged = m1 collect { case (key, value) if m2.contains(key) => key -> (value, m2(key)) } But is there no "more idiomatic" way

ScalaFX - How to get the title of a Scene using a method

自作多情 提交于 2021-02-08 18:27:21
问题 I am using ScalaFX and trying to learn how it works. As an exerpiment (not what I will do in production) I want a method that gets the title of a window. So here is my Graph.scala file: package graphing import scalafx.application.JFXApp import scalafx.scene.Scene import scalafx.scene.paint.Color class Graph { val app = new JFXApp { stage = new JFXApp.PrimaryStage { title = "First GUI" scene = new Scene { fill = Color.Coral } } } def getTitle() = { app.stage.getTitle } def generateChart(args:

Scala: normal functions vs tupled functions?

淺唱寂寞╮ 提交于 2021-02-08 14:11:38
问题 What's the difference between these? I know that their type signatures are different, and that all functions start off normal and have to be .tupled to get their tupled form. What's the advantage of using un-tupled (but non-curried) functions? Especially because it seems to me that passing multiple arguments to a tupled function automagically unpacks them anyway, so by all appearances they are the same. One difference i see is that it forces you to have types for every number of function