我有一个Haskell记录
data User = User
{ email :: Text
, token :: Text
}
Run Code Online (Sandbox Code Playgroud)
我想在任何输入JSON中忽略"token"的值.例如,如果输入JSON是
{
"email": "foo@bar.com",
"token": "abc123"
}
Run Code Online (Sandbox Code Playgroud)
我希望生成的User对象包含User { email = "foo@bar.com", token = "" }.
我定义了以下内容:
instance FromJSON User where
parseJSON (Object v) =
User <$> v .: "email"
<*> v .:? "" .!= "" -- token
parseJSON _ = fail "Invalid User"
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来设置token字段的值v .:? "" .!= "",忽略"token": "abc123"JSON中的?
我试过了
instance FromJSON User where
parseJSON …Run Code Online (Sandbox Code Playgroud) 在榆树中,如果我有匿名功能
(\f x -> f x)
Run Code Online (Sandbox Code Playgroud)
我可以简化为
(<|)
Run Code Online (Sandbox Code Playgroud)
对于参数为另一个函数的参数的两参数函数,可以这样做吗?
(\x y -> f x y |> g)
Run Code Online (Sandbox Code Playgroud)
我以为我可以用
(f |> g)
Run Code Online (Sandbox Code Playgroud)
但是编译器抱怨类型。
具体来说,在我的update函数的一种情况下,我有类似以下内容:
let
msgNames = [Foo, Bar]
values = ["f", "b"] // These values are actually derived
// by a more complicated operation
model_ = List.map2 (<|) msgNames values
|> List.foldl (\msg mod -> update msg mod |> Tuple.first)
model
in
( model_, Cmd.none )
Run Code Online (Sandbox Code Playgroud)
我试图简化匿名函数参数List.foldl喜欢的东西(update |> Tuple.first),但我从编译器以下错误:
The right side of (|>) …Run Code Online (Sandbox Code Playgroud) 我试图在tomcat-users.xml中使用散列测试用例.(我计划实现其中一个Realm类的子类,以便通过审计,日志记录等进行真正的身份验证.)我运行了命令
$TOMCAT_HOME/bin/digest.sh -a sha secret
Run Code Online (Sandbox Code Playgroud)
结果'秘密:e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4'.我把它粘贴到了
<user password="e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4" roles="test" username="tester"/>
Run Code Online (Sandbox Code Playgroud)
线.我在web.xml中添加了相应的魔术字,以便对servlet使用DIGEST身份验证(role = test),但是当我尝试登录时,我收到401错误.
我用wireshark"观察"了这些交易,似乎浏览器正在发送所有正确的响应.
我这样做了吗?在我看来,摘要认证将发送回MD5("用户名:realm:密码"),因此tomcat无法将存储在tomcat-users.xml文件中的值与浏览器发送的值进行比较,因为它需要从"tomcat-users.xml"或"username:realm:password"中"取消"密码值.
我应该存储"用户名:域名:密码"的哈希值吗?
我正在使用Ant和Ivy构建一个项目.该build.xml文件依赖于ant-contrib,bean脚本框架,beanshell和commons-logging.
Ant在几个地方搜索库,包括${user.home}/.ant/lib.
文件中是否有任何方法build.xml可以让这些库自动下载并安装在${user.home}/.ant/lib目录中(如果它们尚未存在),也许使用Ivy本身?
谢谢,拉尔夫
可能重复:
语法糖:_*
我写了一个函数,它传递了一个格式字符串(对于String.format(...))和一个varargs参数数组(以及其他东西).该方法如下所示:
def myMethod(foo: Number, formatStr: String, params: Any*): Unit = {
// .. some stuff with foo
println(formatStr, params.map(_.asInstanceOf[AnyRef]) : _*)
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到了params参数的语法.有用!但是怎么样?我不理解第二个参数的语法println,尤其是结尾部分(: _*).显然,调用map并将数组扩展为AnyRefs 序列.
我在Clojure -main中的:gen-class :main true命名空间中定义了一个函数.我试图从REPL测试它.
我的main功能看起来像这样:
(defn -main [& args]
; ...
)
Run Code Online (Sandbox Code Playgroud)
我试图与调用它(ns/-main "-x" "foo"),(ns/-main "-x foo"),(ns/-main ["-x" "foo"]),(ns/-main (into-array String ["-x" "foo"]),等,以及所有给我的各种错误.
如何从REPL调用此函数并传入一些命令行参数来测试它?
谢谢.
我需要并行处理多个数据值("SIMD").我可以使用java.util.concurrentAPI(Executors.newFixedThreadPool())使用Future实例以并行方式处理多个值:
import java.util.concurrent.{Executors, Callable}
class ExecutorsTest {
private class Process(value: Int)
extends Callable[Int] {
def call(): Int = {
// Do some time-consuming task
value
}
}
val executorService = {
val threads = Runtime.getRuntime.availableProcessors
Executors.newFixedThreadPool(threads)
}
val processes = for (process <- 1 to 1000) yield new Process(process)
val futures = executorService.invokeAll(processes)
// Wait for futures
}
Run Code Online (Sandbox Code Playgroud)
如何使用Actors做同样的事情?我不相信我想把所有进程"提供"给一个actor,因为actor会依次执行它们.
我是否需要使用"调度程序"actor创建多个"处理器"角色,该角色向每个"处理器"角色发送相同数量的进程?
我有以下Scala类:
case class Person(firstName: String, lastName: String, age: Int)
extends Ordered[Person] {
def compare(that: Person): Int = {
if (this.lastName < that.lastName) -1
else if (this.lastName > that.lastName) 1
else if (this.firstName < that.firstName) -1
else if (this.firstName > that.firstName) 1
else this.age compare that.age
}
}
Run Code Online (Sandbox Code Playgroud)
允许按lastName,firstName和age排序.
如何使用模式匹配来编写?我想出了以下内容,但有更好的方法吗?
case class Person(firstName: String, lastName: String, age: Int)
extends Ordered[Person] {
def compare(that: Person): Int = {
that match {
case Person(_, thatLastName, _) if this.lastName < thatFile => -1
case …Run Code Online (Sandbox Code Playgroud) 在Scala中,我可以创建一个带有多个参数列表的方法:
def myMethod(value: Int)(fn: (Int) => Unit) {
fn(value)
}
Run Code Online (Sandbox Code Playgroud)
并称之为:
myMethod(10) { value => println(value) }
Run Code Online (Sandbox Code Playgroud)
如何使用类构造函数执行相同的操作?我怎么称呼它?