#98 Write Spliterator for flatIf, deepFlatIf, treeFlatIf

Відкрити
3 роки тому відкрито ranides · 8 коментарів
ranides відкоментовано 3 роки тому

We want map/flat iterator which simply consumes Iterator<T> and transforms next element on the fly, if necessary. Don't use intermediate singleton iterators.

That means, we should use simple stack. Method next will just take iterator from top of the stack and:

  • if iterator is empty, will pop it and try next value from the stack
  • if iterator.next is non-transformable, returns item
  • if item is transformable, it transforms, pushes generated iterator on stack and executes "next" again

Such strategy allows us to implement two things:

  • flatten without intermediate singleton iterators
  • flatten recursively, it is deep flatten

In fact, strategy described above is "recursive". If we want to have shallow flattening, we should check transformable only if the stack.size = 1

We want map/flat iterator which simply consumes `Iterator<T>` and transforms next element on the fly, if necessary. Don't use intermediate singleton iterators. That means, we should use simple stack. Method `next` will just take iterator from top of the stack and: - if iterator is empty, will pop it and try next value from the stack - if iterator.next is non-transformable, returns item - if item is transformable, it transforms, pushes generated iterator on stack and executes "next" again Such strategy allows us to implement two things: - flatten without intermediate singleton iterators - flatten recursively, it is deep flatten In fact, strategy described above is "recursive". If we want to have shallow flattening, we should check transformable only if the stack.size = 1
ranides відкоментовано 3 роки тому
Власник

Again, this way will give us:

  • flatIf
  • flatWhile
Again, this way will give us: - `flatIf` - `flatWhile`
ranides відкоментовано 3 роки тому
Власник

The question is, what to do with CQuery.flatIf/CQuery.flatWhile when we want to use Stream instead of Iterator.

If using Stream.flatMap we need to use singletonStream. What's more, we need to wrap generated flattened stream by flatMap again.

The question is, what to do with CQuery.flatIf/CQuery.flatWhile when we want to use Stream instead of Iterator. If using Stream.flatMap we need to use singletonStream. What's more, we need to wrap generated flattened stream by flatMap again.
ranides відкоментовано 3 роки тому
Власник

There is reasonable flatMap implementation:

https://stackoverflow.com/questions/32749148/in-java-how-do-i-efficiently-and-elegantly-stream-a-tree-nodes-descendants/32767282#32767282

Probably we can write reasonable flatMapIf implementation using example above.

What's more, it is good workaround for JDK bug, much better than returning to iterator all the time.

There is reasonable flatMap implementation: https://stackoverflow.com/questions/32749148/in-java-how-do-i-efficiently-and-elegantly-stream-a-tree-nodes-descendants/32767282#32767282 Probably we can write reasonable flatMapIf implementation using example above. What's more, it is good workaround for JDK bug, much better than returning to iterator all the time.
ranides відкоментовано 3 роки тому
Власник

We can write addintional, better IteratorUtils.flat too if we use strategy described above. It is simply unconditional, shallow flat.

Current version: flat(Iterator<Iterator<T>> input)

Additional possible version: flat(Iterator<T> input, Function<T, Iterator<T>> splitter)

We can write addintional, better IteratorUtils.flat too if we use strategy described above. It is simply unconditional, shallow flat. Current version: `flat(Iterator<Iterator<T>> input)` Additional possible version: `flat(Iterator<T> input, Function<T, Iterator<T>> splitter)`
ranides відкоментовано 3 роки тому
Власник

Very important: we need to support preserving splitted value and returning it too. It means: item will be transformed to {data, splitted data...} instead of {splitted data...} alone.

It is possibly trivial: one flag which decides to return item first, even if it is transformable, put into last and our next method should split last item instead of the item retrieved from iterator-stack (retrieved item must be returned always if flag is enabled)

We need that if we want to implement #50 correctly in easy way (we don't want to loose interfaces which implements other interfaces, we want to return all of them, both parent and children).

At the level of non-recursive stream it is something like:

stream.flatMap(v -> Stream.of(v).concat(splitme(v)) )

instead of

stream.flatMap(v -> splitme(v) )

or more precisely

stream.flatMap(v -> condition ? Stream.of(v).concat(splitme(v)) : Stream.of(v) )

instead of

stream.flatMap(v -> condition ? splitme(v) : Stream.of(v) )

recursive version (even more complicated):

stream.flatMap(v -> condition ? Stream.of(v).concat( flatIf(condition, splitme(v)) ) : Stream.of(v))

instead of

stream.flatMap(v -> condition ? flatIf(condition, splitme(v)) : Stream.of(v) )
Very important: we need to support preserving splitted value and returning it too. It means: `item` will be transformed to `{data, splitted data...}` instead of `{splitted data...}` alone. It is possibly trivial: one flag which decides to return item first, even if it is transformable, put into `last` and our `next` method should split `last` item instead of the item retrieved from iterator-stack (retrieved item must be returned always if flag is enabled) We need that if we want to implement #50 correctly in easy way (we don't want to loose interfaces which implements other interfaces, we want to return all of them, both parent and children). At the level of non-recursive stream it is something like: ``` stream.flatMap(v -> Stream.of(v).concat(splitme(v)) ) ``` instead of ``` stream.flatMap(v -> splitme(v) ) ``` or more precisely ``` stream.flatMap(v -> condition ? Stream.of(v).concat(splitme(v)) : Stream.of(v) ) ``` instead of ``` stream.flatMap(v -> condition ? splitme(v) : Stream.of(v) ) ``` recursive version (even more complicated): ``` stream.flatMap(v -> condition ? Stream.of(v).concat( flatIf(condition, splitme(v)) ) : Stream.of(v)) ``` instead of ``` stream.flatMap(v -> condition ? flatIf(condition, splitme(v)) : Stream.of(v) ) ```
ranides відкоментовано 3 роки тому
Власник

done: iterator version

todo: stream/spliterator version

todo: CQuery#deepFlatIf and CQuery#treeFlatIf

done: iterator version todo: stream/spliterator version todo: `CQuery#deepFlatIf` and `CQuery#treeFlatIf`
ranides відкоментовано 3 роки тому
Власник

todo: stream/spliterator version. Now we just delegate all CQuery flat to IteratorUtils. That means we are not parallelizable.

todo: stream/spliterator version. Now we just delegate all CQuery `flat` to IteratorUtils. That means we are not parallelizable.
ranides згадано цю проблему в коміті 3 роки тому
ranides відкоментовано 3 роки тому
Власник

todo: we should write optimized versions for CQuery#each too

todo: we should write optimized versions for `CQuery#each` too
Підпишіться щоб приєднатися до обговорення.
Етап відсутній
Немає відповідального
1 учасників
Завантажується...
Скасувати
Зберегти
Тут ще немає жодного вмісту.