#include <iostream>
int main(){
auto lambda = [] {
return 7;
};
std::cout << lambda() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
该程序编译并打印7.
lambda的返回类型基于返回值7推导为整数类型.
为什么普通功能不能实现这一点?
#include <iostream>
auto function(){
return 42;
}
int main(){
std::cout << function() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
错误:'function'函数使用'auto'类型说明符而没有尾随返回类型
我的同事和我有一个错误是由于我们假设空流调用allMatch()将返回false.
if (myItems.allMatch(i -> i.isValid()) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
当然,假设而不是阅读文档是我们的错.但我不明白为什么allMatch()空流的默认行为返回true.这是什么原因?就像anyMatch()(相反地返回false)一样,这个操作以一种离开monad并且可能在if声明中使用的命令性方式使用.考虑到这些事实,是否有任何理由为什么在大多数用途中需要allMatch()默认为true空流?
当我尝试编译此代码(VS2010)时,我收到以下错误:
error C3499: a lambda that has been specified to have a void return type cannot return a value
void DataFile::removeComments()
{
string::const_iterator start, end;
boost::regex expression("^\\s?#");
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
// Look for lines that either start with a hash (#)
// or have nothing but white-space preceeding the hash symbol
remove_if(rawLines.begin(), rawLines.end(), [&expression, &start, &end, &what, &flags](const string& line)
{
start = line.begin();
end = line.end();
bool temp = boost::regex_search(start, end, what, expression, flags);
return temp; …Run Code Online (Sandbox Code Playgroud) 我理解this在lambda中捕获(修改对象属性)的正确方法如下:
auto f = [this] () { /* ... */ };
Run Code Online (Sandbox Code Playgroud)
但我很好奇我所看到的以下特点:
class C {
public:
void foo() {
// auto f = [] () { // this not captured
auto f = [&] () { // why does this work?
// auto f = [&this] () { // Expected ',' before 'this'
// auto f = [this] () { // works as expected
x = 5;
};
f();
}
private:
int x;
};
Run Code Online (Sandbox Code Playgroud)
我很困惑的奇怪(并希望得到回答)是以下工作的原因:
auto f = …Run Code Online (Sandbox Code Playgroud) 我一直在研究Collections.sort和之间的区别list.sort,特别是关于使用Comparator静态方法以及lambda表达式中是否需要param类型.在我们开始之前,我知道我可以使用方法引用,例如Song::getTitle克服我的问题,但我的查询并不是我想修复的东西,而是我想要的答案,即为什么Java编译器以这种方式处理它.
这是我的发现.假设我们有一个ArrayList类型Song,添加了一些歌曲,有3种标准的get方法:
ArrayList<Song> playlist1 = new ArrayList<Song>();
//add some new Song objects
playlist.addSong( new Song("Only Girl (In The World)", 235, "Rhianna") );
playlist.addSong( new Song("Thinking of Me", 206, "Olly Murs") );
playlist.addSong( new Song("Raise Your Glass", 202,"P!nk") );
Run Code Online (Sandbox Code Playgroud)
这里调用两种类型的排序方法,没问题:
Collections.sort(playlist1,
Comparator.comparing(p1 -> p1.getTitle()));
playlist1.sort(
Comparator.comparing(p1 -> p1.getTitle()));
Run Code Online (Sandbox Code Playgroud)
一旦我开始连锁thenComparing,就会发生以下情况:
Collections.sort(playlist1,
Comparator.comparing(p1 -> p1.getTitle())
.thenComparing(p1 -> p1.getDuration())
.thenComparing(p1 -> p1.getArtist())
);
playlist1.sort(
Comparator.comparing(p1 -> p1.getTitle())
.thenComparing(p1 -> p1.getDuration()) …Run Code Online (Sandbox Code Playgroud) 我试图在下面的Collectors.toMap()调用中为"keyMapper"函数参数提供一个更简洁的表达式:
List<Person> roster = ...;
Map<String, Person> map =
roster
.stream()
.collect(
Collectors.toMap(
new Function<Person, String>() {
public String apply(Person p) { return p.getLast(); }
},
Function.<Person>identity()));
Run Code Online (Sandbox Code Playgroud)
似乎我应该能够使用lambda表达式来内联它,但我无法想出一个编译它的人.(我对lambdas很新,所以这并不奇怪.)
谢谢.
- >更新:
如接受的答案所述
Person::getLast
Run Code Online (Sandbox Code Playgroud)
是我在寻找的东西,也是我尝试过的东西.然而,Eclipse 4.3的BETA_8每晚构建是问题 - 它标记为错误.从命令行编译时(我应该在发布之前完成),它起作用了.所以,是时候用eclipse.org提交bug了.
谢谢.
我最近发现了以下代码:
IntPredicate neg = x -> x <- x;
Run Code Online (Sandbox Code Playgroud)
这是什么,某种反向双lambda?
以下代码有什么作用?
a = lambda _:True
Run Code Online (Sandbox Code Playgroud)
从我在交互式提示中阅读和测试的内容来看,它似乎始终是一个返回的函数True.
我理解正确吗?我希望理解为什么_还使用了下划线().
我正在尝试将每个循环的旧常规转换为java7到java8的每个循环用于映射条目集但是我收到错误.这是我要转换的代码:
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}
Run Code Online (Sandbox Code Playgroud)
这是我所做的改变:
map.forEach( Map.Entry<String, String> entry -> {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
});
Run Code Online (Sandbox Code Playgroud)
我也尝试过这样做:
Map.Entry<String, String> entry;
map.forEach(entry -> {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
});
Run Code Online (Sandbox Code Playgroud)
但仍面临错误.我得到的错误是:Lambda表达式的签名与功能接口方法的签名不匹配accept(String, String)
lambda ×10
java ×5
java-8 ×5
c++ ×3
c++11 ×3
java-stream ×2
auto ×1
c# ×1
collections ×1
comparator ×1
function ×1
obfuscation ×1
operators ×1
python ×1
sorting ×1
syntax ×1
visual-c++ ×1