Skip to content

Instantly share code, notes, and snippets.

View SeanROlszewski's full-sized avatar

Sean Olszewski SeanROlszewski

  • Apple
  • United States
View GitHub Profile
@anandabits
anandabits / Monoid.swift
Created January 8, 2019 00:24
Exploring the design space for the Monoid abstraction in Swift
/*
This gist contains an analysis of the design space for abstractions in a type system like Swift's.
It focuses on Monoid as an example but the same set of tradeoffs apply to any abstraction.
As such, it focuses on a single abstraction and does not explore issues that arise when designing
an abstraction hierarchy in depth.
The matrix below describes some of the design important design tradeoffs for various approaches including:
- OO style protocols such as `Monoid { var identity: Self ... }
(Haskell also adopts this approach for many abstractions, including for Monoid)
- ML signature style static protocols with empty enum conformances analagous with ML structures
@IanKeen
IanKeen / KeypathUpdatable.swift
Created February 15, 2018 20:03
A simpler swifty lens pattern
public protocol KeypathUpdatable {
func update<T>(_ keyPath: WritableKeyPath<Self, T>, to value: T) -> Self
}
public extension KeypathUpdatable {
public func update<T>(_ keyPath: WritableKeyPath<Self, T>, to value: T) -> Self {
var copy = self
copy[keyPath: keyPath] = value
return copy
}
@anandabits
anandabits / ClosedProtocol.swift
Created February 11, 2018 21:30
Emulating closed protocols in Swift
// This example shows how closed protocols can be emulated in Swift today.
// The technique leverages Swift's support for public (but not open) classes.
// First, it's worth observing that there is an almost trivial technique that can be used when
// it is possible to specify a (possibly abstract) superclass for all conforiming types.
// Simply declare a public (not open) base class and a protocol with a Self inheritance constraint.
// Swift does not support open subclasses of a public superclass so no classes outside the module
// will be able to meet the self inheritance constraint.
public class FooBase {}
public protocol Foo where Self: FooBase {}
@anandabits
anandabits / HKT.swift
Last active December 25, 2023 00:57
Emulating HKT in Swift
// This example shows how higher-kinded types can be emulated in Swift today.
// It acheives correct typing at the cost of some boilerplate, manual lifting and an existential representation.
// The technique below was directly inspired by the paper Lightweight Higher-Kinded Polymorphism
// by Jeremy Yallop and Leo White found at http://ocamllabs.io/higher/lightweight-higher-kinded-polymorphism.pdf
/// `ConstructorTag` represents a type constructor.
/// `Argument` represents an argument to the type constructor.
struct Apply<ConstructorTag, Argument> {
/// An existential containing a value of `Constructor<Argument>`
/// Where `Constructor` is the type constructor represented by `ConstructorTag`
@IanKeen
IanKeen / gist:dfbe42fea3f5746e87aebac86d508f7e
Last active January 14, 2021 00:18
Getting playgrounds working in a project in xcode
1. Create a new xcode project
- You can skip this if you're adding the playground to an existing project
2. File > Save as workspace - save workspace in project folder
- You can skip this if you're adding the playground to an existing workspace
3. File > Playground - create new playground in project folder
4. Drag playground in at the _workspace_ level (root item, i.e. _not_ under the project)
5. Add a new Framework to your project (I usually name mine something like 'PlaygroundKit')
- You don't need to include tests
- Choose 'None' for Enbed in Application
6. Make all project files you want to be accessible to the playground are members of the new target
@ZevEisenberg
ZevEisenberg / map float range.swift
Last active July 13, 2020 20:31
Mapping floating point numbers between two ranges in Swift
import QuartzCore
extension CGFloat {
func map(from from: ClosedInterval<CGFloat>, to: ClosedInterval<CGFloat>) -> CGFloat {
let result = ((self - from.start) / (from.end - from.start)) * (to.end - to.start) + to.start
return result
}
}
extension Double {
@natecook1000
natecook1000 / NSScanner+Swift.swift
Created March 3, 2015 20:13
Swift-friendly NSScanner methods
// NSScanner+Swift.swift
// A set of Swift-idiomatic methods for NSScanner
//
// (c) 2015 Nate Cook, licensed under the MIT license
import Foundation
extension NSScanner {
// MARK: Strings
@jboner
jboner / latency.txt
Last active July 17, 2024 03:12
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD