This file contains 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
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)) | |
} | |
} |
This file contains 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 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) |
This file contains 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
// IntervalTypes | |
protocol IntervalType { | |
typealias Value | |
func contains(v: Value) -> Bool | |
} | |
protocol OpenEndedIntervalType: IntervalType { | |
typealias Value: Comparable | |
var val: Value { get } | |
} |
This file contains 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
protocol Bit {} | |
protocol IsSet {} | |
protocol IsClr {} | |
struct Set : Bit, IsSet {} | |
struct Clr : Bit, IsClr {} | |
protocol Op { typealias Result } | |
extension Op { typealias Result = Set } |
This file contains 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
#-----------------------------functional-helpers-------------------------# | |
from functools import reduce, partial | |
def compose(*funcs): | |
""" | |
Mathematical function composition. | |
compose(h, g, f)(x) => h(g(f(x))) | |
""" |
This file contains 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 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 |
This file contains 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
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 | |
} | |
} |
This file contains 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
if let | |
jsonResponse = jsonRoot["response"] as? [String:AnyObject], | |
jsonCategories = jsonResponse["categories"] as? [String:AnyObject], | |
book = jsonCategories["on_book"] as? [AnyObject] { | |
for some in book { | |
} | |
} | |
This file contains 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
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 } | |
} |
This file contains 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
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 { |