我有一个方法需要以许多可能的方式对集合进行排序.myCollection.sortedBy我不想多次调用,而是想sortedBy在变量中传递lambda ,然后我将该变量传递给一次调用sortedBy.但是,我无法弄清楚lambda变量应具有的类型.请记住,"按字段排序"可能有不同的类型(但Comparable显然它们都是).
这是我的问题的简化版本:
data class Person(val name: String, val age: Int)
enum class SortBy {
NAME, AGE
}
fun main(args: Array<String>) {
val people = listOf(Person("John", 30), Person("Mary", 20))
val sort = SortBy.NAME
val comparator = when (sort) {
SortBy.NAME -> { p: Person -> p.name }
SortBy.AGE -> { p: Person -> p.age }
}
// the line below won't compile
people.sortedBy(comparator).forEach(::println)
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我想Error Codes在一个包中定义我的models.
error.go
package models
const{
EOK = iota
EFAILED
}
Run Code Online (Sandbox Code Playgroud)
我怎么能在另一个包中使用它们而不用它们作为models.EOK.我想直接使用EOK,因为这些代码在所有包中都是通用的.
这是正确的方法吗?有更好的选择吗?
如果我make像这样跑:
make VAR=dir
Run Code Online (Sandbox Code Playgroud)
有没有办法将VAR变量指向的位置添加为目标依赖项?实际上,我需要在该目录中定义一个文件作为依赖项.
我会去:
target: $(VAR)/file.txt
echo yes
Run Code Online (Sandbox Code Playgroud)
但是如果没有定义变量,目标就会理解/file.txt,这不是我想要的.我还想过创建一个虚假的目标来检查变量,test但是每次都会执行虚假目标,因此target也会这样做.
任何解决方案?
我正在编写一个包含以下代码的XML文件:
Source source = new DOMSource(rootElement);
Result result = new StreamResult(xmlFile);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
Run Code Online (Sandbox Code Playgroud)
这是输出文件:
<?xml version="1.0" encoding="UTF-8"?>
<feature-sequences>
<sequence>
<initial-frame>0</initial-frame>
<points>
<point>
<x>274.0</x>
<y>316.0</y>
</point>
...
Run Code Online (Sandbox Code Playgroud)
我希望这个文件缩进,例如:
<?xml version="1.0" encoding="UTF-8"?>
<feature-sequences>
<sequence>
<initial-frame>0</initial-frame>
<points>
<point>
<x>274.0</x>
<y>316.0</y>
</point>
...
Run Code Online (Sandbox Code Playgroud)
setOutputProperty在我的代码中调用并没有解决问题,它实际上使文本用新行(但不缩进).
任何人都有解决方案,而不需要外部库?
collections ×1
dependencies ×1
formatting ×1
go ×1
indentation ×1
java ×1
kotlin ×1
lambda ×1
makefile ×1
sorting ×1
target ×1
xml ×1