collect

Collect values from an array of hashes [closed]

风流意气都作罢 提交于 2019-12-02 21:01:33
I have a data structure in the following format: data_hash = [ { price: 1, count: 3 }, { price: 2, count: 3 }, { price: 3, count: 3 } ] Is there an efficient way to get the values of :price as an array like [1,2,3] ? Zabba First, if you are using ruby < 1.9: array = [ {:price => 1, :count => 3}, {:price => 2, :count => 3}, {:price => 3, :count => 3} ] Then to get what you need: array.map{|x| x[:price]} 来源: https://stackoverflow.com/questions/18899868/collect-values-from-an-array-of-hashes

Scala Partition/Collect Usage

落爺英雄遲暮 提交于 2019-12-02 16:37:04
Is it possible to use one call to collect to make 2 new lists? If not, how can I do this using partition ? collect (defined on TraversableLike and available in all subclasses) works with a collection and a PartialFunction . It also just so happens that a bunch of case clauses defined inside braces are a partial function (See section 8.5 of the Scala Language Specification [warning - PDF] ) As in exception handling: try { ... do something risky ... } catch { //The contents of this catch block are a partial function case e: IOException => ... case e: OtherException => ... } It's a handy way to

Neo4j cypher query : using ORDER BY with COLLECT(S)

╄→гoц情女王★ 提交于 2019-12-01 21:31:26
I'm having a hard time collecting data from two distinct sources and merge the collections so that the final one is a set of objects ordered by 'dateCreated'. Context Users can ask questions in groups. A question can be either general or related to a specific video-game. If the question asked in a group is video-game related, this question also appears in the video-game's questions page. Currently, I have two general questions and one specific to one video-game. Hence, when fetching the questions, I should have 3 questions. Query Here's the query : START group = node(627) MATCH

Is collectingAndThen method enough efficient?

巧了我就是萌 提交于 2019-12-01 13:45:57
I have recently started using collectingAndThen and found that it is taking a bit long time comparatively to the other coding procedures, which i used for performing the similar tasks. Here is my code: System.out.println("CollectingAndThen"); Long t = System.currentTimeMillis(); String personWithMaxAge = persons.stream() .collect(Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparing(Person::getAge)), (Optional<Person> p) -> p.isPresent() ? p.get().getName() : "none" )); System.out.println("personWithMaxAge - "+personWithMaxAge + " time taken = "+(System.currentTimeMillis() - t));

Is collectingAndThen method enough efficient?

余生长醉 提交于 2019-12-01 07:51:35
问题 I have recently started using collectingAndThen and found that it is taking a bit long time comparatively to the other coding procedures, which i used for performing the similar tasks. Here is my code: System.out.println("CollectingAndThen"); Long t = System.currentTimeMillis(); String personWithMaxAge = persons.stream() .collect(Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparing(Person::getAge)), (Optional<Person> p) -> p.isPresent() ? p.get().getName() : "none" )); System

How can I use a named scope in my model against an array of items?

风流意气都作罢 提交于 2019-11-30 20:39:31
I know that I can do a query for recent books based on an array as in scope :recent_books, lambda {|since_dt| {:conditions=>{:created_at >= since_dt}}} but how can I do a similar query when I have an array of items, e.g. what if I want to know if there any records that match the dates in an array of [date1, date2, date3, etc.] I think there must be a collect/inject/select/map y method that'll do it but I am not sure which from reading them. If you pass an array as the value, ActiveRecord is smart enough to compare for inclusion in the array. For example, Book.where(:author_id => [1, 7, 42])

How can I use a named scope in my model against an array of items?

梦想与她 提交于 2019-11-30 04:56:53
问题 I know that I can do a query for recent books based on an array as in scope :recent_books, lambda {|since_dt| {:conditions=>{:created_at >= since_dt}}} but how can I do a similar query when I have an array of items, e.g. what if I want to know if there any records that match the dates in an array of [date1, date2, date3, etc.] I think there must be a collect/inject/select/map y method that'll do it but I am not sure which from reading them. 回答1: If you pass an array as the value, ActiveRecord

Java 8 stream join and return multiple values

馋奶兔 提交于 2019-11-29 06:53:05
I'm porting a piece of code from .NET to Java and stumbled upon a scenario where I want to use stream to map & reduce. class Content { private String propA, propB, propC; Content(String a, String b, String c) { propA = a; propB = b; propC = c; } public String getA() { return propA; } public String getB() { return propB; } public String getC() { return propC; } } List<Content> contentList = new ArrayList(); contentList.add(new Content("A1", "B1", "C1")); contentList.add(new Content("A2", "B2", "C2")); contentList.add(new Content("A3", "B3", "C3")); I want to write a function that can stream

ruby using the “&:methodname” shortcut from array.map(&:methodname) for hash key strings rather than methodname

雨燕双飞 提交于 2019-11-28 08:11:31
Most ruby developers know how to save a few keystrokes by doing something like this: array.map(&:methodname) rather than array.map {|x| x.methodname } Is there any way I could apply a similar &:methodname shortcut for calling "methods" (values called via keys) on an array of hashes? In my case its with JSON api results being returned in batches of 60 as arrays of hashes originating from JSON. I tried to do this but was not successful: array.map(&:"keyname") but was not successful, throwing a NoMethodError and saying that there's no 'keyname' method for Hash, which is reasonable I guess. I'm

Static context cannot access non-static in Collectors

本小妞迷上赌 提交于 2019-11-28 08:09:48
I have group of students. First I want to group them by the marks. Then I want to further group those sets into same name students together. Map<Integer,Map<String,List<String>>> groupping = students.stream() .collect(Collectors.groupingBy(Student::getMarks, Collectors.mapping(Student::getName,Collectors.toList()))); I am getting a error saying, Non-static method cannot be refered from a static context. Yes. I am pretty much aware that I cannot refer a non-static method without having an instance. But with all these stream operations, I'm bit confused what has gone wrong really. Rather than