Skip to content

Instantly share code, notes, and snippets.

View oisdk's full-sized avatar

Donnacha Oisín Kidney oisdk

View GitHub Profile
extension CollectionType {
func generateWithStride(interval: Self.Index.Distance = 1) -> PermutationGenerator<Self,AnySequence<Self.Index>> {
var index = startIndex
let generator: AnyGenerator<Self.Index> = anyGenerator {
defer { index = index.advancedBy(interval, limit: self.endIndex) }
return index == self.endIndex ? nil : index
}
return PermutationGenerator(elements: self, indices: AnySequence(generator))
}
}
import random
def cardName(card):
face = {11:"Jack", 12:"Queen", 13:"King"}
if card == 1: return "Ace"
if card > 10: return face[card]
else : return str(card)
def hit(hand, card):
if card == 1: return (hand[0] + 1, True)
// IntervalTypes
protocol IntervalType {
typealias Value
func contains(v: Value) -> Bool
}
protocol OpenEndedIntervalType: IntervalType {
typealias Value: Comparable
var val: Value { get }
}
protocol Bit {}
protocol IsSet {}
protocol IsClr {}
struct Set : Bit, IsSet {}
struct Clr : Bit, IsClr {}
protocol Op { typealias Result }
extension Op { typealias Result = Set }
@oisdk
oisdk / 2048.py
Last active November 21, 2015 23:06
#-----------------------------functional-helpers-------------------------#
from functools import reduce, partial
def compose(*funcs):
"""
Mathematical function composition.
compose(h, g, f)(x) => h(g(f(x)))
"""
@oisdk
oisdk / MinTrie.hs
Last active November 1, 2015 23:55
import qualified Data.Map as M
import Data.Foldable (Foldable, foldr)
import Prelude hiding (foldr)
import Data.Maybe (fromMaybe)
data Trie a = Trie { endHere :: Bool
, getTrie :: M.Map a (Trie a)
} deriving (Eq)
insert :: (Ord a, Foldable f) => f a -> Trie a -> Trie a
extension CollectionType where Generator.Element : Equatable {
private func indexOf(from: Index)(_ e: Generator.Element) -> Index? {
for i in from..<endIndex {
if self[i] == e {
return i
}
}
return nil
}
}
if let
jsonResponse = jsonRoot["response"] as? [String:AnyObject],
jsonCategories = jsonResponse["categories"] as? [String:AnyObject],
book = jsonCategories["on_book"] as? [AnyObject] {
for some in book {
}
}
public protocol Summable {
static func +(_: Self, _: Self) -> Self
static var zero: Self { get }
}
extension Dictionary where Value: Summable {
init<S : SequenceType where S.Generator.Element == (Key,Value)>(_ s: S) {
self = [:]
for (k,v) in s { self[k] = self[k].map { p in p + v } ?? v }
}
protocol _AnyTuple : CustomStringConvertible { var tDesc: String { get } }
struct EmptyTuple: _AnyTuple {
var description: String { return "()" }
var tDesc: String { return ")" }
}
struct NonEmptyTuple<Element, Tail : _AnyTuple> { var (first, tail): (Element, Tail) }
extension NonEmptyTuple : Tuple, _AnyTuple {