reduce

Equivalent of Scala's foldLeft in Java 8

旧时模样 提交于 2020-12-28 06:40:19
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Equivalent of Scala's foldLeft in Java 8

我们两清 提交于 2020-12-28 06:39:53
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Equivalent of Scala's foldLeft in Java 8

回眸只為那壹抹淺笑 提交于 2020-12-28 06:38:07
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Mongodb aggregation: $reduce not working as expected

跟風遠走 提交于 2020-12-13 21:14:05
问题 I have a mongodb aggregation $reduce pipleine that is not working as expected. This is what I am trying to achieve. Basically I am trying to get the object with the highest value in a given property. In some objects $reduce returns the wrong object in others it returns null , meaning no object satisfied the condition. My code has the group stage and other stage that produce the variable used in the $reduce stage. Are there any known preceding stages in the aggregation pipeline that might be

Mongodb aggregation: $reduce not working as expected

拟墨画扇 提交于 2020-12-13 21:05:06
问题 I have a mongodb aggregation $reduce pipleine that is not working as expected. This is what I am trying to achieve. Basically I am trying to get the object with the highest value in a given property. In some objects $reduce returns the wrong object in others it returns null , meaning no object satisfied the condition. My code has the group stage and other stage that produce the variable used in the $reduce stage. Are there any known preceding stages in the aggregation pipeline that might be

Mongodb aggregation: $reduce not working as expected

老子叫甜甜 提交于 2020-12-13 21:02:08
问题 I have a mongodb aggregation $reduce pipleine that is not working as expected. This is what I am trying to achieve. Basically I am trying to get the object with the highest value in a given property. In some objects $reduce returns the wrong object in others it returns null , meaning no object satisfied the condition. My code has the group stage and other stage that produce the variable used in the $reduce stage. Are there any known preceding stages in the aggregation pipeline that might be

Mongodb aggregation: $reduce not working as expected

放肆的年华 提交于 2020-12-13 21:01:12
问题 I have a mongodb aggregation $reduce pipleine that is not working as expected. This is what I am trying to achieve. Basically I am trying to get the object with the highest value in a given property. In some objects $reduce returns the wrong object in others it returns null , meaning no object satisfied the condition. My code has the group stage and other stage that produce the variable used in the $reduce stage. Are there any known preceding stages in the aggregation pipeline that might be

Is there a type-safe way to reduce() a larger object into a new type in typescript?

守給你的承諾、 提交于 2020-12-12 05:35:34
问题 I have a data structure that represents the results from a database query, which is an object with many properties, all scalars (in my case, all either strings or numbers). I want to extract a portion of these properties and fill out a new object that has a defined shape. const input: Record<string, string | number> = { name: 'Jane', age: 42, fav_pet: 'Dog', fav_col: 'Blue', fav_dest: 'Paris' }; const FAVS = ['pet', 'col', 'dest'] as const; type FavsType = { pet: string; col: string; dest:

Is there a type-safe way to reduce() a larger object into a new type in typescript?

醉酒当歌 提交于 2020-12-12 05:33:52
问题 I have a data structure that represents the results from a database query, which is an object with many properties, all scalars (in my case, all either strings or numbers). I want to extract a portion of these properties and fill out a new object that has a defined shape. const input: Record<string, string | number> = { name: 'Jane', age: 42, fav_pet: 'Dog', fav_col: 'Blue', fav_dest: 'Paris' }; const FAVS = ['pet', 'col', 'dest'] as const; type FavsType = { pet: string; col: string; dest: