我有这个方法:
def myMethod(value:File,x: (a:File) => Unit) = {
// Some processing here
// More processing
x(value)
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以称之为:
myMethod(new File("c:/"),(x:File) => println(x))
Run Code Online (Sandbox Code Playgroud)
有没有办法用支架称它?就像是:
myMethod(new File("c:/"),{ (x:File) =>
if(x.toString.endsWith(".txt")) {
println x
}
})
Run Code Online (Sandbox Code Playgroud)
或者我是否必须用另一种方法编写并将其传递给myMethod?
我是Maps API中的新手,我正在尝试从2地址获取距离(以公里为单位).这段代码有什么问题?
var mygc = new google.maps.Geocoder();
var locationOrigem;
var locationDestino;
var latOrigem = 0;
var longOrigem = 0;
var latDestino = 0;
var longDestino = 0;
mygc.geocode({'address' : 'Presidente Vargas 897, Centro RJ'}, function(results, status){
locationOrigem = results[0].geometry.location;
latOrigem = results[0].geometry.location.lat();
longOrigem = results[0].geometry.location.lng();
});
mygc.geocode({'address' : 'Abelardo Bueno 3000, Barra da Tijuca RJ'}, function(results, status){
locationDestino = results[0].geometry.location;
latDestino = results[0].geometry.location.lat();
longDestino = results[0].geometry.location.lng();
});
alert(google.maps.geometry.spherical.computeDistanceBetween(locationOrigem, locationDestino));
Run Code Online (Sandbox Code Playgroud)
由于一个未知的原因,var locationOrigem和locationDestino在匿名函数之外是"未定义的"??? 为什么???
javascript distance anonymous-function geolocation google-maps-api-3
我一直在环顾四周,无法找到这方面的例子,我的所有语法摔跤技巧都让我失望.有谁能告诉我如何编译?我的s,s或.s是错误的我想定义一个嵌套函数...
我知道有一个函数可以进行字符串替换,所以我不需要实现这个,但是我正在玩Erlang试图把它拿起来所以我手动旋转我需要使用的一些基础知识. .
replace(Whole,Old,New) ->
OldLen = length(Old),
ReplaceInit = fun(Next, NewWhole) ->
if
lists:prefix(Old, [Next|NewWhole]) -> {_,Rest} = lists:split(OldLen-1, NewWhole), New ++ Rest;
true -> [Next|NewWhole]
end,
lists:foldr(ReplaceInit, [], Whole).
Run Code Online (Sandbox Code Playgroud)
基本上我正在尝试写这个haskell(也可能是坏的,但超出了这一点):
repl xs ys zs =
foldr replaceInit [] xs
where
ylen = length ys
replaceInit y newxs
| take ylen (y:newxs) == ys = zs ++ drop (ylen-1) newxs
| otherwise = y:newxs
Run Code Online (Sandbox Code Playgroud) 我有以下代码:(为了这个问题的目的非常简化,但完美地说明了我遇到的问题)
#!/usr/bin/perl
use strict;
use warnings;
&outer;
my $connected_sub;
sub outer {
print "HELLO\n";
&$connected_sub;
$connected_sub = sub {
print "GOODBYE\n";
}
}
Run Code Online (Sandbox Code Playgroud)
运行时程序会给出此输出和错误:
HELLO
Use of uninitialized value in subroutine entry at subTesting line 13.
Can't use string ("") as a subroutine ref while "strict refs" in use at subTesting.pl line 13.
Run Code Online (Sandbox Code Playgroud)
我完全忽略了什么吗?我无法理解或弄清楚这是什么问题.
试图干掉我写的一些旧的javascript.
测试()
function test() {
var output = function() {
return ajaxPost("test.php", "testvar=bananas");
}
document.getElementById("main").innerHTML = output;
}
Run Code Online (Sandbox Code Playgroud)
ajaxPost()
function ajaxPost(file,stuff) {
var xmlhttp;
var actionFile = file;
var ajaxVars = stuff;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
return xmlhttp.responseText;
} else {
// Waiting...
}
}
xmlhttp.open("POST", actionFile, true);
//Send the proper …Run Code Online (Sandbox Code Playgroud) 完成以下任务的正确方法是什么:
$("#btn").click(function1);
Run Code Online (Sandbox Code Playgroud)
调用函数:
function function1 (event) {
event.preventDefault();
}
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但是我不明白function1如何理解事件参数在没有被传入的情况下引用的内容.这样的监听器设置是否更有意义:
$("#btn").click(function1(event));
Run Code Online (Sandbox Code Playgroud)
这是一个小提琴.
这有效: (1 to 5).reduceLeft( _+_ )
但这不是: (x:Int,y:Int)=>_+_
<console>:8: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
(x:Int,y:Int)=>_+_
^
<console>:8: error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$plus(x$2))
(x:Int,y:Int)=>_+_
^
Run Code Online (Sandbox Code Playgroud)
它是不一致的,因为在第一种情况下,匿名函数(_+_)成功编译,但第二种情况失败.
有没有我错过或误解的东西?或者只是语法定义?
对不起令人困惑的标题; 如果我的某个组件上有onClick属性,如下所示
<Component onClick={this.doSomething()} />
Run Code Online (Sandbox Code Playgroud)
根据doSomething()函数实际调用的内容,我经常会遇到奇怪的错误.如果doSomething()正在改变状态,特别是我得到各种渲染错误.另一方面,如果我这样做
var _this = this;
<Component onClick{
function(){
_this.doSomething()
}
} />
Run Code Online (Sandbox Code Playgroud)
所有错误都消失了,一切都按照我的意图行事.通过将我的onClick属性包装在使其工作的匿名函数中,我到底在做什么?有没有更好的方法来做我想做的事情?
我可以直接在交互式外壳中编写一个匿名函数,如下所示。
iex> total_bottles_milk = fn total -> total * 2 end
iex> total_bottles_milk.(2)
Run Code Online (Sandbox Code Playgroud)
但是,如果我编写一个外部文件并在交互式外壳程序中运行,它将显示“编译错误”。
我的文件名和目录路径是lib / expense.ex
下面是我的代码
defmodule Expense do
total_bread_slices = fn total -> (total * 10) / 100 end
total_bottles_milk = fn total -> total * 2 end
total_cakes = fn total -> total * 15 end
def total_expense(bread_slices, bottles_of_milk, cakes) do
total_bread_slices.(bread_slices) + total_bottles_milk.(bottles_of_milk) + total_cakes.(cakes)
end
end
Run Code Online (Sandbox Code Playgroud)
当我进入文件夹路径并运行iex -S mix以运行Expense模块时,终端显示Compilation错误。
我只是想知道我是否可以直接在交互式外壳程序中运行匿名函数,而不是从外部源进行编译。我想写我作为一等公民的职能。如果有办法,我该怎么办?
Common Lisp宏pushnew返回作为参数给出的(可能已更新)位置列表。看来,如果您想知道给定的项目是否实际被推送,则需要比较放置前和放置后列表以查看其是否已更改。但这对于我的使用来说效率太低,因为它将涉及重复比较两个复杂结构对象的列表以得出等效结果。
另一种可行的替代方法是使用:test参数来pushnew记录是否已找到该项目,因为已扫描列表元素。如果找到该项目,则可以将T作为第二个整数值返回,否则返回NIL。下面的宏尝试执行此操作:
(defmacro pushnew+p (item place &key test (key #'identity) &aux old?)
"Same as pushnew, but also returns whether item was pushed."
`(values (pushnew ,item ,place
:test (lambda (item element)
(if ,test
(setf old? (funcall ,test item element))
(setf old? (eql item element))))
:key ,key)
(not old?)))
Run Code Online (Sandbox Code Playgroud)
似乎工作正常,因为
(defparameter x '(1 2 3))
(pushnew+p 0 x) -> (0 1 2 3), T
(pushnew+p 2 x) -> (0 1 2 3), NIL
(defparameter y '((1) (2) …Run Code Online (Sandbox Code Playgroud) javascript ×4
scala ×2
anonymous ×1
common-lisp ×1
distance ×1
elixir ×1
erlang ×1
geolocation ×1
jquery ×1
jsx ×1
lambda ×1
macros ×1
nested ×1
perl ×1
reactjs ×1
subroutine ×1