Skip to content

Instantly share code, notes, and snippets.

@justinmeiners
Last active March 7, 2022 21:47
Show Gist options
  • Save justinmeiners/ab87e47e5f6fd1451d29d5bb29b6beaf to your computer and use it in GitHub Desktop.
Save justinmeiners/ab87e47e5f6fd1451d29d5bb29b6beaf to your computer and use it in GitHub Desktop.

Revisions

  1. justinmeiners revised this gist Feb 25, 2022. No changes.
  2. justinmeiners revised this gist Feb 25, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion multiplicity.swift
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ enum SequenceMultiplicity<S: Sequence> {

    extension Sequence {
    func multiplicity(where predicate: (Element) -> Bool) -> SequenceMultiplicity<Self> {
    var it = self.makeIterator()
    var it = makeIterator()
    while let x = it.next() {
    if predicate(x) {
    while let y = it.next() {
  3. justinmeiners revised this gist Feb 25, 2022. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions multiplicity.swift
    Original file line number Diff line number Diff line change
    @@ -6,13 +6,13 @@ enum CollectionMultiplicity<C: Collection> {

    extension Collection {
    func indexedMultiplicity(where predicate: (Element) -> Bool) -> CollectionMultiplicity<Self> {
    guard let x = firstIndex(where: predicate) else {
    guard let i = firstIndex(where: predicate) else {
    return .none
    }
    guard let y = suffix(from: index(after: x)).firstIndex(where: predicate) else {
    return .one(first: x)
    guard let j = suffix(from: index(after: i)).firstIndex(where: predicate) else {
    return .one(first: i)
    }
    return .many(first: x, second: y)
    return .many(first: i, second: j)
    }
    }

  4. justinmeiners revised this gist Feb 25, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion multiplicity.swift
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ extension Collection {
    guard let x = firstIndex(where: predicate) else {
    return .none
    }
    guard let y = suffix(from: index(after: first)).firstIndex(where: predicate) else {
    guard let y = suffix(from: index(after: x)).firstIndex(where: predicate) else {
    return .one(first: x)
    }
    return .many(first: x, second: y)
  5. justinmeiners revised this gist Feb 25, 2022. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions multiplicity.swift
    Original file line number Diff line number Diff line change
    @@ -6,14 +6,13 @@ enum CollectionMultiplicity<C: Collection> {

    extension Collection {
    func indexedMultiplicity(where predicate: (Element) -> Bool) -> CollectionMultiplicity<Self> {
    guard let first = firstIndex(where: predicate) else {
    guard let x = firstIndex(where: predicate) else {
    return .none
    }

    guard let second = suffix(from: index(after: first)).firstIndex(where: predicate) else {
    return .one(first: first)
    guard let y = suffix(from: index(after: first)).firstIndex(where: predicate) else {
    return .one(first: x)
    }
    return .many(first: first, second: second)
    return .many(first: x, second: y)
    }
    }

    @@ -26,7 +25,6 @@ enum SequenceMultiplicity<S: Sequence> {
    extension Sequence {
    func multiplicity(where predicate: (Element) -> Bool) -> SequenceMultiplicity<Self> {
    var it = self.makeIterator()

    while let x = it.next() {
    if predicate(x) {
    while let y = it.next() {
  6. justinmeiners created this gist Feb 25, 2022.
    42 changes: 42 additions & 0 deletions multiplicity.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    enum CollectionMultiplicity<C: Collection> {
    case none
    case one(first: C.Index)
    case many(first: C.Index, second: C.Index)
    }

    extension Collection {
    func indexedMultiplicity(where predicate: (Element) -> Bool) -> CollectionMultiplicity<Self> {
    guard let first = firstIndex(where: predicate) else {
    return .none
    }

    guard let second = suffix(from: index(after: first)).firstIndex(where: predicate) else {
    return .one(first: first)
    }
    return .many(first: first, second: second)
    }
    }

    enum SequenceMultiplicity<S: Sequence> {
    case none
    case one(first: S.Element)
    case many(first: S.Element, second: S.Element)
    }

    extension Sequence {
    func multiplicity(where predicate: (Element) -> Bool) -> SequenceMultiplicity<Self> {
    var it = self.makeIterator()

    while let x = it.next() {
    if predicate(x) {
    while let y = it.next() {
    if predicate(y) {
    return .many(first: x, second: y)
    }
    }
    return .one(first: x)
    }
    }
    return .none
    }
    }