如何在Scala中按两个字段对列表进行排序,在本例中我将按lastName和firstName排序?
case class Row(var firstName: String, var lastName: String, var city: String)
var rows = List(new Row("Oscar", "Wilde", "London"),
new Row("Otto", "Swift", "Berlin"),
new Row("Carl", "Swift", "Paris"),
new Row("Hans", "Swift", "Dublin"),
new Row("Hugo", "Swift", "Sligo"))
rows.sortBy(_.lastName)
Run Code Online (Sandbox Code Playgroud)
我尝试这样的事情
rows.sortBy(_.lastName + _.firstName)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.所以我很好奇一个好的,简单的解决方案.
如何将java.util.Set [String]转换为Scala 2.8.1中具有泛型类型的scala.collection.Set?
import scala.collection.JavaConversions._
var in : java.util.Set[String] = new java.util.HashSet[String]()
in.add("Oscar")
in.add("Hugo")
val out : scala.collection.immutable.Set[String] = Set(in.toArray : _*)
Run Code Online (Sandbox Code Playgroud)
这是错误信息
<console>:9: error: type mismatch;
found : Array[java.lang.Object]
required: Array[_ <: String]
val out : scala.collection.immutable.Set[String] = Set(javaset.toArray : _*)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
在托管bean中,我有一个int类型的属性.
@ManagedBean
@SessionScoped
public class Nacharbeit implements Serializable {
private int number;
Run Code Online (Sandbox Code Playgroud)
在JSF页面中,我尝试仅为6位数字输入验证此属性
<h:inputText id="number"
label="Auftragsnummer"
value="#{myController.nacharbeit.number}"
required="true">
<f:validateRegex pattern="(^[1-9]{6}$)" />
</h:inputText>
Run Code Online (Sandbox Code Playgroud)
在运行时我得到一个例外:
javax.servlet.ServletException: java.lang.Integer cannot be cast to java.lang.String
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
Run Code Online (Sandbox Code Playgroud)
正则表达式错了吗?或者只是字符串的ValidateRegex?
是否有可能在更实用的Scala风格中更改这个if-else-construct?
def getMIMEType(document: String): String = {
if (document.endsWith(".pdf")) {
return "application/pdf"
} else if (document.endsWith(".dxf")) {
return "application/dxf"
} else if (document.endsWith(".jpg")) {
return "image/jpeg"
} else return "application/octet-stream"
}
Run Code Online (Sandbox Code Playgroud)
我试图使用模式匹配,但它不起作用.所以我很好奇一个好的解决方案.
提前致谢!
猩猩
我正在阅读Haskell的书"为了好大学而学习你的哈斯克尔!".第2章用这个小例子解释了列表理解:
boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]
Run Code Online (Sandbox Code Playgroud)
有人可以在Scala中重写这个列表理解吗?Scala没有偶数或奇数函数?所以我必须使用
x%2!=0
Run Code Online (Sandbox Code Playgroud)
检查数字是否奇数?
提前感谢您提供优雅的解决方案!
猩猩
我以命令式的方式解决了我的问题,但它看起来非常难看.我怎样才能做得更好(更优雅,更简洁,更实用 - 最后是Scala).应跳过与前一行具有相同值但具有不同字母的行,应添加行的所有其他值.
val row1 = new Row(20, "A", true) // add value
val row2 = new Row(30, "A", true) // add value
val row3 = new Row(40, "A", true) // add value
val row4 = new Row(40, "B", true) // same value as the previous element & different letter -> skip row
val row5 = new Row(60, "B", true) // add value
val row6 = new Row(70, "B", true) // add value
val row7 = new Row(70, "B", true) …Run Code Online (Sandbox Code Playgroud) 怎么昨天在Haskell?对于当天 - 当然它的工作原理如下:
date :: IO (Integer,Int,Int) -- :: (year,month,day)
date = getCurrentTime >>= return . toGregorian . utctDay
Run Code Online (Sandbox Code Playgroud)
但对于昨天?这适用于diffUTCTime吗?
用sql:
select current date, current date - 1 day from sysdba.routine
08.11.2018 07.11.2018
Run Code Online (Sandbox Code Playgroud)
但是对于Haskell?
这是我使用 node.js 的第一步。我将观看带有chokidar的目录以获取添加的文件。如果复制过程完成,则应调用脚本。但我不知道如何确定复制过程何时完成以及文件在我的目录中是否完整可用。带有“完成”一词的控制台日志命令永远不会出现。
var fs = require('fs');
var chokidar = require('chokidar');
var watcher = chokidar.watch('/root/Documents/gw/', {ignored: /^\./, persistent: true});
watcher
.on('add', function(path) {
console.log('File', path, 'has been added');
fs.watchFile(path, function(curr, prev) {
if (curr.size == prev.size) {
console.log('finish');
// TODO start a shell script
} else {
console.log(curr.size);
}
});
});
Run Code Online (Sandbox Code Playgroud)
我在 Linux 系统上使用 node.js 版本 0.10.25。
预先感谢您的帮助!
我正在学习 Kotlin,并且我用 Kotlin 编写了以下代码片段。除了使用 if-else 条件之外,还有什么简洁的方法可以编写以下代码吗?
fun test(a: int, b: int): Coding {
return Coding().apply{
if(a>b){
comment = "first value greater than second value"
value = a
}else{
comment = "second value greater than or equal to first value"
value = b
}
}
}
Run Code Online (Sandbox Code Playgroud) scala ×5
haskell ×2
if-statement ×2
javascript ×1
jsf ×1
kotlin ×1
list ×1
node.js ×1
regex ×1
set ×1
sorting ×1
sum ×1
validation ×1