我正在尝试在 LINQ 中执行一个方法
var result = from fruit in fruits
let type = GetType(fruit)
select new {
fruit = fruit,
type = type != null ? type.Name : "N/A"
};
FruitType GetType(Fruit fruit)
{
if (fruit == a)
return TypeA;
else
return null;
}
Run Code Online (Sandbox Code Playgroud)
这会引发错误,因为: if resultis null,即使在检查之后也let不允许访问。type.Namenot null
有什么解决方法吗?
当我尝试使用以下命令编译此代码时rustc main.rs:
macro_rules! getPI {
let pi = 3.141592;
println!("Pi is roughly 3.142 \n {0}", pi);
}
fn main() {
print!(getPI);
}
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误:
macro_rules! getPI {
let pi = 3.141592;
println!("Pi is roughly 3.142 \n {0}", pi);
}
fn main() {
print!(getPI);
}
Run Code Online (Sandbox Code Playgroud)
我对编程很陌生,希望有人能提供解决方案。
在我看来,我已经理解了 JavaScript 作用域和提升的基本概念。在这方面,这个问题+答案对我帮助很大。
\n\n不过,最近我遇到了一些让我有点惊讶的事情。考虑以下代码:
\n\nvar a = 1;\n\nif (true) {\n console.log(a);\n let a = 2;\n}\n\nconsole.log(a);\nRun Code Online (Sandbox Code Playgroud)\n\n鉴于我所学到的知识,我希望它能够输出undefined和1。虽然它的结果是Uncaught ReferenceError: a is not defined.
我的理解是,上面的代码应该相当于(因为声明部分let a = 2;应该被提升到最近的封闭块 \xe2\x80\x94 if,在本例中是 ):
var a = 1;\n\nif (true) {\n let a;\n console.log(a);\n a = 2;\n}\n\nconsole.log(a);\nRun Code Online (Sandbox Code Playgroud)\n\n顺便说一下,这段代码产生了undefined和1,正如我所期望的那样。
我的问题是:
\n\nlet在最近的封闭块内使用提升声明?\n\nUncaught ReferenceError: a is not defined?我有一个像下面这样的函数,其中 isformValid 被标记为一个 let 并在 if 块中使用它并根据条件更改它的值。
validateForm(validationErrors, formData) {
let validationRules = this.state.dynamicJourneyData[this.state.currentStepName].validationRules;
let isFormValid = true;
let fullErrorList = [];
validationRules.rules.forEach((rule) => {
let errorList = this.evaluateRule(rule, formData);
if (errorList.length > 0) {
fullErrorList = fullErrorList.concat(errorList);
}
});
let finalErrorList = [];
let errorKeys = [];
fullErrorList.filter((error) => errorKeys.indexOf(error.id) < 0).forEach((error) => {
finalErrorList.push(error);
errorKeys.push(error.id);
});
if (finalErrorList.length > 0) {
isFormValid = false;
if (finalErrorList.length === 1) {
validationErrors.messageTitle = validationErrors.messageTitle
.replace('@count', finalErrorList.length)
.replace('were', 'was')
.replace('errors', 'error'); …Run Code Online (Sandbox Code Playgroud) 我的 JavaScript 书“JavaScript The Definitive Guide,第 6 版”,第 270 页包含以下文本和代码:
“...在 for 循环中,初始化表达式在新变量的范围之外进行评估”
let x = 1;
for (let x = x + 1; x < 5; x++) {
console.log(x); // prints 2, 3, 4
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行上述代码(在最新版本的 Chrome 和 FF 中)时,出现控制台错误:
参考错误:x 未定义
初始化前无法访问词法声明“x”
书上的代码不对吗?(这本书的勘误网站上没有任何关于:这个的内容。)
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block(this)
}
Run Code Online (Sandbox Code Playgroud)
并具有如下功能:
fun getType() : String? {
val type = mContent.let {
if (!TextUtils.isEmpty(it) && it == "TYPE_1") {
return "TYPE_A" . //where it returns to, as the result of the let{}, or as return value to exit the fun getType()?
}
else {
return it
}
}
if (type == "TYPE_A") {
return getType_A()
}
return type
}
Run Code Online (Sandbox Code Playgroud)
let go{} …
在这种对letlambda 演算版本的处理let中给出
(\f.z)(\x.y)
Run Code Online (Sandbox Code Playgroud)
用词
ff x = y 在表达式中 由 定义z,然后作为
let f x = y in z
Run Code Online (Sandbox Code Playgroud)
我从初学者的角度知道 Haskell 是如何let工作的,即定义遵循let并且表达式(对这些定义做一些事情)遵循in.
let <definitions> in <expression>
Run Code Online (Sandbox Code Playgroud)
但是这种最一般的 lambda 演算描述令人困惑。例如,我假设可能有一个 Haskell lambda 函数版本的let f x = y in z. 有人可以在 Haskell 中写出这个 - 也许举一两个例子 - 吗?只是猜测,似乎第一个 lambda 函数需要第二个 lambda 函数——不知何故?
(\x -> y)(\f -> z)
Run Code Online (Sandbox Code Playgroud)
但这只是一个猜测。
我想使用 hspec 创建一些具有不同值的测试。我写了下面的代码,它不能编译但给出了我的目标:
spec :: Spec
spec = do
describe "productOneLine" $ do
let
inVector = Data.Vector.replicate 0 0
inInteger = 3
outVector = Data.Vector.replicate 1 0
in
it "must manage empty vector" $ productOneLine inVector inInteger `shouldBe` outVector
let
inVector = Data.Vector.fromList [2, 4, 5]
inInteger = 4
outVector = Data.Vector.fromList [9, 6, 1, 2]
in
it "must multiply a vector by an integer" $ productOneLine inVector inInteger `shouldBe` outVector
Run Code Online (Sandbox Code Playgroud)
如何为每个以 开头的 ligne创建不同的inVector, inIntegeret集?outVectorit
我正在阅读Paul Graham 的On Lisp,试图更好地理解编程的函数式风格。在第 3 章中,他提到函数式程序“通过返回值来工作”,而不是产生副作用。我不太明白该声明对如何操作和表示过程的中间结果的影响。如果函数式程序返回一个值,那么使用捕获它(let)是否等同于使用(lambda)函数?
下面的示例显示了函数 的定义,(group)使用 lambda 函数捕获中间结果。(group)接受一个列表src和一个数字n,并将列表元素细分为k大小为 的组n。
(defun group (src n acc)
(funcall
#'(lambda (back)
(cond
((consp back)
(group back n (cons (subseq src 0 n) acc)))
(t
(reverse (cons src acc)))))
(nthcdr n src)))
Run Code Online (Sandbox Code Playgroud)
该函数的最后一行通过获取(nthcdr n src). 然后,这个结果作为参数传递给 lambda 函数,该函数决定如何src根据参数进行处理。这是纯粹的功能代码,没有副作用。另一方面,我可以用(group)以下方式定义:
(defun group (src n acc)
(let ((back (nthcdr n src)))
(cond …Run Code Online (Sandbox Code Playgroud) 这是原始功能
[let square x = x * x in (square 5, square 3, square 2)]
Run Code Online (Sandbox Code Playgroud)
我试图从上面的行中删除let,但它不起作用.
[square x = x * x in (square 5, square 3, square 2)]
<interactive>:21:11: error:
parse error on input ‘=’
Perhaps you need a 'let' in a 'do' block?
e.g. 'let x = 5' instead of 'x = 5'
Run Code Online (Sandbox Code Playgroud)
为什么有必要在那里?
let ×10
haskell ×3
javascript ×3
ecmascript-6 ×2
.net ×1
asp.net-mvc ×1
c# ×1
common-lisp ×1
do-notation ×1
eslint ×1
kotlin ×1
lambda ×1
linq ×1
on-lisp ×1
reactjs ×1
rust ×1
scope ×1
syntax ×1
var ×1