让我们定义f一个支持currying的函数:
def f(a: Int)(b: Int) = a + b
Run Code Online (Sandbox Code Playgroud)
此代码无法编译
def g= f(1)
<console>:10: error: missing arguments for method f;
follow this method with `_' if you want to treat it as a partially applied function
def g= f(1)
Run Code Online (Sandbox Code Playgroud)
我发现了这2个解决方法:
scala> def h = f(1) _
h: Int => Int
scala> def i : Int => Int = f(1)
i: Int => Int
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么推理引擎需要帮助这样一个微不足道的案例呢?
我需要一个按元组列表分组和平均的方法.
这是我目前的实施:
// (a, 1), (a, 2), (b, 3) -> (a, 1.5), (b, 3)
def groupByAndAvg[T, U](ts: Iterable[(T, U)])(implicit num: Numeric[U]): Iterable[(T, Double)] = {
ts.groupBy(_._1)
.mapValues { _.unzip._2 }
.mapValues { xs => num.toDouble(xs.sum) / xs.size }
}
Run Code Online (Sandbox Code Playgroud)
有没有一个更好的方式在性能或简单性?
我想弄清楚什么是最方便的方法(意思是最简洁的)来分割按域分组的URLs(List[String])列表,List[List[String]]其中每个内部列表包含URL来自同一域的s,例如.www.somewhere.net
你将如何面对这项任务?
我想知道a List是否是同质的.
这是我的代码:
def isHomogeneous(ls: List[Any]) = ls.map(_.getClass).toSet.size == 1
有没有更好的办法 ?
我有以下代码:
def a = f(aa)
def b = f(bb)
def c = f(cc)
def d = f(dd)
Run Code Online (Sandbox Code Playgroud)
是否有解决方案来删除样板,即显式调用 f
我有一个不想改变的特质位置。O可以是一个String或Seq[String]
trait Location {
type O
def value: O
}
Run Code Online (Sandbox Code Playgroud)
这是我想要实现的目标:
private val stringLog = Log(new Location {
type O = String
def value = "base"
})
private val seqStringLog = Log(new Location {
type O = Seq[String]
def value = Seq("foo", "bar")
})
println(stringLog.getPath("2020"))
println(stringLog.getPath(List("2018", "2019")))
println(seqStringLog.getPath("2020"))
println(seqStringLog.getPath(List("2018", "2019")))
Run Code Online (Sandbox Code Playgroud)
和预期的结果:(在第一种情况下,我有一个位置和一个日期,所以返回类型可以String代替Seq[String])
base/2020
List(base/2018, base/2019)
List(foo/2020, bar/2020)
List(foo/2018, foo/2019, bar/2018, bar/2019)
Run Code Online (Sandbox Code Playgroud)
我当前的解决方案使用类型投影。我已经看到它可以是一个反模式,它将被删除。有没有更清洁/更好的解决方案?
class Log[L <: Location](location: L)(implicit mapper: Mapper[L#O]) {
def getPath(date: …Run Code Online (Sandbox Code Playgroud) 我只是试图从用户那里读取输入整数
2 2 1 1 1 <作为一个整体
在调试器中,它可以放置每个整数但是何时放置
生成的数组打印出类似[I@19eda2c打印的内容.
public static void main(String[] args) {
int count=0;
int[] array = new int[10];
String input;
Scanner scan = new Scanner(System.in);
System.out.println("Enter up to 10 integers: ");
while(scan.hasNextInt()){
array[count] = scan.nextInt();
count++;
}
System.out.println(array);
}
}
Run Code Online (Sandbox Code Playgroud)
我现在明白它需要用for循环或toString方法打印
但是当我运行代码时,我意识到
即使用户输入整数,程序也会等待我
我的扫描仪物流不正确?