#98 Write Spliterator for flatIf, deepFlatIf, treeFlatIf

Mở
%! (template.HTML=3 năm trước cách đây)đang mở bởi ranides · 8 ý kiến

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

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.

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 referenced this issue from a commit 3 năm trước cách đây
Đăng nhập để tham gia bình luận.
Không có Milestone
Không có người được phân công
1 tham gia
Đang tải...
Hủy bỏ
Lưu
Ở đây vẫn chưa có nội dung nào.