This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func intToRoman(var n: Int) -> String { | |
var result = "" | |
for (value, letter) in | |
[( 1000, "M"), | |
( 900, "CM"), | |
( 500, "D"), | |
( 400, "CD"), | |
( 100, "C"), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simulating multimethods in Swift | |
// Consider these two hierarchies: | |
// FamilyA | |
// / \ | |
// A1 A2 | |
// \ | |
// SubA1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
// | |
// Notifications.swift | |
// | |
// Created by Hooman Mehr ([email protected]) 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
// | |
// | |
// By: Hooman Mehr ([email protected]) | |
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
NewerOlder