Skip to content

Instantly share code, notes, and snippets.

enum Direction {
case north, east, south, west
var left: Direction {
switch self {
case .north: return .west
case .west: return .south
case .south: return .east
case .east: return .north
}
// The contingency table contains the support counts for each item
// in comparision with one another.
struct ContingencyTable {
var bc: Int // B and C
var b_c: Int // B and not C
var _bc: Int // not B and C
var _b_c: Int // not B and not C
@rayfix
rayfix / eclat.swift
Created January 20, 2019 19:43
ECLAT Frequent Pattern Matching in Swift
var accumulatedItemSets = Dictionary(uniqueKeysWithValues: database.map {
(Set([$0.key]), Set($0.value)) })
var itemSetsToEvaluate = accumulatedItemSets
while !itemSetsToEvaluate.isEmpty {
var temp: [Set<String>: Set<Int>] = [:]
itemSetsToEvaluate.pairs().forEach { a, b in
let key = a.key.union(b.key)
let value = a.value.intersection(b.value)
if value.count >= absoluteSupport {
@rayfix
rayfix / GildedRose.swift
Created April 21, 2018 22:32
Gilded Rose
import Foundation
typealias Quality = Int
typealias SellIn = Int
typealias UpdateRule = (Item) -> Item
struct Item {
enum Kind: String {
case normal
extension StoryboardInitializable where Self: UIViewController {
static var storyboardName: String {
return "Main"
}
static var storyboardBundle: Bundle? {
return nil
}
static var storyboardIdentifier: String {
return String(describing: self)
struct BeginsWith {
let value: String
init(_ value: String) {
self.value = value
}
}
func ~=(pattern: BeginsWith, value: String) -> Bool {
return value.hasPrefix(pattern.value)
}
/**
* Copyright (c) 2016 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
//
// Blurring.swift
// TransitionDemo
//
// Created by Ray Fix on 1/7/16.
//
import UIKit
protocol Blurring {}
@rayfix
rayfix / zombie.swift
Created April 6, 2017 17:30
Create a zombie
class Person {}
unowned var zombie: Person
do {
let alive = Person()
zombie = alive
}
// a zombie is born
@rayfix
rayfix / LinkedList.swift
Created April 2, 2017 04:32
An alternate (slightly memory hungry) solution to the middle problem
public enum LinkedList<T> {
case end
indirect case node(value: T, next: LinkedList)
public func cons(_ value: T) -> LinkedList {
return .node(value: value, next: self)
}
}