标签: let

理解LISP中的"let"表达

我是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)

lisp common-lisp let

5
推荐指数
1
解决办法
1537
查看次数

Rspec中'let'的范围是什么?

我尝试了以下方法:

  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)

ruby rspec let

5
推荐指数
1
解决办法
2102
查看次数

脚本范围的目的是什么?

在DevTools控制台中检查函数的范围时,我注意到了一个"脚本"范围.一些研究之后,它似乎是要创建letconst变量.

没有constlet变量的脚本中函数的作用域:

全球范围

带有let变量的脚本中函数的作用域:

全局范围和脚本范围

然而,1控制台中的以下打印- 脚本范围中的变量仍然可以从其他脚本访问:

<script>let v = 1</script>
<script>console.log(v)</script>
Run Code Online (Sandbox Code Playgroud)

我听说过ES6模块,其中顶层变量无法从模块外部访问.这是范围用于什么或它有任何其他目的?

javascript scope let ecmascript-6

5
推荐指数
2
解决办法
944
查看次数

更改 rspec 中关联对象的正确方法

我最近开始使用 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)

我究竟做错了什么 ?以及如何解决它?

ruby rspec ruby-on-rails let factory-bot

5
推荐指数
1
解决办法
1742
查看次数

使用let变量而不是使用全局变量

我听说使用全局变量在JavaScript中很糟糕.由于let是块作用域,我可以在包含所有其他函数的块中使用它,并以与全局变量类似的方式使用它吗?

{
  var b = 10;
  let c = 20;
  function foo(){
    return c;
  } 
}
Run Code Online (Sandbox Code Playgroud)

javascript let ecmascript-6

5
推荐指数
1
解决办法
392
查看次数

在JavaScript中让vs var

我知道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)

javascript closures var let

5
推荐指数
1
解决办法
864
查看次数

如何在 HTML 模板中使用 let ?

我正在尝试做一些应该很简单的事情:我想在 *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)

html let angular

5
推荐指数
2
解决办法
7947
查看次数

TypeScript - 函数内没有“变量在分配之前使用”

我想了解以下 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

variables initialization undefined let typescript

5
推荐指数
1
解决办法
1524
查看次数

嵌套上下文中的显式多态注释

在下面的代码中,我不确定我是否理解为什么_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)

polymorphism ocaml types type-inference let

5
推荐指数
1
解决办法
103
查看次数

无法使用 Let 和 Makearray 函数将范围传递给 lambda

回顾这个问题,我希望在数组中创建一个运行总计,但在数组的每一行中重新开始

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)

相同的代码还可以将范围作为参数传递到 …

excel range pass-by-reference let excel-formula

5
推荐指数
1
解决办法
1164
查看次数