假设你有一个50000次迭代的循环,并希望从很多矩阵中计算平均值(标量).这不完整,但大致如下:
for k=1:50000
...
mean=sum(sum(matrix))/numel(matrix); %Arithmetic mean
...
end
Run Code Online (Sandbox Code Playgroud)
现在想要包括不同的均值方程式可供选择.首先我尝试了这个:
average='arithmetic'
for k=1:50000
...
switch average
case 'arithmetic'
mean=sum(sum(matrix))/numel(matrix); %Arithmetic mean
case 'geometric'
mean=prod(prod(matrix)).^(1/numel(matrix)); %Geometric mean
case 'harmonic'
mean=numel(matrix)/sum(sum(1./matrix)); %Harmonic mean
end
...
end
Run Code Online (Sandbox Code Playgroud)
这显然比第一个循环慢很多,因为它需要为每次迭代找到匹配的字符串,感觉真的没必要.然后我尝试了这个:
average='arithmetic'
switch average
case 'arithmetic'
eq=@(arg)sum(sum(arg))/numel(arg); %Arithmetic mean
case 'geometric'
eq=@(arg)prod(prod(arg)).^(1/numel(arg)); %Geometric mean
case 'harmonic'
eq=@(arg)numel(arg)/sum(sum(1./arg)); %Harmonic mean
end
for k=1:50000
...
mean=eq(matrix); %Call mean equation
...
end
Run Code Online (Sandbox Code Playgroud)
这仍然是第一个循环的两倍慢,我不明白为什么.最后两个循环的速度几乎相似.
我在这里做错了吗?如何通过这个额外功能实现与第一个循环相同的性能?
非常感谢帮助!
拿一个人为的例子,我想protected static从另一个上下文通过回调函数调用一个方法:
class Foo {
protected static function toBeCalled() { }
public static function bar() {
functionThatAcceptsACallback(function () {
self::toBeCalled();
});
}
}
Run Code Online (Sandbox Code Playgroud)
这在PHP 5.3中是否可行?我找不到让它工作的方法......
在下面的代码示例中,我不明白为什么函数fun可以作为参数传递给方法addAction.该方法fun属于类型Unit,而该方法addAction需要类型的函数() => Unit.
如果fun是类型() => Unit,那么当我尝试添加到操作列表时,为什么编译器会抱怨fun类型?Unitfunactions = fun :: actions
package myscala
object MyScala {
def fun() { println("fun1 executed.") }
def addAction(a: () => Unit) {
actions = a :: actions
}
var actions: List[() => Unit] = List()
def main(args: Array[String]) {
// the following line would produce a compiler error (found: Unit, required: () => Unit), it's OK …Run Code Online (Sandbox Code Playgroud) 我正在研究scala labs的东西,我正在构建一个函数,最终会返回这样的东西:
tails(List(1,2,3,4)) = List(List(1,2,3,4), List(2,3,4), List(3,4), List(4), List())
我通过使用两个函数并在第二个函数上使用一些递归来实现此功能.
def tails[T](l: List[T]): List[List[T]] = {
if ( l.length > 1 )trailUtil(List() ::: List(l))
else List() ::: List(l);
}
def trailUtil[T](l:List[List[T]]) : List[List[T]] = {
if ( l.last.length == 0)l
else trailUtil(l :+ l.last.init);
}Run Code Online (Sandbox Code Playgroud)
这一切都很好,但是我需要两个函数才能做到这一点.我尝试切换:trailUtil(List() ::: List(l))对于匿名函数,但我type mismatch; found :List[List[T]] required:Int从IDE 得到了这个错误.
val ret : List[List[T]] = (ll:List[List[T]]) => {
if ( ll.last.length == 0) ll else ret(ll :+ ll.last.init)
}
ret(List() ::: List(1))Run Code Online (Sandbox Code Playgroud)
有人可以指出我做错了什么,或者更好的做法,这将是伟大的.
(我确实看过 …
我试图在数据框中生成新变量,这些变量以数据框中的两个(或更多)其他变量为条件.我相信R中的循环函数(即lapply,sapply等)对于此目的是有用且有效的.然而,根据我的方法,有些事情是不对的,我无法弄清楚是什么.
M <- data.frame(x=c("A", "A", "B", "B"), y=c(1,2,1,2))
Run Code Online (Sandbox Code Playgroud)
使用这个数据框,我想生成一个新的列z,包含如果是x == "A"和,则为TRUE的逻辑y == 1.以下代码是我能在这里得到的最好的代码,但似乎只是评估我的第一个条件.
M$z <- sapply(M$x, function(x,y) if((x == "A") && (y == 1)) T else F, M$y)
Run Code Online (Sandbox Code Playgroud)
例如,如果我们有一个类似的方法
def find[A](xs: Seq[A], p: A => Boolean): Option[A] = {
xs.foreach(x => if (p(x)) return Some(x));
None;
}
Run Code Online (Sandbox Code Playgroud)
(当然这里有一个库函数,这只是一个例子).foreach当内部函数returns 时,执行如何逃逸?
或者在
def foo(x: AnyRef): String =
process(x match {
case (s: String) => s;
case _ => return "";
})
Run Code Online (Sandbox Code Playgroud)
发出process时执行如何避免运行return ""?
我在关于匿名方法(C#编程指南)的MSDN文档中阅读本文,但我不理解省略参数列表的部分.它说:
有一种情况是匿名方法提供lambda表达式中找不到的功能.匿名方法使您可以省略参数列表.这意味着可以将匿名方法转换为具有各种签名的委托.lambda表达式无法做到这一点.
你能举一个省略匿名方法参数列表的例子吗?
我一直在使用php学习web开发,我对匿名函数有点困惑.特别是关于参数的传递以及它们如何在这样的函数内工作.例如,在代码中
$array = array("really long string here, boy", "this", "middling length", "larger");
usort($array, function($a, $b) {
return strlen($a) - strlen($b);
});
print_r($array);
Run Code Online (Sandbox Code Playgroud)
我真的不明白怎样的参数$a和$b使用.我认为它们是为了比较而对数组进行排序,以确定函数应该如何使用它们并将它们从中取出?
在下一个代码中
$mult = function($x)
{
return $x * 5;
};
echo $mult(2);
Run Code Online (Sandbox Code Playgroud)
我知道参数直接传递给函数并用于返回乘法的结果.
在这篇文章中的例子
$arr = range(0, 10);
$arr_even = array_filter($arr, function($val) { return $val % 2 == 0; });
$arr_square = array_map(function($val) { return $val * $val; }, $arr);
Run Code Online (Sandbox Code Playgroud)
变量$val取自哪里?
我知道也许这并不像看起来那么复杂,但我真的很困惑在这种功能上使用参数
Code with val and var:
val adder: Int => Int = _ + 3 // Works fine
var adder: Int => Int = (_ + 3) // Works fine
var adder: Int => Int = _ + 3 // Error (using var, but not brackets)
Run Code Online (Sandbox Code Playgroud)
Error message for the last line with var:
';' expected but identifier found.
What can explain the difference in behavior between the val and var variant?
我在Q#中有一个用例,其中有一个量子位寄存器qs,需要将CNOT除第一个量子位之外的每个量子位都应用门,并使用第一个作为控制。使用for循环,我可以做到以下几点:
for (i in 1..Length(qs)-1) {
CNOT(qs[0], qs[i]);
}
Run Code Online (Sandbox Code Playgroud)
现在,我想给它一个更实用的味道,并尝试做类似的事情:
ApplyToEach(q => CNOT(qs[0], q), qs[1..Length(qs)-1]);
Run Code Online (Sandbox Code Playgroud)
Q#编译器不接受这样的表达式,通知我它遇到了意外的代码片段。就我的口味而言,这还不太丰富。一些文档声称Q#支持匿名功能a'la C#,因此是上面的尝试。有人可以指出我在Q#中正确使用lambda还是消除我的错误信念?