Skip to content

Instantly share code, notes, and snippets.

@Pretz
Created September 24, 2015 18:21
Show Gist options
  • Select an option

  • Save Pretz/23057802003f1f4936a7 to your computer and use it in GitHub Desktop.

Select an option

Save Pretz/23057802003f1f4936a7 to your computer and use it in GitHub Desktop.

Revisions

  1. Pretz created this gist Sep 24, 2015.
    15 changes: 15 additions & 0 deletions get.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    import Swift

    extension CollectionType where Index: SignedNumberType, Index == Index.Distance {
    func get(var position: Self.Index) -> Self.Generator.Element? {
    if position < 0 {
    position = self.endIndex.advancedBy(position)
    }
    return (indices ~= position) ? self[position] : nil

    }
    }

    let a = [1, 2, 3, 4, 5]

    a.get(-2) // returns 4