我目前正在使用scala进行编码,我认为自己是新手.我有3个不受我控制的课程,这意味着我无法改变它们.
class P
class A {
def m(p: P): A = { println("A.m"); this }
}
class B {
def m(p: P): B = { println("B.m"); this }
}
Run Code Online (Sandbox Code Playgroud)
这是一个简化的例子,实际代码更复杂,类A,B有许多其他类似的方法.
我需要为类A,B的实例调用方法m
明显的解决方案是:
def fill(ab: AnyRef, p: P): Unit = {
ab match {
case a: A => a.m(p)
case b: B => b.m(p)
}
}
Run Code Online (Sandbox Code Playgroud)
但这涉及代码重复.我尝试用鸭子打字解决它,到目前为止,我对这个问题的最佳看法是:
type WithM[T] = { def m(p: P): T }
def fill[S, T <: WithM[S]](ab: T, p: P): S =
ab.m(p)
fill(new A, new P) …Run Code Online (Sandbox Code Playgroud) 我有一个充满 json 文件的目录,其结构如下: file1
{
"products": [
{"f1": "v1"},
{"f1": "v2"}
]
}
Run Code Online (Sandbox Code Playgroud)
文件2:
{
"products": [
{"f1": "v3"},
{"f1": "v4"}
]
}
Run Code Online (Sandbox Code Playgroud)
“.products”的内容与示例中的内容不同,但我们可以假设它们是格式良好的更复杂的 json 值。
我想生成一个具有以下结构的文件:
[
{"f1": "v1"},
{"f1": "v2"},
{"f1": "v3"},
{"f1": "v4"}
]
Run Code Online (Sandbox Code Playgroud)
关于如何使用 jq 执行此操作有什么想法吗?
您好,是否可以检查scala中的方法/函数引用是否等于另一个?例如,编写如下的函数并获得真实?
scala> def m = 1
m: Int
scala> def check(p: () => Int): Boolean = p == m _
check: (p: () => Int)Boolean
scala> check(m _)
res0: Boolean = false
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用clojure.test和deftest宏编写一些测试.
(deftest a-test
(for [item collection]
(is (= (f item) :a-value))))
Run Code Online (Sandbox Code Playgroud)
但我意识到这不是执行此测试的正确方法.我搜索了文档,发现'是'宏,但我不确定如何在这些情况下使用它.
编写此类测试的首选方法是什么?