Skip to content

Instantly share code, notes, and snippets.

View lorentey's full-sized avatar
⚙️

Karoy Lorentey lorentey

⚙️
View GitHub Profile
@lorentey
lorentey / Mutable Class Method.swift
Created December 12, 2015 00:01
You wouldn't expect calling a method on a class to modify the variable holding a reference to the instance, but it can happen.
//: Playground - noun: a place where people can play
import UIKit
protocol MagicProtocol {
}
extension MagicProtocol {
mutating func magic(xyzzy: Self) {
// Mutating methods defined in a protocol extension can mutate `self` even if
// the protocol is implemented by a class.
@lorentey
lorentey / inference-snafu.swift
Last active June 2, 2016 22:05
The following snippet compiles under Swift 2.2, but not Swift 3. It defines a (quite useless) left shift operator on `Double` values. For some reason, Swift 3's type inference engine can go awry when such a shift operator is used in a subexpression.
public func <<(num: Double, exp: Int) -> Double {
return num * Double(1 << exp)
}
let s = (0.5 << 5) - 1
print(s)
@lorentey
lorentey / shadow.swift
Last active July 8, 2018 12:20
The most annoying thing about Swift 3's naming conventions is that some renamed members now shadow frequently used global functions:
extension Array where Element: Comparable {
func clamp(from lowerBound: Element, to upperBound: Element) -> Array {
return self.map { min(upperBound, max(lowerBound, $0)) }
}
}
let clamped = [0, 1, 2, 3, 4, 5, 6].clamp(from: 2, to: 4))
### Keybase proof
I hereby claim:
* I am lorentey on github.
* I am lorentey (https://keybase.io/lorentey) on keybase.
* I have a public key whose fingerprint is 086D 2046 8C31 E936 754F C0C7 E9EA 32A8 0FB2 7A3F
To claim this, I am signing this object:
protocol Sink {
func receive()
}
protocol Source {
associatedtype Value
func subscribe(_ sink: Sink) -> Int
func unsubscribe(_ token: Int)
protocol Sink {
associatedtype Value
func receive(_ value: Value)
}
// This assumes Generalized Existentials are a thing
// https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#generalized-existentials
typealias AnySink<Value> = Any<Sink where .Value == Value>
protocol Source {
import Foundation
@objc class Person: NSObject {
var _firstName: String = "John"
var _lastName: String = "Smith"
dynamic var firstName: String { get { return _firstName } set { _firstName = newValue } }
dynamic var lastName: String { get { return _lastName } set { _lastName = newValue } }
dynamic var name: String { return "\(firstName) \(lastName)" }
Hello
This sample file contains examples of all kinds of data that can be stored in a pile.
All text up to the first line that consists of a #pile version directive is ignored by the parser.
Feel free to write anything here.
This is how all text piles start: (The "1" is the version number of the file format.)
#pile:1
@lorentey
lorentey / ivar-access-pitch.md
Last active November 8, 2019 04:22
[pitch] Exposing the memory locations of class instance variables
@lorentey
lorentey / Atomics.md
Last active October 29, 2024 18:56
[pitch] Low-Level Atomic Operations ⚛︎