Skip to content

Add recursiveMap(_:) - #185

Open
SusanDoggie wants to merge 17 commits into
apple:mainfrom
SusanDoggie:patch-1
Open

SusanDoggie wants to merge 17 commits into
apple:mainfrom
SusanDoggie:patch-1

Conversation

@SusanDoggie

@SusanDoggie SusanDoggie commented Mar 27, 2022

Copy link
Copy Markdown

add recursiveMap() method.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

@SusanDoggie
SusanDoggie marked this pull request as ready for review March 28, 2022 03:32
@SusanDoggie SusanDoggie changed the title add recursiveMap() add recursiveMap(_:) Mar 28, 2022
@SusanDoggie SusanDoggie changed the title add recursiveMap(_:) [DNM] add recursiveMap(_:) Mar 28, 2022
@SusanDoggie SusanDoggie changed the title [DNM] add recursiveMap(_:) Add recursiveMap(_:) Mar 28, 2022
@CTMacUser

Copy link
Copy Markdown
Contributor

Is a "recursive mapped sequence" a term of art for a particular structure? I've never heard of it before, and the documentation so far assumes you pretty much already know. A quick web search found something related using the Python language; is the code here supposed to be a Swift adaptation of the Python code?

The concept seems to be a way to do tree traversal without that type having to provide its own depth- and breath-first iterators; you just have to supply a function to map an element to a Sequence of its children, and this function takes care of the rest.

@SusanDoggie

Copy link
Copy Markdown
Author

Is a "recursive mapped sequence" a term of art for a particular structure? I've never heard of it before, and the documentation so far assumes you pretty much already know. A quick web search found something related using the Python language; is the code here supposed to be a Swift adaptation of the Python code?

The concept seems to be a way to do tree traversal without that type having to provide its own depth- and breath-first iterators; you just have to supply a function to map an element to a Sequence of its children, and this function takes care of the rest.

This method is simplified recursive CTE, which can map a hierarchical table to a linear sequence.

I don’t known what’s the name of this operation. So I call it recursive map.

@ssouzawallace ssouzawallace left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without further proof of time and space complexity. This PR IMO is unmeargeable.
Even though it could be a good addition. I firstly thought that this would apply to any Sequence. However. Looks like this is a special case of Colletion and maybe not Sequence.
Having to conform to RecursiveMapSequence seems like too much trouble for a rare case in this protocol.
But as I said. Maybe it fits better with Collection or similar.

-/+ 1 - The Node struct example is not properly written for official docs.

Comment on lines +46 to +48
///
/// - Complexity: O(1)
@inlinable

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SusanDoggie
I'm kinda skeptic this func is actually O(1) for time-complexity. Or any recursive function. Unles I'm missing a rare 0.00001% case. Are you sure about that L47 statement?

Could you demonstrate if possible?

Looks like it is at least O(n). But what is n? Is n self.count? Is n the whole tree size i.e. all nodes from self.

Also VERY IMPORTANT*! It doesn't look like your Node struct example is conforming with Sequence or RecursiveMapSequence at all.

Other than that. Interesting use case you put there and awesome work with putting this so much ready for official documentation.

@ssouzawallace

Copy link
Copy Markdown

@SusanDoggie Also. Tip. If you plan on keeping this PR up-to-date. I suggest squash commits, make them atomic commits, all properly signed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants