This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator ¢ { | |
associativity left | |
precedence 140 | |
} | |
// Intercal interleave (mingle) | |
func ¢(var lhs: UInt16, var rhs: UInt16) -> UInt32 { | |
let msb = 1 << UInt16(sizeof(UInt16)*8-1) | |
var result: UInt32 = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example usage | |
func square<T : NumericArithmeticType>(value: T) -> T { | |
return value * value | |
} | |
let squaredInt: Int = square(3) // 9 | |
let squaredFloat: Float = square(3) // 9.0 | |
// Only keeps 1 bits that have a 1 bit neighboring them on the left |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I advise you skip this as it isn't particularly useful or complete | |
// but.... | |
// it's kinda cool in that it shows how structs can be used in generics | |
// for non-generic purposes, if that makes any sense. | |
protocol IntercalType { | |
static var symbol: String { get } | |
} | |
protocol IntercalValueType : IntercalType { } | |
protocol IntercalArrayType : IntercalType { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator ** { | |
associativity left | |
precedence 150 | |
} | |
func **(lhs: Float, rhs: Int) -> Float { | |
let result = Array(count: abs(rhs), repeatedValue: lhs).reduce(1, combine: { lhs, rhs in lhs * rhs }) | |
return rhs >= 0 ? result : 1 / result | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A binary representation of arbitrarily large numbers that supports adding | |
// Note that his does not support any other operations (even printing a description!) and thus is just an experiment | |
func map2<S : SequenceType, T : SequenceType, U>(lhs: S, rhs: T, transform: (S.Generator.Element, T.Generator.Element) -> U) -> [U] { | |
return Array(Zip2(lhs, rhs)).map(transform) | |
} | |
struct BigInt : IntegerLiteralConvertible { | |
let backing: [UIntMax] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A generator that allows you to provide a default value (instead of nil) for when the generator | |
// runs out of elements | |
struct DefaultValueGenerator<T : GeneratorType> : GeneratorType { | |
var generator: T | |
var defaultValue: T.Element | |
mutating func next() -> T.Element? { | |
return generator.next() ?? defaultValue | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct WeakZip2<S0: SequenceType, S1: SequenceType> : SequenceType { | |
let s0: S0 | |
let s1: S1 | |
func generate() -> WeakZipGenerator2<S0.Generator, S1.Generator> { | |
return WeakZipGenerator2(e0: s0.generate(), e1: s1.generate()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ConcatenatedSequence<S0: SequenceType, S1: SequenceType where S0.Generator.Element == S1.Generator.Element> : SequenceType { | |
let s0: S0 | |
let s1: S1 | |
func generate() -> ConcatenatedGenerator2<S0.Generator, S1.Generator> { | |
return ConcatenatedGenerator2(e0: s0.generate(), e1: s1.generate()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Integer { | |
var value: Int { get } | |
init() | |
} | |
// Note we don't use static properties because, as of Swift 1.2, static properties on generics are unsupported | |
struct Zero : Integer { | |
let value = 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note this is just experimentation. Swift is not well suited for this sort of | |
// manipulation (as is evident by all the hard-coded functions and the length 5 limitation) | |
// We created an array types such that | |
// a) values do not have to be all the same type | |
// b) the array is typed by its length, and thus is safer | |
// Example | |
var a = (5, "hi", 3.14) |