Update3: 如果你喜欢这个帖子,请不要赞成我,但请通过下面的DVK赞助天才答案.
我有以下子程序:
use warnings;
#Input
my @pairs = (
"fred bill",
"hello bye",
"hello fred",
"foo bar",
"fred foo");
#calling the subroutine
my @ccomp = connected_component(@pairs);
use Data::Dumper;
print Dumper \@ccomp;
sub connected_component {
my @arr = @_;
my %links;
foreach my $arrm ( @arr ) {
my ($x,$y) = split(/\s+/,$arrm);;
$links{$x}{$y} = $links{$y}{$x} = 1;
}
my %marked; # nodes we have already visited
my @stack;
my @all_ccomp;
for my $node (sort keys %links) {
next if exists …Run Code Online (Sandbox Code Playgroud) 以下内容无法编译。
public class A
{
private readonly int i;
public A()
{
void SetI()
{
i = 10;
}
SetI();
}
}
Run Code Online (Sandbox Code Playgroud)
失败并显示以下错误:
CS0191不能将只读字段分配给它(在构造函数或变量初始化程序中除外)
从技术上讲,由于局部函数的可见性受到限制,我们仍然不在构造函数中,所以我想知道为什么它不能编译。
例如,
public int DoSomething(in SomeType something){
int local(){
return something.anInt;
}
return local();
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器会发出无法在局部函数中使用 some 变量的错误?
我在这里找到了这个代码:
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
return _(); IEnumerable<TSource> _()
{
var knownKeys = new HashSet<TKey>(comparer);
foreach (var element in source)
{
if (knownKeys.Add(keySelector(element)))
yield return element;
}
}
}
Run Code Online (Sandbox Code Playgroud)
起初我不明白这部分return _(); IEnumerable<TSource> _(),但我意识到这是一个在同一行中调用和声明的本地函数。这是在这里完成的。
我的问题是:与简单地内联该代码相比有什么优势吗?
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
if (source == null) …Run Code Online (Sandbox Code Playgroud) https://www.infoworld.com/article/3182416/c-7-in-depth-exploring-local-functions.html
我真的需要确切地知道本地函数应该如何遵循代码。
是 0% IL 代码/MSIL 经验。
public static void Display(string str)
{
int ctr = 5;
DisplayText();
void DisplayText ()
{
for(int i = 0; i < ctr; i++)
Console.WriteLine(str);
}
}
Run Code Online (Sandbox Code Playgroud)
我需要从以前的代码中了解的问题:
Display()是否DisplayText()总是在调用 main 函数时生成Local Function ?或者它在 C# 中创建为一个假的本地函数,但在 MSIL 中它生成为全局函数?编辑:-我认为本地属性(尚不存在或可能......)。有时也很有用。您可以在与本地属性相同的方法中进行一些计算。如上一个示例,但 DisplayText 是属性。希望他们也添加它。
In C# 8.0, Static Local Functions are announced
Can anyone help enlighten me as to why you would want to declare a local function as static?
The reason given in in the article:
to ensure that local function doesn't capture (reference) any variables from the enclosing scope
But:
The example code given in the article is:
int …Run Code Online (Sandbox Code Playgroud) 如您所知,C# 7.0 添加了一些新功能,其中之一是本地函数。我查看了一些使用本地函数的示例和用例,发现了使用它们的两个原因:
1)隐藏函数或方法。原因是:如果该函数不是本地的,其他成员可能会无意中直接使用该函数
2) 使用“Parent”函数的变量
在重构代码的调试过程中,我找不到对 Visual Studio 中本地函数的引用。有对私有函数的引用:
当我调试或重构代码时它很有帮助。在本地函数中我找不到它们:
那么,第一个问题是为什么局部函数不显示摘要注释和参考文献?
有些程序员喜欢使用本地函数,但有些则不喜欢。下面是一个示例(来自C# 7.0 的新增功能 | .NET 博客):
public IEnumerable<T> Filter<T>(IEnumerable<T> source, Func<T, bool> filter)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (filter == null) throw new ArgumentNullException(nameof(filter));
return Iterator();
IEnumerable<T> Iterator()
{
foreach (var element in source)
{
if (filter(element)) { yield return element; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,使用本地函数的原因是:
如果Iterator它是 旁边的私有方法Filter,则其他成员可能会意外地直接使用它(无需参数检查)。此外,它需要采用所有相同的参数,而Filter不是让它们只在范围内
第二个问题是为什么我们要使用局部函数?在这种情况下,我们可以删除本地方法,因为它只使用一次。如果我们担心代码大小或代码责任,我们可以使用区域:
public IEnumerable<T> Filter<T>(IEnumerable<T> …Run Code Online (Sandbox Code Playgroud) 我有一个转换器方法:
MyPoco Convert(dynamic p) => new MyPoco { A = p.X, B = p.Y };
void Test()
{
dynamic item = new { X = 1, Y = 2 };
var poco = (MyPoco)Convert(item);
}
Run Code Online (Sandbox Code Playgroud)
我必须将结果显式转换为 MyPoco,否则 poco 也将成为动态变量。
但是,如果我内联 Convert 方法;
void Test()
{
MyPoco Convert(dynamic p) => new MyPoco { A = p.X, B = p.Y };
dynamic item = new { X = 1, Y = 2 };
var poco = Convert(item);
}
Run Code Online (Sandbox Code Playgroud)
我不需要将 ConvertItem 转换为 MyPoco。这种行为有原因吗?编译器应该很容易知道Convert返回类型是MyPoco,对吧?
我正在尝试通过《Common Lisp:符号计算的温和介绍》一书来学习 Common Lisp 。此外,我正在使用 SBCL、Emacs 和 Slime。
在第 8 章的高级部分,作者介绍了labels特殊功能。实际上,他将在顶层(主函数和辅助函数)上定义事物与label在函数内部使用表达式进行了对比。
例如,这将是一个reverse使用顶级方法进行尾调用的列表函数:
(defun reverse-top-level-helper (xs-left accu)
(cond ((null xs-left) accu)
(t (reverse-top-level-helper (cdr xs-left)
(cons (car xs-left)
accu)))))
(defun reverse-top-level-main (xs)
(reverse-top-level-helper xs nil))
Run Code Online (Sandbox Code Playgroud)
另一方面,下面的代码将使用labels:
(defun reverse-labels (xs)
(labels ((aux-label (xs-left accu)
(cond ((null xs-left) accu)
(t (aux-label (cdr xs-left)
(cons (car xs-left) accu))))))
(aux-label xs nil)))
Run Code Online (Sandbox Code Playgroud)
因此,标签方法避免了人们将顶层的辅助函数搞砸的机会。与顶级方法不同,标签方法可以访问主函数的局部变量。
不幸的是,根据作者的说法,在大多数 lisp 实现中,没有办法跟踪标签表达式中的函数。这似乎是我的情况,因为我是从 REPL 得到的:
CL-USER> (trace aux-label)
WARNING: COMMON-LISP-USER::AUX-LABEL is undefined, …Run Code Online (Sandbox Code Playgroud) 我正在玩本地函数,如果它包含具有相同名称的本地函数,则无法弄清楚如何调用host函数.
class Program
{
static void Main(string[] args)
{
new Test().Foo();
Console.Read();
}
}
class Test
{
public void Foo()
{
Console.WriteLine("Host function");
void Foo()
{
Console.WriteLine("Local function");
}
Foo(); // This calls the local function
Foo(); // I would like to call the host Foo() recursively here
}
}
Run Code Online (Sandbox Code Playgroud)