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 / RomanNumerals.swift
Last active August 29, 2015 14:11
One of my Swift samples for RosettaCode.org
func intToRoman(var n: Int) -> String {
var result = ""
for (value, letter) in
[( 1000, "M"),
( 900, "CM"),
( 500, "D"),
( 400, "CD"),
( 100, "C"),
@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.
/// Y-Combinator for creating recursive closures.
///
/// :param: `In` Arbitrary input parameter type(s) of the closure.
/// :param: `Out` Arbitrary return type (including `Void`) of the closure.
/// :param: `f` represents the recursive closure.
/// :returns: Returns a recursive closure.
///
/// It is used with a pair of closures: An outer closure that names the inner closure (`f`)
/// and lets the inner closure recurse on itself by capturing itself using the parameter of
/// the outer closure (`f`). For example:
@hooman
hooman / FuncBox.swift
Last active August 29, 2015 14:06
A wrapper class to hold and pass around a Swift function in ObjC Code.
import Foundation
@objc
public final class FuncBox<T,U> {
private var _fn: [T->U]
public var fn: T->U { return _fn[0] }
// A trick to make callable types by (ab)using subscript.
@hooman
hooman / ArrayMultiD.swift
Last active August 29, 2015 14:05
This is obsolete, please see this one: https://github.com/hooman/ArrayMultiD
// NOTE: THIS IS NOT CURRENT
// The current version is here: https://github.com/hooman/ArrayMultiD
// Playground - noun: a place where people can play
//
// ArrayMultiD.swift
// ArrayMultiD
@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
// 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 / 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 / UnwrapOrDefault.swift
Last active August 29, 2015 14:04
Unwrap convenience operator
// Playground - noun: a place where people can play
// NOTE: Now that Swift has ?? operator, this gist is mostly obsolete.
protocol HasNaturalDefault
{ class var naturalDefault:Self {get} }
operator postfix ~! {}
operator infix ~! {associativity none precedence 170}
@hooman
hooman / LazyDouble.swift
Last active August 29, 2015 14:04
Lazy Swift: Double Math Example
// Playground - noun: a place where people can play
/*
I was working on various ways to implement lazy behavior in Swift.
I am sharing this simple example for community feedback and to see
if we can come up with some easy and nice looking ways to integrate
lazy behavior in our daily code.
Apple Developer Forums discussion is here:
https://devforums.apple.com/message/1005900#1005900