Questions tagged [flatten]

Flattening refers to either reducing a multi-dimensional array to a single dimension or to reducing a class and class methods to handle based function calls.

1554 questions
41
votes
6 answers

Un-optioning an optioned Option

Say I have a val s: Option[Option[String]]. It can thus have the following values: Some(Some("foo")) Some(None) None I want to reduce it so that the first becomes Some("foo") while the two others become None. Obviously there are many ways to…
Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59
41
votes
2 answers

Flattening an Iterable> in Guava

Is there a flatten method in Guava - or an easy way to convert an Iterable> to an Iterable? I have a Multimap [sourceMultimap] and I want to return all values where the key matches some predicate [keyPredicate]. So at the moment…
Andy Whitfield
  • 2,373
  • 2
  • 19
  • 22
41
votes
6 answers

Scala - convert List of Lists into a single List: List[List[A]] to List[A]

What's the best way to convert a List of Lists in scala (2.9)? I have a list: List[List[A]] which I want to convert into List[A] How can that be achieved recursively? Or is there any other better way?
A Far
  • 425
  • 1
  • 4
  • 6
40
votes
2 answers

Python list comprehension, unpacking and multiple operations

I want to unpack the tuples I create by doing the following so he the result is just one simple list. I can get the desired result in 2-3 lines but surely there is a oneliner list.comp? >>> x = range(10) >>> y = [(i,j**2) for i,j in zip(x,x)] >>>…
arynaq
  • 6,710
  • 9
  • 44
  • 74
38
votes
1 answer

How to flatten a List of Futures in Scala

I want to take this val: val f = List(Future(1), Future(2), Future(3)) Perform some operation on it (I was thinking flatten) f.flatten And get this result scala> f.flatten = List(1,2,3) If the flatten method isn't appropriate here, that's fine. …
mljohns89
  • 887
  • 1
  • 11
  • 16
35
votes
8 answers

Convert array of single-element arrays to a one-dimensional array

I have this kind of an array containing single-element arrays: $array = [[88868], [88867], [88869], [88870]]; I need to convert this to one dimensional array. Desired output: [88868, 88867, 88869, 88870] Is there any built-in/native PHP…
DEVOPS
  • 18,190
  • 34
  • 95
  • 118
33
votes
2 answers

Remove names from named vector and get only the values

I have a vector like below tmp <- c(a=1, b=2, c=3) a b c 1 2 3 I want to flatten this vector to get only 1, 2, 3. I tried unlist(tmp) but it still gives me the same result. How to achieve that efficiently?
sertsedat
  • 3,490
  • 1
  • 25
  • 45
33
votes
5 answers

Julia: Flattening array of array/tuples

In Julia vec reshapes multidimensional arrays into one-dimension arrays. However it doesn't work for arrays of arrays or arrays of tuples. A part from using array comprehension, is there another way to flatten arrays of arrays/tuples? Or arrays of…
Pigna
  • 2,792
  • 5
  • 29
  • 51
33
votes
11 answers

How to Serialize Binary Tree

I went to an interview today where I was asked to serialize a binary tree. I implemented an array-based approach where the children of node i (numbering in level-order traversal) were at the 2*i index for the left child and 2*i + 1 for the right…
worker1138
  • 2,071
  • 5
  • 29
  • 36
32
votes
5 answers

Flatten multidimensional array concatenating keys

Possible Duplicate: PHP convert nested array to single array while concatenating keys? Get array's key recursively and create underscore seperated string Please, read the whole question before answering. I have this multidimensional array: $data…
J. Bruni
  • 20,322
  • 12
  • 75
  • 92
32
votes
8 answers

A better way to use AutoMapper to flatten nested objects?

I have been flattening domain objects into DTOs as shown in the example below: public class Root { public string AParentProperty { get; set; } public Nested TheNestedClass { get; set; } } public class Nested { public string…
John
  • 511
  • 1
  • 5
  • 10
32
votes
4 answers

how to do a "flat push" in javascript?

I want to push all individual elements of a source array onto a target array, target.push(source); puts just source's reference on the target list. In stead I want to do: for (i = 0; i < source.length; i++) { target.push(source[i]); } Is there…
dr jerry
  • 9,768
  • 24
  • 79
  • 122
30
votes
7 answers

Flatten a list in Prolog

I've only been working with Prolog for a couple days. I understand some things but this is really confusing me. I'm suppose to write a function that takes a list and flattens it. ?- flatten([a,[b,c],[[d],[],[e]]],Xs). Xs = [a,b,c,d,e]. …
ToastyMallows
  • 4,203
  • 5
  • 43
  • 52
28
votes
4 answers

Convert 2 dimensional array

What is selectMany.ToArray() method? Is it a built in method in C#? I need to convert two dimensional array to one dimensional array.
Manoj
  • 451
  • 1
  • 7
  • 8
28
votes
7 answers

How to flatten a tuple in python

I have the following element of a list, and the list is 100 elements long. [(50, (2.7387451803816479e-13, 219))] How do I convert each element to look like this? [(50, 2.7387451803816479e-13, 219)]
olliepower
  • 1,269
  • 2
  • 11
  • 17