Forked from masters3d/elvisOperatorStdLib.swift
Last active
December 5, 2015 23:29
-
-
Save ilyannn/be9380f15b0b1f114c09 to your computer and use it in GitHub Desktop.
An experiment in removing tetriary expressions from stdlib (thanks to masters3d for original file)
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
//Filter.swift | |
public mutating func next() -> Base.Element? { | |
var n: Base.Element? | |
for/*ever*/;; { | |
n = _base.next() | |
if n != nil ? _predicate(n!) : true { | |
return n | |
} | |
} | |
} | |
// Also kills var, nil check and ! as a bonus. | |
public mutating func next() -> Base.Element? { | |
while let next = _base.next() { | |
if _predicate(next) { | |
return next | |
} | |
} | |
return nil | |
} | |
// HeapBuffer.swift | |
static func _requiredAlignMask() -> Int { | |
// We can't use max here because it can allocate an array. | |
let heapAlign = alignof(_HeapObject.self) &- 1 | |
let valueAlign = alignof(Value.self) &- 1 | |
let elementAlign = alignof(Element.self) &- 1 | |
return (heapAlign < valueAlign | |
? (valueAlign < elementAlign ? elementAlign : valueAlign) | |
: (heapAlign < elementAlign ? elementAlign : heapAlign)) | |
} | |
// Use version of max that is guaranteed not to create an array. | |
static func _requiredAlignMask() -> Int { | |
let heapAlign = alignof(_HeapObject.self) &- 1 | |
let valueAlign = alignof(Value.self) &- 1 | |
let elementAlign = alignof(Element.self) &- 1 | |
return max(heapAlign, max(valueAlign, elementAlign)) | |
} | |
// Index.swift | |
@warn_unused_result | |
public func advancedBy(n: Distance, limit: Self) -> Self { | |
let d = self.distanceTo(limit) | |
if d == 0 || (d > 0 ? d <= n : d >= n) { | |
return limit | |
} | |
return self.advancedBy(n) | |
} | |
// Assuming we have a <=> comparison operator. | |
@warn_unused_result | |
public func advancedBy(n: Distance, limit: Self) -> Self { | |
let d = self.distanceTo(limit) | |
// Perform advancedBy only if 0 and n are on the same side of d. | |
guard 0 <=> d == n <=> d else { | |
return limit | |
} | |
return self.advancedBy(n) | |
} | |
// Reflection.swift | |
let bullet = count == 0 ? "-" | |
: maxDepth <= 0 ? "▹" : "▿" | |
print("\(bullet) ", terminator: "", toStream: &targetStream) | |
// Assuming we implemented switch as an expression and Sign enum. | |
let bullet = switch (count, maxDepth.sign) { | |
(0, _) : "-" | |
(_, .Positive) : "▿" | |
default : "▹" | |
} | |
print("\(bullet) ", terminator: "", toStream: &targetStream) | |
// Stride.swift | |
public mutating func next() -> Element? { | |
if done { | |
return nil | |
} | |
if stride > 0 ? current >= end : current <= end { | |
if current == end { | |
done = true | |
return current | |
} | |
return nil | |
} | |
let ret = current | |
current += stride | |
return ret | |
} | |
} | |
// Assuming we have a <=> comparison operator. | |
public mutating func next() -> Element? { | |
if done { | |
return nil | |
} | |
if current == end { | |
done = true | |
return current | |
} | |
guard 0 <=> stride == current <=> end else { | |
return nil | |
} | |
let ret = current | |
current += stride | |
return ret | |
} | |
} | |
// StringBridge.swift | |
self._core = _StringCore( | |
baseAddress: COpaquePointer(start), | |
count: length, | |
elementShift: isUTF16 ? 1 : 0, | |
hasCocoaBuffer: true, | |
owner: unsafeBitCast(cfImmutableValue, Optional<AnyObject>.self)) | |
// I don't see any harm in Int(_ b:Bool). | |
self._core = _StringCore( | |
baseAddress: COpaquePointer(start), | |
count: length, | |
elementShift: Int(isUTF16), | |
hasCocoaBuffer: true, | |
owner: unsafeBitCast(cfImmutableValue, Optional<AnyObject>.self)) | |
// UnicodeScalar | |
subscript(position: Int) -> UTF16.CodeUnit { | |
return position == 0 ? ( | |
endIndex == 1 ? UTF16.CodeUnit(value.value) : UTF16.leadSurrogate(value) | |
) : UTF16.trailSurrogate(value) | |
} | |
} | |
// This doesn't even need switch as expression. | |
// We make it more readable and as a bonus are able to check correctness. | |
subscript(position: Int) -> UTF16.CodeUnit throws { | |
switch (position, endIndex) { | |
case (0, 1): return UTF16.CodeUnit(value.value) | |
case (0, 2): return UTF16.leadSurrogate(value) | |
case (1, 2): return UTF16.trailSurrogate(value) | |
} | |
throw OutOfBounds | |
} | |
// StringCore.swift | |
_StringBuffer( | |
capacity: newCount, | |
initialSize: 0, | |
elementWidth: | |
width == 1 ? 1 | |
: representableAsASCII() && !newElements.contains { $0 > 0x7f } ? 1 | |
: 2 | |
) | |
// We could also use 1 + Int(...) here. | |
var elementWidth = width | |
if !representableAsASCII() || newElements.contains ({ $0 > 0x7f }) { | |
elementWidth = 2 | |
} | |
_StringBuffer( | |
capacity: newCount, | |
initialSize: 0, | |
elementWidth: elementWidth | |
) | |
// old code | |
let width = elementWidth == 2 || newElements.contains { $0 > 0x7f } ? 2 : 1 | |
// becomes | |
var width = elementWidth | |
if newElements.contains ({ $0 > 0x7f }) { | |
width = 2 | |
} | |
// old code | |
let minElementWidth | |
= elementWidth >= rhs.elementWidth | |
? elementWidth | |
: rhs.representableAsASCII() ? 1 : 2 | |
// becomes | |
let minElementWidth = switch (elementWidth, rhs.elementWidth, rhs.representableAsASCII()) { | |
(1, 1, _) : 1 | |
(1, _, true): 1 | |
default : 2 | |
} | |
// old code | |
let newElementWidth = | |
minElementWidth >= elementWidth | |
? minElementWidth | |
: representableAsASCII() ? 1 : 2 | |
// becomes | |
let newElementWidth = switch (minElementWidth, elementWidth, representableAsASCII()) { | |
(1, 1, _) : 1 | |
(1, _, true): 1 | |
default : 2 | |
} | |
// old code | |
self._countAndFlags | |
= (UInt(elementShift) << (UInt._sizeInBits - 1)) | |
| ((hasCocoaBuffer ? 1 : 0) << (UInt._sizeInBits - 2)) | |
| UInt(count) | |
// becomes | |
self._countAndFlags | |
= (UInt(elementShift) << (UInt._sizeInBits - 1)) | |
| (UInt(hasCocoaBuffer) << (UInt._sizeInBits - 2)) | |
| UInt(count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment