RxDart convert Stream based on previous value - flutter

Lets say I have a Stream that emits a List followed by single elements like:
Stream.fromIterable([
[1, 2, 3],
4,
5,
]);
How to convert it to a Stream that updates the previous element with current value and emits:
[1, 2, 3],
[1, 2, 3, 4],
[1, 2, 3, 4, 5],

Related

(mongodb) How to find a nested array in a two-dimensional array, elements of which match a condition

There is a two-dimensional array in every document of my collection. I have a value A and arrays B and C. How can I find documents where there is a nested array that contains A along with values from B (they don't have to be present, there may be other elements apart from them) and at the same time doesn't contain any values from C.
For example, I have:
{id: 1, arr: [[1, 2], [3, 4]]}
{id: 2, arr: [[1, 7], [3, 4]]}
{id: 3, arr: [[2], [3, 4]]}
{id: 4, arr: [[1, 2, 3], [4]]}
A = 1, B = [2, 5], C = [3, 4]
I want to get:
{id: 1, arr: [[1, 2], [3, 4]]}
{id: 2, arr: [[1, 7], [3, 4]]}
I tried to use $and along with $elemMatch, but it hasn't worked out.

MongoDB: Can I store stock data in this way?

{
{
"symbol": "MSFT",
"close": [0, 1, 2, 3, 4, 5],
"open": [0, 1, 2, 3, 4, 5],
"high": [0, 1, 2, 3, 4, 5],
"low": [0, 1, 2, 3, 4, 5],
"volume": [0, 1, 2, 3, 4, 5],
"dates": ["2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06"],
"date_to_index": {
"2022-01-01": 0,
"2022-01-02": 1,
"2022-01-03": 2,
"2022-01-04": 3,
"2022-01-05": 4,
"2022-01-06": 5
}
}
when I need the data of MicroSoft from 2022-01-03 to 2022-01-05, I will get the start and end indices from date_to_index and then retrieve the slice from index 2 to index 4 of the data arrays I want.
You can certainly store data this way, but
looks you'll need to fetch the entire object each time you want to extract only a part of data or do two queries. Either way, it looks not ideal.
Gut feeling says there's a risk of not fitting into document size limit when using real world data (MSFT, for example, has decades of stock data history). Having sub-day resolution increases this risk even further.
Overall, I'd explore alternate strategies.

How to generate combinations of r elements in a given array of size n in swift?

I want total permutation pairs and its count
Like.
If input array is {1, 2, 3, 4} and r is 2.
then output should be {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4}.
Actually the total permutation pairs are
[[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4, 2], [4, 3]]
There's no need to reinvent the wheel. Apple provides a collection of useful and optimized algorithms.
Add the package Swift Algorithms to your project
Then write
import Algorithms
let array = [1, 2, 3, 4]
let permutations = array.permutations(ofCount: 2)
print(Array(permutations), permutations.count)

How to filter duplicate elements and create new array?

I have a duplicate elements array
let array = [1, 2, 2, 3, 5, 3, 6]
How to filter this array and create new array like this:
[[1], [2, 2], [3, 3], [5], [6]]
Quite easy, you should try with this:
let array = [1, 2, 2, 3, 5, 3, 6]
let result= Set(array).map{ value in return array.filter{$0==value} }
print(result)

how to query multiple arrays in Mongod DB

Here are some records in my collection:
{ id: 1, arrayA: [1, 2, 3], arrayB: [4, 5, 6]},
{ id: 2, arrayA: [2, 4], arrayB: [1, 5]},
{ id: 3, arrayA: [1, 6], arrayB: [2, 3, 4]},
{ id: 4, arrayA: [2, 6], arrayB: [1, 3, 4]},
{ id: 5, arrayA: [1, 4, 5], arrayB: [2, 3, 6]},
{ id: 6, arrayA: [2, 4, 5], arrayB: [2, 3]}
....
How do I construct a query that takes an array of numbers (call it inputArray) and would fetch records that contain in either arrayA or arrayB all the numbers specified by inputArray.
If inputArray is [2, 6], then the query would be: find all records that have 2 and 6 in either arrayA or arrayB.
This should return:
id 1: because 2 is in arrayA and 6 is in arrayB
id 3: because 2 is in arrayB and 6 is in arrayA
id 4: because 2 is in arrayA and 6 is in arrayA
id 5: because 2 is in arrayB and 6 is in arrayB
This is the best I could come up with:
db.collection.find({$or: [
{arrayA: {$all: [2, 6]}},
{arrayB: {$all: [2, 6]}},
{$and: [
{arrayA: {$in: [2, 6]}},
{arrayB: {$in: [2, 6]}}
]}
]})


I am just surprised that there isn't any better way!
Okay, here's another version (don't ask me about the efficiency of this :) )
db.ma.find({$or: [{arrayA: 2, arrayB: 6},
{arrayA: 6, arrayB: 2},
{arrayA: {$all: [2,6]}},
{arrayB: {$all: [2, 6]}}]})