[swift-evolution] Proposal to copy C++'s mismatch

Daryle Walker darylew at mac.com
Wed Jul 6 04:01:17 CDT 2016

I mentioned in other messages about adding permutations and combinations (probably as generators/iterators) to the standard library.  I tried making sample implementations and a proposal, but it transitioned to adapting C++’s “is_permutation,” “next_permutation,” and “prev_permutation” instead.  The sample implementation of “is_permutation” I saw at <http://en.cppreference.com/w/cpp/algorithm/is_permutation> involves the “mismatch” function, which we also don’t seem to have.  Since that function seems like a small enough bite the chew, I finally made a proposal at <https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc>.  (The Gist is currently flagged as secret.)

Oh, it seems that everyone here has moved on to Swift 3, and so has my third-party documentation program.  Unfortunately, I still on non-beta code, which means Swift 2.2.  So I took some time having to translate concepts between the versions, including new names.

The name “mismatch” didn’t seem Swift-y enough since it doesn’t describe what’s happening from a Swift programming perspective.  I tried:

* commonPrefixUntil / commonSuffixUntil
* elementsEqualUntil / elementsEqualSince
* elementsShared(until:) / elementsShared(since:)
* elementsDiverge / elementsConverge

No, those parameters on the third one don’t make sense.  The last one inspired me to trim the fat and just use “diverge(from:)”.  Since we use indexes here like C++’s iterators, that was the best choice for a return type that allows the users to take the results in an inspecting manner or mutating manner.  But Swift’s model doesn’t handle reversed collections like C++ does, so I need a separate routine for mismatching with reverse iterators, i.e. searching backwards with indexes.  Since I used the “diverge” name for the forward search, I flipped it to “converge(with:)” for the reverse/suffix search.  The returns aren’t used in quite the same manner since I have to avoid needing a before-the-start index.

A lot of the format was badly copied from the rotate/reverse proposal (<https://github.com/apple/swift-evolution/blob/master/proposals/0078-rotate-algorithm.md>).  Looking for opinions, mistakes/clean-up, anything major missing?...

Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160706/723943b8/attachment.html>

More information about the swift-evolution mailing list