我需要从方法中检查返回的类型以调用不同的方法.这是代码:
class X ...
class Y ...
...
def getType(input:String) : Option[Class[_]] = {
if ... return Some(classOf[X])
if ... return Some(classOf[Y])
...
}
getType(input) match {
case Some(classOf[X]) => ... // ERROR
case Some(classOf[Y]) => ...
case None => ...
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到了错误:

可能有什么问题?
给出一个地图来查找C {NUMBER} - > STRING模式的元素; 这是我的代码.
val pattern = "C([0-9]+)".r
// find the elements C[0-9]+ format
val plots = smap filter { x =>
x._1 match {
case pattern(r) => true
case _ => false
}
}
Run Code Online (Sandbox Code Playgroud)
我需要使用模式提取元素,但要创建Map [Int,String]的新映射.例如:
Map[String, String]("C1"->"a", "B", "C2"->"c") => Map[Int](1 -> "a", 2 -> "c")
Run Code Online (Sandbox Code Playgroud)
如何在Scala中实现?
我正在尝试构建一个数组数组,以将其作为方法的参数.内部数组的值是任何类型的数据(AnyVal),例如Int或Double.
该方法的签名如下:
def plot[T <: AnyVal](config:Map[String, String], data:Array[Array[T]]): Unit = {
Run Code Online (Sandbox Code Playgroud)
这是代码:
val array1 = (1 to 10).toArray
val array2 = ArrayBuffer[Int]()
array1.foreach { i =>
array2 += (getSize(summary, i))
}
val array3 = new Array[Int](summary.getSize())
val arrays = ArrayBuffer[Array[AnyVal]](array1, array2.toArray, array3) # <-- ERROR1
Gnuplotter.plot(smap, arrays.toArray) # <-- ERROR2
Run Code Online (Sandbox Code Playgroud)
但是,我有两个错误:

可能有什么问题?
在Real World OCaml一书中,我找到了这段代码:
let command =
Command.basic
~summary:"Generate an MD5 hash of the input data"
Command.Spec.(
empty
...
+> anon (maybe_with_default "-" ("filename" %: file))
)
(fun use_string trial filename () ->
Run Code Online (Sandbox Code Playgroud)
我()在最后一行(fun use_string trial filename ())中看到了.
同样来自OCaml中的Print a List,我也在()第一场比赛中看到了.
let rec print_list = function
[] -> ()
| e::l -> print_int e ; print_string " " ; print_list l
Run Code Online (Sandbox Code Playgroud)
那么,()两种情况下的含义是什么?lambda表达式(fun)()在参数列表中有什么作用?
我需要创建一个具有一些值的变量,并将该值提供给 python 脚本一个 liner ( python -c "...") 以处理该变量,并将其恢复为相同或其他变量。
如何使用 Keyboard Maestro 做到这一点?
我可以apply在Scala 中使用来重载()运算符.
class A {
val arr = Array[Int](1,2,3,4,5)
def apply(i:Int) = {
arr(i)
}
}
object Main extends App {
val a = new A
println(a(0))
}
Run Code Online (Sandbox Code Playgroud)
当我设置值时,我可以添加set方法
def set(i:Int, value:Int) = {arr(i) = value}
...
arr.set(3, 10)
Run Code Online (Sandbox Code Playgroud)
Scala是否允许更好的语法糖,例如arr(3) = 10获得相同的结果?
我有instance.get返回值的代码,并根据类型I进行相应处理.
instance.get match {
case v:Range => {
val sizeInBytes = util.conversion.Util.getBytesForBits(v.size)
val value = v.decode(contentByteArray.slice(index, index + sizeInBytes))
index += sizeInBytes
res(key) = value
}
case v:Encoding => {
val sizeInBytes = util.conversion.Util.getBytesForBits(v.size)
val value = v.decode(contentByteArray.slice(index, index + sizeInBytes))
index += sizeInBytes
res(key) = value
}
...
}
Run Code Online (Sandbox Code Playgroud)
在代码中,我有Range和Encoding类型的重复.我如何合并这两个案例?
我尝试了|操作员,但它不起作用.
case v:Range | v:Encoding
Run Code Online (Sandbox Code Playgroud) 当我有一个函数定义make-cd并执行该函数以得到错误的答案时。
(defun make-cd (title artist rating ripped)
'(:title title :artist artist :rating rating :ripped ripped))
(add-record (make-cd "Roses" "Kathy Mattea" 7 t))
(:TITLE TITLE :ARTIST ARTIST :RATING RATING :RIPPED RIPPED)
Run Code Online (Sandbox Code Playgroud)
我本来应该(list ...)得到一个正确的答案。
(defun make-cd (title artist rating ripped)
(list :title title :artist artist :rating rating :ripped ripped))
(add-record (make-cd "Roses" "Kathy Mattea" 7 t))
(:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
我有一个函数a 1)计算(argument+) argument,2)加倍它(解释Haskell monad例子).
a :: Int -> Int
a = (id >>= (\n -> (n+))) >>= (\d -> return (d + d))
Run Code Online (Sandbox Code Playgroud)
在这个函数中,我应该使用return来防止错误.
但是在这个三重函数中,返回((argument+) argument)+ argument)三倍的输入参数.我不能用退货.
triplex :: Int -> Int
triplex = (id >>= (\n -> (n+))) >>= (\d -> (d+))
Run Code Online (Sandbox Code Playgroud)
从这些例子中,我想简单的规则return是1)我们return在返回值时使用,2)return当我们返回部分函数时我们不使用.但我不确定这是正确的理解.那么,returnHaskell 背后有这样的规则吗?
这是C#,F#,IronPython和IronRuby集成的后续问题
为了使用Python的C/C++函数,SWIG是最简单的解决方案.反过来也可以使用Python C API,例如,如果我们有一个python函数,如下所示
def add(x,y):
return (x + 10*y)
我们可以在C中提出使用此python的包装器,如下所示.
double Add(double a, double b)
{
PyObject *X, *Y, *pValue, *pArgs;
double res;
pArgs = PyTuple_New(2);
X = Py_BuildValue("d", a);
Y = Py_BuildValue("d", b);
PyTuple_SetItem(pArgs, 0, X);
PyTuple_SetItem(pArgs, 1, Y);
pValue = PyEval_CallObject(pFunc, pArgs);
res = PyFloat_AsDouble(pValue);
Py_DECREF(X);
Py_DECREF(Y);
Py_DECREF(pArgs);
return res;
}
IronPython/C#甚至F#怎么样?