我在下面编写了下面的Scala代码来处理我传入的String,格式化String,将其附加到a StringBuilder
并将String
带有转义的unicode 的格式化返回给我的调用者以进行其他处理.
Scala编译器在存在String.format
以下错误的调用行时抱怨以下内容:
带备选方法的重载方法值格式:
(x$1; java.util.Locale; x$2: String, X$3: Object*)
(x$1:String,x$2: Object*)
无法应用字符串(*String, Int)
class TestClass {
private def escapeUnicodeStuff(input: String): String = {
//type StringBuilder = scala.collection.mutable.StringBuilder
val sb = new StringBuilder()
val cPtArray = toCodePointArray(input) //this method call returns an Array[Int]
val len = cPtArray.length
for (i <- 0 until len) {
if (cPtArray(i) > 65535) {
val hi = (cPtArray(i) - 0x10000) / 0x400 + 0xD800
val lo = (cPtArray(i) …
Run Code Online (Sandbox Code Playgroud) 这是一个以流方式解析JSON结构的程序.这是晚上10点,我的大脑很累,这就是为什么我似乎无法弄清楚为什么我得到异常:java.io.Exception:Stream关闭.
如果这里的专家可以帮助我摆脱这个例外,我会感谢帮助我在这个夜晚解决这个问题.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import java.util.Properties;
public class JacksonStreaming {
public static void main(String[] args) {
System.out.println("Entered Main");
try {
new JacksonStreaming().getNames();
} catch (Exception e) {
e.printStackTrace();
}
}
ObjectMapper jsonMapper = new ObjectMapper();
JsonFactory jsonFactory = new JsonFactory();
Properties prop = new Properties();
String filePath = "";
final String[] path = new String[] {"myReport", "docReports", "part1/.", "myAnalysis", "matches", "name"};
void getNames() {
System.out.println("Entered getNames");
//final String …
Run Code Online (Sandbox Code Playgroud) 我基于相应的Java类编写了以下Scala类:结果不好.它仍然看起来像Java一样,充满了vars,很长,在我看来并不是惯用的Scala.
我希望缩小这段代码,消除变量和@BeanHeader的东西.
这是我的代码:
import scala.collection.immutable.Map
class ReplyEmail {
private val to: List[String] = List()
private val toname: List[String] = List()
private var cc: ArrayList[String] = new ArrayList[String]()
@BeanProperty
var from: String = _
private var fromname: String = _
private var replyto: String = _
@BeanProperty
var subject: String = _
@BeanProperty
var text: String = _
private var contents: Map[String, String] = new scala.collection.immutable.HashMap[String, String]()
@BeanProperty
var headers: Map[String, String] = new scala.collection.immutable.HashMap[String, String]()
def addTo(to: String): ReplyEmail = …
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚另一个程序员写的Scala中的自定义迭代器.我无法理解函数声明.它们对我来说看起来像匿名函数,但我根本无法完全绕过它们.
我做了一些关于Scala中的匿名函数的阅读,我发现这个资源[ http://www.scala-lang.org/old/node/133]很有帮助,但我仍然无法阅读上述函数并完全理解它们.
这是代码:
class MyCustomIterator(somePath: Path, someInt: Int, aMaxNumber: Int) {
def customFilter:(Path) => Boolean = (p) => true
// Path is from java.nio.files.Path
def doSomethingWithPath:(Path) => Path = (p) => p
}
Run Code Online (Sandbox Code Playgroud)
我想了解这些了解这些功能.什么是回归类型?这个功能的主体是什么?
.
我试图分析下面的Scala代码:
import java.nio.file._
import scala.Some
abstract class MyCustomDirectoryIterator[T](path:Path,someNumber:Int, anotherNum:Int) extends Iterator[T] {
def getCustomIterator(myPath:Path):Option[(DirectoryStream[Path],
Iterator[Path])] = try {
//we get the directory stream
val str = Files.newDirectoryStream(myPath)
//then we get the iterator out of the stream
val iter = str.iterator()
Some((str -> iter))
} catch {
case de:DirectoryIteratorException =>
printstacktrace(de.getMessage)
None
}
Run Code Online (Sandbox Code Playgroud)
我如何Some((str -> iter))
插入这段代码:是的,它返回一个类型的值:
Option[(DirectoryStream[Path], Iterator[Path])]
Run Code Online (Sandbox Code Playgroud)
根据我的理解, - >运算符是scala.Predef包中的ArrowAssoc.
implicit final class ArrowAssoc[A] extends AnyVal
Run Code Online (Sandbox Code Playgroud)
但我仍然不明白 - >为了给我一个类型的返回值做了什么:
Option[(DirectoryStream[Path], Iterator[Path])]
Run Code Online (Sandbox Code Playgroud)
这里的Scala专家能否为此提供更多信息?有没有办法以更易读的方式编写"Some(..)"的东西?不过,我确实理解了Some扮演的角色.
我仍然习惯了 Scala 中 PartialFunction 的概念。我遇到了以下我无法理解的陈述:
更新: def msgsToGet: PartialFunction[Any, Unit] 在 trait 中定义如下:
trait MyActorTrait {
palindrome: String => def msgsToGet: PartialFunction[Any, Unit]
//After the answers I received, I understand that the "def msgsToGet: //PartialFunction[Any, Unit]" represents the LHS of an abstract (as yet) //unimplemented function (no function body yet)
}
Run Code Online (Sandbox Code Playgroud)
这是什么意思?我知道“def”表示一个函数。在这种情况下,它是一个函数 name msgsToGet
。
然后是一个冒号 (:)。好吧,直到现在,我一直认为“在冒号之后,是类型,这就是我迷路的地方。好吧,是不是和:一样的东西:
def msgsToGet(): PartialFunction[Any, Unit]
Run Code Online (Sandbox Code Playgroud)
[一个不接受任何参数并返回类型的函数PartialFunction[Any, Unit]
。
PartialFunction[Any, Unit]
以某种方式在我看来就像一个返回类型。但是没有函数体。那请问这里是怎么回事?
这是一个更长但可读的语法糖吗?
请帮我解释一下。。
我的标题可能没有描述我试图理解的问题代码:
这是一段代码:
def getMeConcurrentInputStream[A, I <: InputStream](in:I)(fn:I => A):Future[A] = {
future {
fn(in)
}andThen {
case all => in.close()
}
}
Run Code Online (Sandbox Code Playgroud)
我试图了解该功能的用途.这是什么:
[A, I <: InputStream](in:I)(fn:I => A)
Run Code Online (Sandbox Code Playgroud)
这是什么: (in:I)(fn:I => A)
而功能正在回归未来?我怎么解释:Future[A]
我如何解释以上所有内容?如何通过从代码中的其他位置调用它来使用此函数?