每当我使用lambda表达式时,我都会得到这个pep8警告.是不是建议使用lambda表达式?如果不是为什么?
The C++ Standard Library (Second Edition)Nicolai Josuttis 在其着作中指出,编译器可以比普通函数更好地优化lambdas.
此外,C++编译器比普通函数更好地优化lambdas.(第213页)
这是为什么?
我认为在内联时不应该有任何差别.我能想到的唯一原因是编译器可能有一个更好的本地上下文与lambdas,这样可以做出更多假设并执行更多优化.
当我使用Java 8的新语法糖迭代一个集合时,例如
myStream.forEach(item -> {
// do something useful
});
Run Code Online (Sandbox Code Playgroud)
这不等同于下面的"旧语法"片段吗?
myStream.forEach(new Consumer<Item>() {
@Override
public void accept(Item item) {
// do something useful
}
});
Run Code Online (Sandbox Code Playgroud)
这是否意味着Consumer每次迭代集合时都会在堆上创建一个新的匿名对象?这需要多少堆空间?它有什么性能影响?这是否意味着我在迭代大型多级数据结构时应该使用旧样式for循环?
假设我在Java 8中有以下功能接口:
interface Action<T, U> {
U execute(T t);
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我需要一个没有参数或返回类型的操作.所以我写这样的东西:
Action<Void, Void> a = () -> { System.out.println("Do nothing!"); };
Run Code Online (Sandbox Code Playgroud)
但是,它给了我编译错误,我需要把它写成
Action<Void, Void> a = (Void v) -> { System.out.println("Do nothing!"); return null;};
Run Code Online (Sandbox Code Playgroud)
这很难看.有没有办法摆脱Void类型参数?
这是原始问题的简化版本.
我有一个名为Person的类:
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public int Weight { get; set; }
public DateTime FavouriteDay { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
......然后说一个例子:
var bob = new Person {
Name = "Bob",
Age = 30,
Weight = 213,
FavouriteDay = '1/1/2000'
}
Run Code Online (Sandbox Code Playgroud)
我想在我最喜欢的文本编辑器中将以下内容写成字符串 ....
(Person.Age > 3 AND Person.Weight > 50) OR Person.Age < 3
Run Code Online (Sandbox Code Playgroud)
我想取这个字符串和我的对象实例并评估一个TRUE或FALSE - 即在对象实例上评估一个Func <Person,bool>.
这是我目前的想法:
为什么不能在lambda表达式中使用ref或out参数?
我今天遇到了错误并找到了解决方法,但我仍然很好奇为什么这是编译时错误.
CS1628:不能在匿名方法,lambda表达式或查询表达式中的ref或out参数'parameter'中使用
这是一个简单的例子:
private void Foo()
{
int value;
Bar(out value);
}
private void Bar(out int value)
{
value = 3;
int[] array = { 1, 2, 3, 4, 5 };
int newValue = array.Where(a => a == value).First();
}
Run Code Online (Sandbox Code Playgroud) 我在尝试Java 8的Lambda表达式时遇到了问题.通常它工作正常,但现在我有方法可以抛出IOException.最好看一下以下代码:
class Bank{
....
public Set<String> getActiveAccountNumbers() throws IOException {
Stream<Account> s = accounts.values().stream();
s = s.filter(a -> a.isActive());
Stream<String> ss = s.map(a -> a.getNumber());
return ss.collect(Collectors.toSet());
}
....
}
interface Account{
....
boolean isActive() throws IOException;
String getNumber() throws IOException;
....
}
Run Code Online (Sandbox Code Playgroud)
问题是,它不能编译,因为我必须捕获isActive-和getNumber-Methods的可能例外.但即使我明确使用如下所示的try-catch-Block,它仍然无法编译,因为我没有捕获异常.所以要么JDK中存在错误,要么我不知道如何捕获这些异常.
class Bank{
....
//Doesn't compile either
public Set<String> getActiveAccountNumbers() throws IOException {
try{
Stream<Account> s = accounts.values().stream();
s = s.filter(a -> a.isActive());
Stream<String> ss = s.map(a -> a.getNumber());
return ss.collect(Collectors.toSet());
}catch(IOException ex){
} …Run Code Online (Sandbox Code Playgroud) 我正在研究C#7.0中的新实现,我觉得有趣的是他们已经实现了本地函数,但我无法想象一个局部函数比lambda表达式更受欢迎的场景,两者之间有什么区别.
我知道lambdas是anonymous函数,而局部函数却没有,但我无法弄清楚一个真实世界的场景,其中局部函数优于lambda表达式
任何例子都将非常感激.谢谢.
我很少遇到任何其他程序员!
当我第一次看到令牌时,我的想法是"暗示",因为这就是它在数学证明中所读到的,但显然不是它的意义.
那么如何说或读"=>"如下: -
IEnumerable<Person> Adults = people.Where(p => p.Age > 16)
Run Code Online (Sandbox Code Playgroud)
或者甚至有一种商定的说法吗?
我List<BuildingStatus>有个叫buildingStatus.我想检查它是否包含其char代码(返回者GetCharCode())等于某个变量的状态v.Status.
有没有办法按照下面的(非编译)代码的方式做到这一点?
buildingStatus.Contains(item => item.GetCharValue() == v.Status)
Run Code Online (Sandbox Code Playgroud)