flatten

Clojure: Why is flatten “the wrong thing to use”

泪湿孤枕 提交于 2019-12-09 16:30:18
问题 I've read this kind of thing a couple of times since I've started Clojure. For instance, here: How to convert map to a sequence? And in some tweet I don't remember exactly that was more or less saying "if you're using flatten you're probably doing it wrong". I would like to know, what is wrong with flatten? 回答1: I think this is what they were talking about in the answer you linked: so> ((comp flatten seq) {:a [1 2] :b [3 4]}) (:b 3 4 :a 1 2) so> (apply concat {:a [1 2] :b [3 4]}) (:b [3 4] :a

Flattening an Iterable<Iterable<T>> in Guava

为君一笑 提交于 2019-12-09 07:25:53
问题 Is there a flatten method in Guava - or an easy way to convert an Iterable<Iterable<T>> to an Iterable<T> ? I have a Multimap<K, V> [sourceMultimap] and I want to return all values where the key matches some predicate [keyPredicate]. So at the moment I have: Iterable<Collection<V>> vals = Maps.filterKeys(sourceMultimap.asMap(), keyPredicate).values(); Collection<V> retColl = ...; for (Collection<V> vs : vals) retColl.addAll(vs); return retColl; I've looked through the Guava docs, but nothing

How to Flatten a HashMap?

被刻印的时光 ゝ 提交于 2019-12-08 14:17:39
问题 I have a nested HashMap in this form: {key1=val1, key2=val2, key3=[ {key4=val4, key5=val5}, {key6=val6, key7=val7} ] } I now want to flatten that map, so that all entries are on the same level: {key1=val1, key2=val2, key4=val4, key5=val5,key6=val6, key7=val7} When I try map.values().forEach(map.get("key3")::addAll); as described in this post, I get the following error: invalid method reference cannot find symbol symbol: method addAll(T) location: class Object where T is a type-variable: T

Denormalize/flatten list of nested objects into dot separated key value pairs

寵の児 提交于 2019-12-08 09:19:30
问题 It would have simpler if my nested objects were dictionaries, but these are list of dictionaries. Example: all_objs1 = [{ 'a': 1, 'b': [{'ba': 2, 'bb': 3}, {'ba': 21, 'bb': 31}], 'c': 4 }, { 'a': 11, 'b': [{'ba': 22, 'bb': 33, 'bc': [{'h': 1, 'e': 2}]}], 'c': 44 }] I expect output in following format: [ {'a': 1, 'b.ba': 2, 'b.bb': 3, 'c': 4}, {'a': 1, 'b.ba': 21, 'b.bb': 31, 'c': 4}, {'a': 11, 'b.ba': 22, 'b.bb': 33, 'bc.h': 1, 'bc.e': 2, 'c': 44}, ] Basically, number of flattened objects

SelectMany to flatten a nested structure

喜欢而已 提交于 2019-12-08 02:51:44
问题 I am parsing an XML structure and my classes look like the following: class MyXml { //... List<Node> Content { get; set; } //... } class Node { // ... public List<Node> Nodes { get; set; } public string Type { get; set; } //... } MyXml represents the XML file I am parsing, whose elements are all called <node> . Each node has a type attribute, which can have different values. The type of the node is not connected to its depth. I can have any node type at any depth level. I can parse the

flatten javascript array of objects

孤人 提交于 2019-12-08 02:16:25
I have an array of object with hierarchical structure, something like this: [ {name: 'ParentOne', children: [ {name: 'ParentOneChildOne'}, {name: 'ParentOneChildTwo', children: [ {name: 'ParentOneChildTwoGrandChildOne'}, ]}, ]}, {name: 'ParentTwo', children: [ {name: 'ParentTwoChildOne', children: [ {name: 'ParentTwoChildOneGrandChildOne'}, {name: 'ParentTwoChildOneGrandChildTwo'} ]}, {name: 'ParentTwoChildTwo'} ]} ]; I want to flatten it: [ {name: 'ParentOne'}, {name: 'ParentOneChildOne'}, {name: 'ParentOneChildTwo'}, {name: 'ParentOneChildTwoGrandChildOne'}, {name: 'ParentTwo'}, {name:

What is destructive?

北城余情 提交于 2019-12-07 17:31:50
问题 I know that Ruby has many methods, especially on array or array like objects, for example sort or flatten . However, these methods also have a twin (the one with an exclamation mark) like sort! and flatten! . Now my questions are: What is the difference between flatten and flatten! (destructive flatten)? A more general question, why is it called destructive? 回答1: The difference is simply that flatten returns a copy of the array (a new array that is flattened) and flatten! does the

Code style - “flattening” a package's namespace

 ̄綄美尐妖づ 提交于 2019-12-07 09:33:56
问题 My package hierarchy: InstrumentController/ __init__.py instruments/ __init__.py _BaseInstrument.py Keithley2000.py # etc... The contents of the instrument files: # _BaseInstrument.py class _BaseInstrument(object): """Base class for instruments""" # etc... # Keithley2000.py from InstrumentController.instruments._BaseInstrument import _BaseInstrument class Keithley2000(_BaseInstrument): # etc... I want my users to be able to access the classes without having to delve into a hierarchy of

Flatten tuple like a bag

笑着哭i 提交于 2019-12-07 02:04:20
问题 My dataset looks like the following: ( A, (1,2) ) ( B, (2,9) ) I would like to "flatten" the tuples in Pig, basically repeating each record for each value found in the inner-tuple, such that the expected output is: ( A, 1 ) ( A, 2 ) ( B, 2 ) ( B, 9 ) I know this is possible when the tuples (1,2) and (2,9) are bags instead. 回答1: Your insight is good; it's possible by transforming the tuple in a bag. The schema we want to aim for is: {a: chararray,{(chararray)}} for example: (A,{(1),(2)}) Here

How to flatten a hash, making each key a unique value?

情到浓时终转凉″ 提交于 2019-12-06 18:21:32
问题 I want to take a hash with nested hashes and arrays and flatten it out into a single hash with unique values. I keep trying to approach this from different angles, but then I make it way more complex than it needs to be and get myself lost in what's happening. Example Source Hash: { "Name" => "Kim Kones", "License Number" => "54321", "Details" => { "Name" => "Kones, Kim", "Licenses" => [ { "License Type" => "PT", "License Number" => "54321" }, { "License Type" => "Temp", "License Number" =>