lambda

How to get the index of filtered item in list using lambda?

一笑奈何 提交于 2020-08-27 22:29:03
问题 I have a list of fruits [{'name': 'apple', 'qty': 233}, {'name': 'orange', 'qty': '441'}] When i filter the list for orange using lambda, list(filter(lambda x: x['name']=='orange', fruits)) , i get the right dict but i can not get the index of the dict. Index should be 1 not 0. How do i get the right index of the filtered item ? 回答1: You can use a list comprehension and enumerate() instead: >>> fruits = [{'name': 'apple', 'qty': 233}, {'name': 'orange', 'qty': '441'}] >>> [(idx, fruit) for

Why java 8 introduces iterable.forEach() loop even though it has for each?

℡╲_俬逩灬. 提交于 2020-08-27 06:51:26
问题 I'm not able to understand why java 8 has forEach loop and taking the Consumer functional interface as parameter. Even though we can do the same task using traditional for each loop without creating any extra overhead to create a class implements that from Consumer and implements a method and then pass this as reference to the forEach(). Although there is lambda expression to make it short. Q1- why iterable.forEach()? Q2. Where to use it? Q3. which one is faster traditional for each of Java 8

How to Except Property instead of Select in Lambda LINQ

自闭症网瘾萝莉.ら 提交于 2020-08-25 05:09:29
问题 I have a code that fetching a table. Here's my sample code: public IQueryable<MyItem> MyItems(){ return context.MyItem; } Here are the sample properties of MyItem public int Id {get;set;} public byte[] Image {get;set;} public string Name {get;set;} Since, byte[] can have multiple characters, I don't want to include then in searching because it will take so long if I have a records like 10,000 items. Typically, I would Select like this: public IQueryable<MyItem> MyItems(){ return context

Declare array of lambdas in Java

不问归期 提交于 2020-08-24 05:42:05
问题 I'd like to create an array of lambda. The problem is that the lambda could be different from each other. Example: private interface I0 { int interface0(int a, int b); } private interface I1 { int interface1(double d); } Now, how can I declare a list which can contain both I0 and I1? List<Object> test = Arrays.asList( (int a, int b) -> a + b, (double d) -> d * 2 ); Obviously Object does not work. 回答1: You must assign the lambda expressions to variables of the functional interface types first.

Declare array of lambdas in Java

匆匆过客 提交于 2020-08-24 05:41:11
问题 I'd like to create an array of lambda. The problem is that the lambda could be different from each other. Example: private interface I0 { int interface0(int a, int b); } private interface I1 { int interface1(double d); } Now, how can I declare a list which can contain both I0 and I1? List<Object> test = Arrays.asList( (int a, int b) -> a + b, (double d) -> d * 2 ); Obviously Object does not work. 回答1: You must assign the lambda expressions to variables of the functional interface types first.

how can i store and reuse pieces of my lambda expressions

自古美人都是妖i 提交于 2020-08-22 09:36:30
问题 I have a block of code where a piece of the lambda expression is used again and again. How can store this logic so that i can reuse this expression piece? Eg: lets take the example of the code given below Session.Query<DimensionGroup>()(dimgroup=>(dimgroup.Users.Where(map => ((map.User.Key == _users.PublicUser.Key || map.User.Key == _users.CurrentUser.Key) && map.AccessLevel.ToAccessLevel() == AccessLevel.Write)).Count() > 0)); (map.User.Key == _users.PublicUser.Key || map.User.Key == _users

Why lambda captures only automatic storage variables?

一曲冷凌霜 提交于 2020-08-22 08:32:39
问题 I just started learning lambda functions in C++ and i don't understand why lambda's allow capturing only automatic storage variables? For example: int x; int main() { [&x](int n){x = n;}; // 'x' cannot be captured... return 0; } On the other hand static variables don't need capturing at all static int s = 0; [](int n){s = n;}; So, why the first example is not allowed and the second works? 回答1: You need to go back and ask yourself: Why do lambdas capture variables at all? Lambdas can use

Why lambda captures only automatic storage variables?

喜欢而已 提交于 2020-08-22 08:29:07
问题 I just started learning lambda functions in C++ and i don't understand why lambda's allow capturing only automatic storage variables? For example: int x; int main() { [&x](int n){x = n;}; // 'x' cannot be captured... return 0; } On the other hand static variables don't need capturing at all static int s = 0; [](int n){s = n;}; So, why the first example is not allowed and the second works? 回答1: You need to go back and ask yourself: Why do lambdas capture variables at all? Lambdas can use

Java 8: How to convert List<String> to Map<String,List<String>>?

爷,独闯天下 提交于 2020-08-22 03:35:40
问题 I have a List of String like: List<String> locations = Arrays.asList("US:5423","US:6321","CA:1326","AU:5631"); And I want to convert in Map<String, List<String>> as like: AU = [5631] CA = [1326] US = [5423, 6321] I have tried this code and it works but in this case, I have to create a new class GeoLocation.java . List<String> locations=Arrays.asList("US:5423", "US:6321", "CA:1326", "AU:5631"); Map<String, List<String>> locationMap = locations .stream() .map(s -> new GeoLocation(s.split(":")[0