lambda

Variable capture in Lambda

无人久伴 提交于 2021-02-16 15:44:04
问题 I can't think why the captured variables are final or effectively final in lambda expressions. I looked over this question and really quite didn't get the answer. What is this variable capture? As I searched solutions for my problem, I read that these variables are final because of concurrency problems. But for such situation why can't we lock the task code in the lambda with a reentrant lock object. public class Lambda { private int instance=0; public void m(int i,String s,Integer integer

How do I deal with checked exceptions in lambda? [duplicate]

允我心安 提交于 2021-02-16 15:36:08
问题 This question already has answers here : Java 8 Lambda function that throws exception? (26 answers) Closed 4 years ago . I have the following code snippet. package web_xtra_klasa.utils; import java.util.Arrays; import java.util.Properties; import java.util.function.Function; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class

Bracket Abstraction in Prolog

前提是你 提交于 2021-02-16 14:47:20
问题 The algorithm "A" according to Antoni Diller looks fairly simple: http://www.cantab.net/users/antoni.diller/brackets/intro.html Can we do this in Prolog? 回答1: % associate formulas to left associate_left(F, A, B) :- append(A, [B], F). % apply algorithm reduce(b(_, []), []). reduce(b(A, B), 'K'(B)) :- atom(B), dif(A, B). reduce(b(A, A), 'I'). reduce(b(A, [F]), R) :- reduce(b(A, F), R). % uncessary paranthesis case reduce(b(A, F), 'S'(Pr, Qr)) :- associate_left(F, P, Q), reduce(b(A, P), Pr),

Java 8 - why they provided method references?

点点圈 提交于 2021-02-16 14:46:10
问题 What is better in calling names.stream().forEach(System.out::println); Than names.stream().forEach(n -> System.out.println(n)); Despite the fact you have to write less code? Are there any other advantages of introducing method references in Java 8? 回答1: Despite the fact you have to write less code? Are there any other advantages of introducing method references in Java 8? Having to write less code is enough of an advantage to consider introducing a language feature. There is a similar feature

Convert List<Person> to Map<Integer, List<Integer>> using Lambdas

可紊 提交于 2021-02-16 14:39:25
问题 I wanted to convert list to Map as below. Here is the example. I have student list something like below code snippet. Get a Hasmap out of it with Key as Integer which is Age and value as List. From the below input feed, My response should be something like below. How do I achieve this ? Map[[10, {1}], [20, {2,3,4}], [30,{5}]. [40,{6}]]; private static List<Person> getPersonTestData() { List<Person> personList = new ArrayList<>(); personList.add(Person.of(1, "First1", "Last1", 10)); personList

Combine BinaryExpression and Expression<Func<dynamic, bool>> in C#

前提是你 提交于 2021-02-16 13:49:08
问题 How can I combine BinaryExpression and Expression<Func<dynamic / T, bool>> ? For example: void AddGlobalFilter<T>(Expression<Func<T, bool>> expr) { var parameter = Expression.Parameter(type, "t"); var member = Expression.Property(filter.Parameter, field); var constant = Expression.Constant(null); var body = Expression.Equal(member, constant); var combine = Expression.AndAlso(body, expr); } I am trying to define global filter for Entity Framework (EF) Core. The problem is I must manually

Unhandled IOException Inside Java List.stream.forEach Lambda Expression

谁都会走 提交于 2021-02-16 09:10:17
问题 I am using the Stream API introduced in Java 8 to run a method for each string in a list. public boolean initFile() throws IOException { if (this.outFile.exists()) { this.outFile.delete(); } return this.outFile.createNewFile(); } public void writeStringToFile(String str, boolean addNewLine) throws IOException { if (this.mode != FileMode.READ) { if (this.initFile()) { FileWriter fileWriter; if (this.mode == FileMode.APPEND) fileWriter = new FileWriter(outFile.getAbsolutePath(), true); else

Unhandled IOException Inside Java List.stream.forEach Lambda Expression

回眸只為那壹抹淺笑 提交于 2021-02-16 09:01:20
问题 I am using the Stream API introduced in Java 8 to run a method for each string in a list. public boolean initFile() throws IOException { if (this.outFile.exists()) { this.outFile.delete(); } return this.outFile.createNewFile(); } public void writeStringToFile(String str, boolean addNewLine) throws IOException { if (this.mode != FileMode.READ) { if (this.initFile()) { FileWriter fileWriter; if (this.mode == FileMode.APPEND) fileWriter = new FileWriter(outFile.getAbsolutePath(), true); else

How to perfectly forward `auto&&` in a generic lambda?

允我心安 提交于 2021-02-15 10:16:34
问题 C++14 supports generic lambdas. However, the following code is rejected by clang 3.4. #include <utility> void f(int); void f(int&); int main() { [](auto&& v) { f(std::forward<auto>(v)); }(8); // error } How to perfectly forward auto&& in a generic lambda? 回答1: auto is not a type so I’m not surprised this doesn’t work. But can’t you use decltype ? [](auto&& v) { f(std::forward<decltype(v)>(v)); }(8); Scott Meyers has more details. 来源: https://stackoverflow.com/questions/24535430/how-to

How to perfectly forward `auto&&` in a generic lambda?

不羁岁月 提交于 2021-02-15 10:16:25
问题 C++14 supports generic lambdas. However, the following code is rejected by clang 3.4. #include <utility> void f(int); void f(int&); int main() { [](auto&& v) { f(std::forward<auto>(v)); }(8); // error } How to perfectly forward auto&& in a generic lambda? 回答1: auto is not a type so I’m not surprised this doesn’t work. But can’t you use decltype ? [](auto&& v) { f(std::forward<decltype(v)>(v)); }(8); Scott Meyers has more details. 来源: https://stackoverflow.com/questions/24535430/how-to