scala

How to split up an Iterator?

情到浓时终转凉″ 提交于 2021-02-04 16:37:23
问题 How to split an iterator into a prefix with duplicates and the rest ? For instance, def splitDupes(it: Iterator[Int]): (Iterator[Int], Iterator[Int]) = ??? val (xs, ys) = splitDupes(List(1, 1, 1, 2, 3, 4, 5).iterator) xs.toList // List(1, 1, 1) ys.toList // List(2, 3, 4, 5) val (xs, ys) = splitDupes(List(1, 2, 3, 4, 5).iterator) xs.toList // List(1) ys.toList // List(2, 3, 4, 5) val (xs, ys) = splitDupes(List(1, 1, 1, 1, 1).iterator) xs.toList // List(1, 1, 1, 1, 1) ys.toList // List() val

Play 2.x @ sign in route definitions

瘦欲@ 提交于 2021-02-04 15:54:45
问题 I'm learning the Play! framework (2.3.x). I am confused about the meaning of the '@' character in front of the controller in route definitions like this: POST /myresource/:id/custom @controllers.MyResource.custom(id: Long) As much as I can infer from the context I've seen this in, it probably has to do with the exact controller implementation (object vs class). Or am I completely wrong in this? I can't find anything in the docs and the code I've seen isn't exactly explanatory. Can anyone

How do you add xml document info with scala.xml?

本秂侑毒 提交于 2021-02-04 14:56:08
问题 First of all: I am aware of anti-xml, and scales, but I would like to use standard scala.xml I prefer to build xml document using explicit methods, not with implicit xml syntax built into Scala Ok, so I have such piece of code: val text = new scala.xml.Text("just a text") val root = new scala.xml.Elem(null,"element",null,scala.xml.TopScope,text) val doc = new scala.xml.Document() doc.docElem = root println(doc.toString()) Almost good but as result I get: <element>just a text</element> and I

Can a method call be instrumented with ByteBuddy instead of the called method?

雨燕双飞 提交于 2021-02-04 14:19:45
问题 I would like to replace some AspectJ code that protects calls to java.lang.System from some user code. java.lang.System cannot/should not be instrumented. With AspectJ the solution is to instrument the calling code like the following example. The code that should be guarded will be instrumented while the code that is allowed is not. @Around("call(public long java.lang.System.currentTimeMillis()) && within(io.someuserdomain..*) && !within(io.someotherdomain..*)) def

Can a method call be instrumented with ByteBuddy instead of the called method?

守給你的承諾、 提交于 2021-02-04 14:19:29
问题 I would like to replace some AspectJ code that protects calls to java.lang.System from some user code. java.lang.System cannot/should not be instrumented. With AspectJ the solution is to instrument the calling code like the following example. The code that should be guarded will be instrumented while the code that is allowed is not. @Around("call(public long java.lang.System.currentTimeMillis()) && within(io.someuserdomain..*) && !within(io.someotherdomain..*)) def

Error compiling Java/Scala mixed project and Lombok

╄→гoц情女王★ 提交于 2021-02-04 13:24:25
问题 I am trying to compile a Maven Java/Scala mixed project that has a Scala class that depends on a Java bean with lombok annotations. I tried adding the lombok jar file to the boot classpath of the Scala compiler as well as the lombok agent, but the compiler still failed to find the generated getters. Is there a way for the Scala compiler to recognize the lombok annotations? If not, what would be a good workaround? Pease note that I am trying to avoid introducing another maven project just for

How to infer inner type of Shapeless record value with unary type constructor?

孤人 提交于 2021-02-04 12:36:52
问题 I'm having trouble understanding the way the Shapeless record Selector interacts with scala's type inference. I'm trying to create a method that can grab a field from a Shapeless record by key, only if the value of the field has a certain unary type constructor, in this particular case Vector[_] , and then grab an inner value of inferred type V out of the Vector , in this case with Vector.apply() . I feel like I'm close. This works, with a concrete inner type of Int : val record = ( "a" ->>

How to read gzip'd file in Scala

我怕爱的太早我们不能终老 提交于 2021-02-04 10:37:06
问题 In Java, I'd wrap a GZIPInputStream over a FileInputStream and be done. How is the equivalent done in Scala? Source.fromFile("a.csv.gz").... fromFile returns a BufferedSource, which really wants to view the world as a collection of lines. Is there no more elegant way than this? Source.fromInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream("a.csv.gz")))) 回答1: If you want to use Source and not do everything the Java way, then yes, you'll have to add one more layer of

Scala UTC timestamp in seconds since January 1st, 1970

女生的网名这么多〃 提交于 2021-02-04 10:33:47
问题 How do I get the Scala UTC timestamp in seconds since January 1st, 1970? 回答1: Same way as you would in Java: val timestamp: Long = System.currentTimeMillis / 1000 回答2: As of Java 8 it's possible to do so like so: import java.time.Instant unixTimestamp : Long = Instant.now.getEpochSecond Via micha's answer here: https://stackoverflow.com/a/24703573/577199 来源: https://stackoverflow.com/questions/11352037/scala-utc-timestamp-in-seconds-since-january-1st-1970

Scala: multiple objects and classes in a single file or each object/class its own file

岁酱吖の 提交于 2021-02-04 09:25:48
问题 I've recently started programming in Scala, coming from Python and Java I was wondering what the correct way or the accepted way is when defining objects/classes in Scala. Scala supports, just like python, to add several class or object definitions in a single file. So purely from an accepted structure perspective, does every object need to be defined in its own file or are you allowed to choose this yourself? 回答1: There is a chapter in the official Scala Style Guide on this. It's pretty