Skip to content

Instantly share code, notes, and snippets.

View lorentey's full-sized avatar
⚙️

Karoy Lorentey lorentey

⚙️
View GitHub Profile
@lorentey
lorentey / Package.swift
Created February 19, 2021 08:14
A Swift solver for the numbers game in Countdown
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "countdown-solver",
products: [
.executable(name: "countdown-solver", targets: ["countdown-solver"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.3.0"),
],
@lorentey
lorentey / Atomics.md
Last active April 16, 2020 22:28
[pitch] Low-Level Atomic Operations ⚛︎
@lorentey
lorentey / ivar-access-pitch.md
Last active November 8, 2019 04:22
[pitch] Exposing the memory locations of class instance variables
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
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)" }
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 {
protocol Sink {
func receive()
}
protocol Source {
associatedtype Value
func subscribe(_ sink: Sink) -> Int
func unsubscribe(_ token: Int)
### 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:
@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))
@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)