Skip to content

Instantly share code, notes, and snippets.

View hooman's full-sized avatar

Hooman Mehr hooman

  • Kirkland, WA, United States
  • X @h_mehr
View GitHub Profile
@hooman
hooman / WeakReference.swift
Last active December 29, 2022 09:06
A set of utilities for working with weak references.
//: Playground - noun: a place where people can play
/// A protocol to provide an abstraction of types that hold a weak reference to a target object.
///
/// It is defined because a single generic implementation cannot support existentials, as they
/// do not conform to themselves or `AnyObject`. Most of its API is defined by protocol extensions
/// to makes it easier to create existential wrapper `struct`s or `final class`es.
///
/// Here is an example protocol and the corresponding weak reference
@hooman
hooman / Mutex.swift
Last active September 2, 2022 17:56
Swift 3.0 wrapper for the new os_unfair_lock
import Darwin
public protocol MutexValue {
mutating func lock()
mutating func locked() -> Bool
mutating func unlock()
@hooman
hooman / AssociatedObjects.swift
Last active May 4, 2022 07:33
Take 2.0: A completely new take on Objective-C associated types. (See history for the ancient approach)
// Playground - noun: a place where people can play
//
//
// By: Hooman Mehr (hooman@mac.com)
// Gist: https://gist.github.com/hooman/599e381d5f037b87d20b (The latest version is always here)
//
//
// Update: 07/30/2014
// Added WeaklyOwned & removeOrphans for memory management support, added more generics & basic access control.
// 08/06/2014
@hooman
hooman / RationalNumber.swift
Last active January 26, 2021 15:40
Rational numbers in Swift 3.0
// RationalNumber.swift
//
// A basic implementation of Rational numbers in Swift 3.0.
// (c) 2016 Hooman Mehr. Licensed under Apache License v2.0 with Runtime Library Exception
/// A data type to model rational numbers in Swift.
///
/// It always uses fully-reduced representation for simplicity and clarity of comparisons and uses LCM
@hooman
hooman / UnicodeScalarUtils.swift
Created March 15, 2019 00:44
Utilities to work with ASCII characters and characters as integers.
// A small set of utilities for working with Unicode and ASCII scalars. Here is what it does:
//
// 1. Make `UnicodeScalar` strideable. This enables ranges of Unicode Scalars such as "a"..."z".
// 2. Add strideable Unicode.ASCII.Scalar (type alias `ASCII`) to enable type-safe manipulation of ASCII bytes.
// 3. Define limited `+`/`-` operators for stridable types to make something like `"x" + ("A"-"a")` possible.
// 1. Make `UnicodeScalar` strideable. This enables ranges of Unicode Scalars such as "a"..."z".
// Constants used in `Strideable` conformance extension of `Unicode.Scalar`
// Playground - noun: a place where people can play
//
// Notifications.swift
//
// Created by Hooman Mehr (hooman@mac.com) on 7/26/14.
// Copyright (c) 2014 Hooman Mehr. New BSD License.
//
// WARNING: This is a sample code to study and to demostrate some Swift capabilities
// and limitations. Use it at your own risk.
@hooman
hooman / HTMLBuilder.swift
Created April 10, 2017 03:20
This is a prototype code of a DSL to make it easier to generate HTML within Swift source code.
//
// HTMLBuilder.swift
//
// This is a prototype code of a DSL to make it easier to generate HTML within Swift source code.
//
// This version of the prototype usues global constants for html elements because of a compiler limitation / bug.
// The intended behavior was having these globals static properties of `Html` type.
//
// Created by Hooman Mehr on 04/07/17.
// Copyright © 2017 Hooman Mehr. See the MIT license at the bottom.
@hooman
hooman / TupleSplat.swift
Last active February 16, 2018 23:23
An operator for tuple splat of simple function arguments (up to six arguments implemented).
// An operator for tuple splat of simple function arguments (up to six arguments implemented).
// by Hooman Mehr (hooman@mac.com)
// Source released as public domain.
precedencegroup TupleSplatPrecedence {
associativity: left
higherThan: BitwiseShiftPrecedence
}
// Infix operator for on the fly tuple splat:
@hooman
hooman / EquatableClass.swift
Last active January 26, 2018 05:47
Sample code on how to correctly implement `Equatable` protocol for class types.
// The correct implementation of `Equatable` (and `Comparable`) can be tricky for class
// hierarchies. To make it easier, it is better to follow a well-defined pattern. Here
// is my suggestion on how to do it:
// 1. Define a protocol for polymorphic test function for equality (or comparison):
// Note: operators are not polymorphic (not virtual in C++ terms)). The function to
// call is determined and hard-coded at compile time.
/// A protocol to ammend `Equatable` for use with `class` types.
@hooman
hooman / multimethod.swift
Last active October 13, 2016 07:56
A sample to demonstrate how to simulate multimethods in Swift
// Simulating multimethods in Swift
// Consider these two hierarchies:
// FamilyA
// / \
// A1 A2
// \
// SubA1