This file contains hidden or 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
| /* | |
| 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: |
This file contains hidden or 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 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 file contains hidden or 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
| -- 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 |
This file contains hidden or 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
| 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 } | |
| } | |
| } |
This file contains hidden or 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
| /* | |
| 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) | |
| } | |
| /* |
This file contains hidden or 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
| { | |
| "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 |
This file contains hidden or 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 Data.List | |
| import Data.Ord | |
| data Field = Str String | |
| | Num Double | |
| deriving (Show, Eq, Ord) | |
| data Column = Column { fields::[Field] } | |
| deriving (Show, Eq, Ord) |
This file contains hidden or 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
| -- 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 |
This file contains hidden or 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
| // 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 | |
| } |
This file contains hidden or 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 `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> { |