Skip to content

Instantly share code, notes, and snippets.

View alskipp's full-sized avatar

Al Skipp alskipp

View GitHub Profile
@alskipp
alskipp / example.json
Last active August 29, 2015 14:22
iTunes JSON parsing example in Swift
{
"resultCount":1,
"results": [
{"isGameCenterEnabled":false,
"screenshotUrls":["http://a4.mzstatic.com/us/r30/Purple7/v4/8d/94/3a/8d943ac7-77de-2011-d920-ccf1c0eb0cd2/screen1136x1136.jpeg", "http://a4.mzstatic.com/us/r30/Purple7/v4/ca/cd/1c/cacd1ca9-987c-9ad5-1861-67a9ff0653e7/screen1136x1136.jpeg", "http://a5.mzstatic.com/us/r30/Purple7/v4/30/26/af/3026af60-9efd-771c-8a42-5183fdc74df2/screen1136x1136.jpeg", "http://a5.mzstatic.com/us/r30/Purple7/v4/d6/4d/2f/d64d2f39-da72-cc1e-35bd-f52623d1a5a0/screen1136x1136.jpeg"],
"ipadScreenshotUrls":["http://a5.mzstatic.com/us/r30/Purple5/v4/ad/23/12/ad2312c5-a5b1-bc75-3e84-523981013388/screen480x480.jpeg", "http://a4.mzstatic.com/us/r30/Purple1/v4/14/6b/cd/146bcde2-8461-604b-0722-f8744b28dac9/screen480x480.jpeg", "http://a4.mzstatic.com/us/r30/Purple5/v4/c5/a1/48/c5a14861-13fb-18bb-0752-e0d13b4d2c3c/screen480x480.jpeg", "http://a4.mzstatic.com/us/r30/Purple7/v4/c2/36/25/c2362536-f6fc-4ef6-2a03-9b899ca00af9/screen480x480.jpeg"], "artworkUrl60":"http://i
@alskipp
alskipp / reduce1_maxBy_minBy.swift
Created May 6, 2015 10:45
A safe version of Haskell’s foldl1 for Swift (+ minBy & maxBy functions)
/*
Pipe forward operator:
Applies the function on the right to the value on the left
*/
infix operator |> {associativity left precedence 95}
func |> <A,B>(x:A, f:A -> B) -> B {
return f(x)
}
/*
@alskipp
alskipp / powerset.swift
Last active November 14, 2015 09:10
Powerset function in Swift using filterM
func pure<A>(x:A) -> [A] {
return [x]
}
func filterM<A>(xs:[A], predicate: A -> [Bool]) -> [[A]] {
return reduce(xs, pure([])) { acc, x in
predicate(x).flatMap { b in b ? (acc.flatMap { pure($0 + [x]) }) : acc }
}
}
@alskipp
alskipp / filter_colors.hs
Last active January 6, 2024 07:23
Filtering arrays of enums (Haskell / Swift 1.2 / Swift 2.0 comparison)
-- data structure for RGBA, RGB & CMYK colors
data Color = RGBA Float Float Float Float
| RGB Float Float Float
| CMYK Float Float Float Float
deriving (Eq, Ord, Show)
-- list of colors
colors = [ RGBA 0.1 0.1 0.5 0.2
, RGBA 1 1 0 0.9
, RGB 0.5 0.5 0
@alskipp
alskipp / nato_alpha.hs
Last active November 14, 2015 09:12
NATO Phonetic Alphabet – Haskell vs Swift
import Data.Char
import Data.Maybe
letters :: [(Char, String)]
letters = [
('A', "Alpha"), ('B', "Bravo"), ('C', "Charlie"),
('D', "Delta"), ('E', "Echo"), ('F', "Foxtrot"),
('G', "Golf"), ('H', "Hotel"), ('I', "India"),
('J', "Juliett"),('K', "Kilo"), ('L', "Lima"),
('M', "Mike"), ('N', "November"),('O', "Oscar"),
/*
This is a demonstration of how to implement the Optional type in Swift.
The name 'Maybe' is taken from Haskell, but the two cases 'None' & 'Some'
are the same as Swift's Optional type (Haskell uses 'Nothing' & 'Just').
The Maybe type conforms to NilLiteralConvertible, as does Swift's
Optional type. This allows a Maybe value to be constructed from 'nil'.
One aspect of Swift's Optional type which can't be reproduced is
'implicit Optional wrapping'. Here's an example:
@alskipp
alskipp / beasts_to_feed.hs
Last active August 29, 2015 14:14
Swift cat care with enums and protocols (+ Haskell for a varied diet)
type MilliLitresWaterPerGram = Double
type CaloriesPerGram = Double
type Grams = Double
data Food = Dry CaloriesPerGram
| Wet CaloriesPerGram MilliLitresWaterPerGram
deriving (Show, Eq)
data Animal = Cat { clawLength :: Double, calorieCount :: Double, hydrationLevel :: Double }
| Dog { barkVolume :: Double, calorieCount :: Double, hydrationLevel :: Double }
@alskipp
alskipp / haskell_towers.hs
Created January 14, 2015 15:01
The Towers of Hanoi – Haskell/Swift comparison
type Peg = String
type Move = (Peg, Peg)
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi 0 _ _ _ = []
hanoi n a b c = hanoi (n-1) a c b ++ [(a,b)] ++ hanoi (n-1) c b a
{-
Usage:
hanoi 3 "a" "b" "c"
@alskipp
alskipp / creditCardValidationA.swift
Last active April 30, 2016 14:12
Credit card validation in Swift for Swift London Meetup
// Swift 2.0
/* Function composition operator */
infix operator •> { associativity left precedence 150 }
func •> <A,B,C> (f:A -> B, g:B -> C ) -> A -> C {
return { g(f($0)) }
}
extension Array where T: IntegerType {
func sum() -> T {
@alskipp
alskipp / encrypt_xor1.swift
Last active November 1, 2018 11:10
Swift encrypt/decrypt string using XOR
import Foundation
extension Character {
func utf8() -> UInt8 {
let utf8 = String(self).utf8
return utf8[utf8.startIndex]
}
}
func encrypt(c:Character, key:Character) -> String {