Skip to content

Instantly share code, notes, and snippets.

View alskipp's full-sized avatar

Al Skipp alskipp

View GitHub Profile
/*
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 / 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"),
@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 / 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 / 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 / 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 / table_sort.hs
Created June 7, 2015 12:05
Haskell example of spreadsheet-like column sorting
import Data.List
import Data.Ord
data Field = Str String
| Num Double
deriving (Show, Eq, Ord)
data Column = Column { fields::[Field] }
deriving (Show, Eq, Ord)
@alskipp
alskipp / flatMappyMonady.hs
Last active August 29, 2015 14:26
Fear not the flatMapper
-- a function that accepts 2 monad args ‘containing’ number types and multiplies the ‘contents’
mult a b =
a >>= \x ->
b >>= \y -> return (x * y)
mult (Just 2) (Just 3)
-- Just 6
mult Nothing Nothing
-- Nothing
@alskipp
alskipp / monoid.swift
Last active February 3, 2018 22:56
Swiftly towards the monoid
// erm… this Num protocol is useless, but let's overlook this bit for now, shall we?
protocol Num: IntegerLiteralConvertible, IntegerArithmeticType {}
extension Int: Num {}
protocol Monoid {
static func mempty() -> Self
static func mappend(a: Self, _ b: Self) -> Self
static func mconcat(a: [Self]) -> Self
}
@alskipp
alskipp / List_monoid_foldable.swift
Last active February 3, 2018 22:58
Foldable built on a bedrock of Monoids
/*
If `Monoid` and `Foldable` are predefined, it's possible to create data structures that
receive many default methods, simply by implementing the `Monoid` and `Foldable` protocols.
These data structures could be Trees or Lists, or the built in Array.
Here's an example with a basic implementation of a List:
*/
enum List<Element> {