我是lisp的新手,之前有过函数式编程(Haskell,SML)的经验.为什么这段代码会返回14,而不是10(即1 + 2y + 3 + 1)?
(defvar x 1)
(defun g (z)
(+ x z))
(defun f (y)
(+ (g 1)
(let ((x (+ y 3)))
(g (+ y x)))))
(f 2)
Run Code Online (Sandbox Code Playgroud) 我尝试了以下方法:
describe "#check_recurring_and_send_message" do
let(:schedule) {ScheduleKaya.new('test-client-id')}
context "when it is 11AM and recurring event time is 10AM" do
schedule.create_recurring_event('test-keyword', 'slack', 'day', '10 AM')
it "sends an SMS" do
end
it "set the next_occurrence to be for 10AM tomorrow" do
tomorrow = Chronic.parse("tomorrow at 10AM")
expect(schedule.next_occurrence).to eq(tomorrow)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我在范围内遇到错误:
`method_missing': `schedule` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that …Run Code Online (Sandbox Code Playgroud) 在DevTools控制台中检查函数的范围时,我注意到了一个"脚本"范围.一些研究之后,它似乎是要创建let和const变量.
没有const或let变量的脚本中函数的作用域:
带有let变量的脚本中函数的作用域:
然而,1控制台中的以下打印- 脚本范围中的变量仍然可以从其他脚本访问:
<script>let v = 1</script>
<script>console.log(v)</script>
Run Code Online (Sandbox Code Playgroud)
我听说过ES6模块,其中顶层变量无法从模块外部访问.这是范围用于什么或它有任何其他目的?
我最近开始使用 rspec 进行测试,所以我可能会犯错误,如果有更好的方法请纠正我
我创建了两个相关模型
let(:user) {FactoryGirl.create :user}
let!(:participation) {FactoryGirl.create :participation, user: user}
Run Code Online (Sandbox Code Playgroud)
在其中一项测试更改相关对象之一之前
context "when" do
before {participation.prize = 100}
it "" do
binding.pry
end
end
Run Code Online (Sandbox Code Playgroud)
但里面
participation.prize => 100
user.participatons.select(:prize) => nil
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?以及如何解决它?
我听说使用全局变量在JavaScript中很糟糕.由于let是块作用域,我可以在包含所有其他函数的块中使用它,并以与全局变量类似的方式使用它吗?
{
var b = 10;
let c = 20;
function foo(){
return c;
}
}
Run Code Online (Sandbox Code Playgroud) 我知道let具有块作用域,而var具有功能作用域。但是我不明白在这种情况下,如何使用let解决问题
const arr = [1,2,3,4];
for (var i = 0; i < arr.length; i++) {
setTimeout(function() {
console.log(arr[i])
}, 1000);
} // Prints undefined 5 times
const arr = [1,2,3,4];
for (let i = 0; i < arr.length; i++) {
setTimeout(function() {
console.log(arr[i])
}, 1000);
} // Prints all the values correctly
Run Code Online (Sandbox Code Playgroud) 我正在尝试做一些应该很简单的事情:我想在 *ngFor 中执行一个函数。该函数返回一个对象。我想在一种“let”语句中设置该对象,这样我就可以在 HTML 中使用它的属性:
<div *ngFor="let productGroup of getproductGroupBySomeVariable(variable)">
<!-- This is where I need to set a variable with the output of
the getProductBySomeProperty function-->
<div *ngLet="'{{getProductBySomeProperty(productGroup.someproperty)}}'
as myVar" class="ui-g">
<!-- Here I want to use and display the properties of the
object created by the function above-->
<span>{{myVar.property1}} </span>
<span>{{myVar.property2}} </span> etc....
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我想了解以下 TypeScript 行为:
下面的代码
let a: number
if (a === undefined) {
console.log("how?")
}
Run Code Online (Sandbox Code Playgroud)
抛出错误:“变量'a'在分配之前被使用。”。
但是下面的代码
let a: number
const f = (): void => {
if (a === undefined) {
console.log("how?")
}
}
f()
Run Code Online (Sandbox Code Playgroud)
工作正常并记录“如何?”。
这是为什么?而且,a === undefined如果它的类型是 ,那又如何呢number?
在下面的代码中,我不确定我是否理解为什么_nested2.
这是否意味着只有顶级定义才能概括其推断类型以匹配显式多态签名?
let toplevel1 : 'a. 'a -> int = (fun _ -> 0)
let toplevel2 : 'a. 'a -> int = (fun (_ : 'a) -> 0)
let nest1 =
let _nested1 : 'a. 'a -> int = (fun _ -> 0) in 0
let nest2 =
let _nested2 : 'a. 'a -> int = (fun (_ : 'a) -> 0) in 0
(* ^^^^^^^^^^^^^^^^^^^
Error: This definition has type 'a -> int which is less general …Run Code Online (Sandbox Code Playgroud) 回顾这个问题,我希望在数组中创建一个运行总计,但在数组的每一行中重新开始
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
导致
| 1 | 3 | 6 | 10 |
| 5 | 11 | 18 | 26 |
一种解决方案是使用 Makearray,效果很好:
=MAKEARRAY(
2,
4,
LAMBDA(r, c,
SUM(
INDEX(sheet1!A1:D2, r, 1) : INDEX(sheet1!A1:D2, r, c)
)
)
)
Run Code Online (Sandbox Code Playgroud)
不过,我现在希望使用 Let 语句更普遍地编写此内容:
=LET(
range, Sheet1!A1:D2,
MAKEARRAY(
rows(range),
Columns(range),
LAMBDA(r, c,
SUM(INDEX(range, r, 1) : INDEX(range, r, c))
)
)
)
Run Code Online (Sandbox Code Playgroud)
但这会导致
这个非常相似的公式可以正常工作(它不应该解决原始问题,而只是测试是否可以将范围传递到 Let 语句内的 lambda):
=LET(
range, Sheet1!A1:D2,
SCAN(0, range, LAMBDA(a, c, a + c + INDEX(range, 1, 1)))
)
Run Code Online (Sandbox Code Playgroud)
相同的代码还可以将范围作为参数传递到 …
let ×10
javascript ×3
ecmascript-6 ×2
rspec ×2
ruby ×2
angular ×1
closures ×1
common-lisp ×1
excel ×1
factory-bot ×1
html ×1
lisp ×1
ocaml ×1
polymorphism ×1
range ×1
scope ×1
types ×1
typescript ×1
undefined ×1
var ×1
variables ×1