我有以下课程
class Book implement Borrowable {
@Override
public String toString(Function<? extends Borrowable
, String> format) {
return format.apply(this);
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误,我无法在此(Book对象)上使用"apply".
我目前的格式化程序是
Function<Book, String> REGULAR_FORMAT = book -> "name='" + book.name + '\'' +
", author='" + book.author + '\'' +
", year=" + book.year;
Run Code Online (Sandbox Code Playgroud)
我不想制作该类型的lambda函数
Function<Borrowable, String>
Run Code Online (Sandbox Code Playgroud)
因为我将无法访问未被Borrowable公开的Book成员.
我刚开始学习Scala,在使用流时我在过滤器和takeWhile之间感到困惑.
我遇到了这个程序来生成素数,它在流上使用takeWhile和filter.
lazy val ps: Stream[Int] = 2 #:: Stream.from(3).filter(i =>
ps.takeWhile{j => j * j <= i}.forall{ k => i % k > 0});
Run Code Online (Sandbox Code Playgroud)
在试验时我发现了
Stream.from(1).takeWhile(_ < 10).toList
Run Code Online (Sandbox Code Playgroud)
回报我
List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
Run Code Online (Sandbox Code Playgroud)
而
Stream.from(1).filter(_ < 10).toList
Run Code Online (Sandbox Code Playgroud)
无限地奔跑.