Skip to content

Instantly share code, notes, and snippets.

extension Int {
internal func divMod(other:Int) -> (quotient:Int, modulus:Int) {
let quotient = self / other
let remainder = self % other
if (quotient > 0) || (remainder == 0) {
return (quotient, remainder)
} else if quotient == 0 && (self > 0) && (other < 0) {
let div = quotient - 1
let result = (div * other) - self
@griotspeak
griotspeak / Int.swift
Last active May 23, 2016 15:41
DivMod and QuotRem
extension Int {
internal func divMod(other:Int) -> (quotient:Int, modulus:Int) {
let quotient = self / other
let remainder = self % other
if (quotient > 0) || (remainder == 0) {
return (quotient, remainder)
} else if quotient == 0 && (self > 0) && (other < 0) {
let div = quotient - 1
let result = (div * other) - self
@griotspeak
griotspeak / MicroKanren.hs
Created May 4, 2016 21:37 — forked from msullivan/MicroKanren.hs
MicroKanren (μKanren) in Haskell
import Control.Monad
type Var = Integer
type Subst = [(Var, Term)]
type State = (Subst, Integer)
type Program = State -> KList State
data Term = Atom String | Pair Term Term | Var Var deriving Show
-- Apply a substitution to the top level of a term
@griotspeak
griotspeak / uKanren.scala
Created May 4, 2016 21:33 — forked from adamnew123456/uKanren.scala
microKanren In Scala
/**
* An implementation of microKanren (and probably most of miniKanren), with
* a few extras. Currently, it supports:
*
* - The essential core of microKanren: Unify, Fresh, Disjunction, Conjunction
* - Standard terms: Variables, Atoms, TermCons, EmptyTerm.
* - An implicit conversion from type T to Atom[T]. This makes writing programs
* much easier.
* - A decent reifier, which converts terms to strings.
*
@griotspeak
griotspeak / notAWordAboutMacros.swift
Created February 12, 2016 01:17
n-ary assertions
protocol PayloadBearing_1 {
typealias Payload_1 : Equatable
func payload() -> Payload_1?
}
protocol PayloadBearing_2 : PayloadBearing_1 {
typealias Payload_2 : Equatable
func payload() -> (Payload_1, Payload_2)?
}
@griotspeak
griotspeak / DiamondMaybe.swift
Created June 25, 2015 00:11
"Ambiguous implied conformance" error
public struct MyValue : Comparable {
let value: Int
}
public func ==(first:MyValue, second:MyValue) -> Bool {
return true
}
public func <(first:MyValue, second:MyValue) -> Bool {
@griotspeak
griotspeak / TerribleThings.swift
Created June 18, 2015 00:54
I'm sorry, dear stranger. I've done this to show you a truth.
var theCounter:Int = 0
var theString:String = ""
func doIt() -> String {
if theCounter++ % 2 == 0 {
return "wakka"
} else {
return " "
}
}
@griotspeak
griotspeak / Array+TJU.swift
Last active August 29, 2015 14:22
Swift.Array extensions
//
// Array.swift
// TonalKit
//
// Created by TJ Usiyan on 11/8/14.
// Copyright (c) 2014 Buttons and Lights LLC. All rights reserved.
// https://gist.github.com/7a9444caf1b225070b88.git
public func reduceR<S : CollectionType, U, IndexType : BidirectionalIndexType where S.Index == IndexType>(sequence: S, initial: U, combine: (U, S.Generator.Element) -> U) -> U{
@griotspeak
griotspeak / Optional_TJU.swift
Created June 1, 2015 16:12
Swift.Optional extensions
//
// Optional.swift
// TonalKit
//
// Created by TJ Usiyan on 11/4/14.
// Copyright (c) 2014 Buttons and Lights LLC. All rights reserved.
// https://gist.github.com/d9d826ed242b89ff2d85.git
extension Optional {
@griotspeak
griotspeak / Set+TJU.swift
Created June 1, 2015 16:10
Swift.Set extensions
//
// Set.swift
// TonalKit
//
// Created by TJ Usiyan on 3/2/15.
// Copyright (c) 2015 buttons-and-lights. All rights reserved.
// https://gist.github.com/17d2f63780e9549a3335.git
import Foundation