Skip to content

Instantly share code, notes, and snippets.

View adamchalmers's full-sized avatar

Adam Chalmers adamchalmers

View GitHub Profile
let array = Array(0..<10)
let slice = array[2..<5]
// Mixing Array/ArraySlice compiles!
print(extend(array, withNewElems: array))
print(extend(array, withNewElems: slice))
print(extend(slice, withNewElems: slice))
// Mixing Ints/Bools doesn't compile!
print(extend(array, withNewElems: [true, false]))
func extend<T>(_: [T], withNewElems: [T]) -> [T]
func extend<T>(_: Collection, withNewElems: Collection) -> [T]
func extend<T: Collection>(_ base: T, withNewElems: T) -> T {
// Ignore the function body for now,
// we’re just trying to make it compile.
return base
}
func extend<T: Collection, U: Collection>(_ base: T, withNewElems: U)
func extend<T: Collection, U: Collection>(_ base: T, withNewElems: U)
where T.Iterator.Element == U.Iterator.Element {
// function body will go here
// also ignoring return type for now
}
func extend<T: Collection, U: Collection>(_ base: T, withNewElems: U) -> [T.Iterator.Element]
where T.Iterator.Element == U.Iterator.Element {
let newElems = withNewElems.filter { !base.contains($0) }
return Array(base) + newElems
}
func extend<T: Collection, U: Collection>(_ base: T, withNewElems: U) -> [T.Iterator.Element]
where T.Iterator.Element == U.Iterator.Element,
T.Iterator.Element: Equatable {
let newElems = withNewElems.filter { !base.contains($0) }
return Array(base) + newElems
}
import Foundation
// Represents a calculation that could either succeed, or fail with an error message.
enum Result<A> {
// If the calculation was successful, store the result.
case Ok(A)
// If the calculation failed, store the error message.
case Err(String)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="robots" content="noindex, nofollow">
<link rel="stylesheet" type="text/css" href="/uf/theme/53.css?1461851479">
<link rel="SHORTCUT ICON" title="BigPulse.com" href="/favicon.ico">
<title>Selection or information required</title>
<script type="text/javascript">