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 / 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`
@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 / 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 / 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 / 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 / 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 / CollectionIndexExtensions.swift
Last active May 11, 2016 15:31
Proposed additional index functions for Swift Collections.
//
// CollectionIndexExtensions.swift
// Swift3
//
// Created by Hooman Mehr on 5/9/16.
// Copyright © 2016 Hooman Mehr. See the MIT license at the bottom.
//
@hooman
hooman / KeyValueStore.swift
Last active April 30, 2016 01:46
An API for storing arbitrary key/value pairs. Supporting this API enables extensions to add their properties to our type.
//
// KeyValueStore.swift
// FoundationNG
//
// Created by Hooman Mehr on 4/27/16.
// Copyright © 2016 Hooman Mehr. See the MIT license at the bottom.
//
@hooman
hooman / API.swift
Created March 18, 2016 23:00 — forked from higepon/API.swift
An example of JSON API call in Swift
//
// API.swift
//
// Created by Taro Minowa on 6/10/14.
// Copyright (c) 2014 Higepon Taro Minowa. All rights reserved.
//
import Foundation
typealias JSONDictionary = Dictionary<String, AnyObject>
@hooman
hooman / factorial.swift
Last active August 29, 2015 14:11
Another one of my Swift samples for RosettaCode.org
// Recursive:
func factorial(num: Int) -> Int {
return num < 2 ? 1 : num * factorial(num - 1)
}
// Iterative:
func factorial2(num: Int) -> Int {