多重链接的 lambda 表达式?
Multiple chained lambda expression?
我偶然发现了这个 code golf question:
Given a string s
and an integer n
representing an index in s
, output s
with the character at the n
-th position removed.
投票最高的 answer(截至目前 post)在 C# 中。
s=>n=>s.Remove(n,1);
这个多重 =>
语法是什么?它看起来类似于 lambda 表达式 (s,n)=>s.Remove(n,1)
,但我不知道如何使用这段代码。
展开一点就很简单了!
s =>
n =>
s.Remove(n, 1);
让我们调用我们的函数 fn
:使用变量 s
调用 fn
returns 另一个接受变量 n
.[=19= 的函数]
fn("hello")(0)
可以看到在调用fn("hello")
之后,我们得到的其实是这样的:
n => "hello".Remove(n, 1);
所以当我们调用返回的函数时,我们通过给它一个 n
.
来执行它
没有类型定义对我来说似乎不是一个有效的答案:
Func<string, Func<int, string>> f = s => n => s.Remove(n, 1);
string result = f("123")(1); // "13"
我偶然发现了这个 code golf question:
Given a string
s
and an integern
representing an index ins
, outputs
with the character at then
-th position removed.
投票最高的 answer(截至目前 post)在 C# 中。
s=>n=>s.Remove(n,1);
这个多重 =>
语法是什么?它看起来类似于 lambda 表达式 (s,n)=>s.Remove(n,1)
,但我不知道如何使用这段代码。
展开一点就很简单了!
s =>
n =>
s.Remove(n, 1);
让我们调用我们的函数 fn
:使用变量 s
调用 fn
returns 另一个接受变量 n
.[=19= 的函数]
fn("hello")(0)
可以看到在调用fn("hello")
之后,我们得到的其实是这样的:
n => "hello".Remove(n, 1);
所以当我们调用返回的函数时,我们通过给它一个 n
.
没有类型定义对我来说似乎不是一个有效的答案:
Func<string, Func<int, string>> f = s => n => s.Remove(n, 1);
string result = f("123")(1); // "13"