Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 3.1 / Xcode 8.3.2 #116

Merged
merged 4 commits into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ xcuserdata
*.xcuserdatad
*.xccheckout
*.xcscmblueprint
*.xctimeline
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "antitypical/Result" ~> 1.0.2
github "antitypical/Result" ~> 3.2.4
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "antitypical/Result" "1.0.2"
github "antitypical/Result" "3.2.4"
6 changes: 5 additions & 1 deletion Documentation/Collections.playground/section-2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ let input = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]

typealias Fibonacci = Parser<[Int], [Int]>.Function

func fix<T, U>(_ f: @escaping (@escaping (T) -> U) -> (T) -> U) -> (T) -> U {
return { f(fix(f))($0) }
}

let fibonacci: (Int, Int) -> Fibonacci = fix { fibonacci in
{ (x: Int, y: Int) -> Fibonacci in
let combined: Parser<[Int], [Int]>.Function = %(x + y) >>- { (xy: Int) -> Fibonacci in
Expand All @@ -12,5 +16,5 @@ let fibonacci: (Int, Int) -> Fibonacci = fix { fibonacci in
}
}

parse(fibonacci(0, 1), input: input).right
parse(fibonacci(0, 1), input: input).value

6 changes: 0 additions & 6 deletions Documentation/Collections.playground/timeline.xctimeline

This file was deleted.

1 change: 0 additions & 1 deletion Documentation/Colours.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Cocoa
import Darwin
import Madness
import Prelude
12 changes: 6 additions & 6 deletions Documentation/Colours.playground/section-2.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func toComponent(string: String) -> CGFloat {
func toComponent(_ string: String) -> CGFloat {
return CGFloat(strtol(string, nil, 16)) / 255
}

Expand All @@ -12,11 +12,11 @@ let component2: Parser<String.CharacterView, CGFloat>.Function = toComponent <^>
let three: Parser<String.CharacterView, [CGFloat]>.Function = component1 * 3
let six: Parser<String.CharacterView, [CGFloat]>.Function = component2 * 3

let colour: Parser<String.CharacterView, NSColor>.Function = %"#" *> (six <|> three) |> map {
let colour: Parser<String.CharacterView, NSColor>.Function = map({
NSColor(calibratedRed: $0[0], green: $0[1], blue: $0[2], alpha: 1)
}
})(%"#" *> (six <|> three))

let reddish = parse(colour, input: "#d52a41").right
let greenish = parse(colour, input: "#5a2").right
let blueish = parse(colour, input: "#5e8ca1").right
let reddish = parse(colour, input: "#d52a41").value
let greenish = parse(colour, input: "#5a2").value
let blueish = parse(colour, input: "#5e8ca1").value

6 changes: 0 additions & 6 deletions Documentation/Colours.playground/timeline.xctimeline

This file was deleted.

2 changes: 0 additions & 2 deletions Documentation/Lambda Calculus.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Either
import Madness
import Prelude
26 changes: 15 additions & 11 deletions Documentation/Lambda Calculus.playground/section-2.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
indirect enum Lambda: CustomStringConvertible {
case Variable(String)
case Abstraction(String, Lambda)
case Application(Lambda, Lambda)
case variable(String)
case abstraction(String, Lambda)
case application(Lambda, Lambda)

var description: String {
switch self {
case let Variable(symbol):
case let .variable(symbol):
return symbol
case let Abstraction(symbol, body):
case let .abstraction(symbol, body):
return "λ\(symbol).\(body.description)"
case let Application(x, y):
case let .application(x, y):
return "(\(x.description) \(y.description))"
}
}
}

typealias LambdaParser = Parser<String.CharacterView, Lambda>.Function

func fix<T, U>(_ f: @escaping (@escaping (T) -> U) -> (T) -> U) -> (T) -> U {
return { f(fix(f))($0) }
}

let lambda: LambdaParser = fix { lambda in
let symbol: StringParser = %("a"..."z")

let variable: LambdaParser = Lambda.Variable <^> symbol
let abstraction: LambdaParser = Lambda.Abstraction <^> ( lift(pair) <*> (%"λ" *> symbol) <*> (%"." *> lambda) )
let application: LambdaParser = Lambda.Application <^> ( lift(pair) <*> (%"(" *> lambda) <*> (%" " *> lambda) <* %")" )
let variable: LambdaParser = Lambda.variable <^> symbol
let abstraction: LambdaParser = Lambda.abstraction <^> ( lift(pair) <*> (%"λ" *> symbol) <*> (%"." *> lambda) )
let application: LambdaParser = Lambda.application <^> ( lift(pair) <*> (%"(" *> lambda) <*> (%" " *> lambda) <* %")" )

return variable <|> abstraction <|> application
}

parse(lambda, input: "λx.(x x)").right?.description
parse(lambda, input: "(λx.(x x) λx.(x x))").right?.description
parse(lambda, input: "λx.(x x)").value?.description
parse(lambda, input: "(λx.(x x) λx.(x x))").value?.description

6 changes: 0 additions & 6 deletions Documentation/Lambda Calculus.playground/timeline.xctimeline

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import Madness
import Prelude
36 changes: 20 additions & 16 deletions Documentation/Subset of Common Markdown.playground/section-2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ let lower = %("a"..."z")
let upper = %("A"..."Z")
let digit = %("0"..."9")
let text = lower <|> upper <|> digit <|> ws
let restOfLine = { $0.joinWithSeparator("") } <^> many(text) <* newline
let texts = { $0.joinWithSeparator("") } <^> some(text <|> (%"" <* newline))
let restOfLine = { $0.joined(separator: "") } <^> many(text) <* newline
let texts = { $0.joined(separator: "") } <^> some(text <|> (%"" <* newline))

// MARK: - AST

enum Node: CustomStringConvertible {
case Blockquote([Node])
case Header(Int, String)
case Paragraph(String)
case blockquote([Node])
case header(Int, String)
case paragraph(String)

func analysis<T>(ifBlockquote ifBlockquote: [Node] -> T, ifHeader: (Int, String) -> T, ifParagraph: String -> T) -> T {
func analysis<T>(ifBlockquote: ([Node]) -> T, ifHeader: (Int, String) -> T, ifParagraph: (String) -> T) -> T {
switch self {
case let Blockquote(nodes):
case let .blockquote(nodes):
return ifBlockquote(nodes)
case let Header(level, text):
case let .header(level, text):
return ifHeader(level, text)
case let Paragraph(text):
case let .paragraph(text):
return ifParagraph(text)
}
}
Expand All @@ -31,7 +31,7 @@ enum Node: CustomStringConvertible {

var description: String {
return analysis(
ifBlockquote: { "<blockquote>" + $0.lazy.map{ $0.description }.joinWithSeparator("") + "</blockquote>" },
ifBlockquote: { "<blockquote>" + $0.lazy.map{ $0.description }.joined(separator: "") + "</blockquote>" },
ifHeader: { "<h\($0)>\($1)</h\($0)>" },
ifParagraph: { "<p>\($0)</p>" })
}
Expand All @@ -41,26 +41,30 @@ enum Node: CustomStringConvertible {
// MARK: - Parsing rules

typealias NodeParser = Parser<String.CharacterView, Node>.Function
typealias ElementParser = StringParser -> NodeParser
typealias ElementParser = (@escaping StringParser) -> NodeParser

func fix<T, U>(_ f: @escaping (@escaping (T) -> U) -> (T) -> U) -> (T) -> U {
return { f(fix(f))($0) }
}

let element: ElementParser = fix { element in
{ prefix in

let octothorpes: IntParser = { $0.count } <^> (%"#" * (1..<7))
let header: NodeParser = prefix *> ( Node.Header <^> (lift(pair) <*> octothorpes <*> (%" " *> restOfLine)) )
let paragraph: NodeParser = prefix *> ( Node.Paragraph <^> texts )
let blockquote: NodeParser = prefix *> { ( Node.Blockquote <^> some(element(prefix *> %"> ")) )($0, $1) }
let header: NodeParser = prefix *> ( Node.header <^> (lift(pair) <*> octothorpes <*> (%" " *> restOfLine)) )
let paragraph: NodeParser = prefix *> ( Node.paragraph <^> texts )
let blockquote: NodeParser = prefix *> { ( Node.blockquote <^> some(element(prefix *> %"> ")) )($0, $1) }

return header <|> paragraph <|> blockquote
}
}

let parser = many(element(pure("")))
if let parsed = parse(parser, input: "> # hello\n> \n> hello\n> there\n> \n> \n").right {
if let parsed = parse(parser, input: "> # hello\n> \n> hello\n> there\n> \n> \n").value {
let description = parsed.reduce(""){ $0 + $1.description }
}

if let parsed = parse(parser, input: "This is a \nparagraph\n> # title\n> ### subtitle\n> a").right {
if let parsed = parse(parser, input: "This is a \nparagraph\n> # title\n> ### subtitle\n> a").value {
let description = parsed.reduce(""){ $0 + $1.description }
}

This file was deleted.

Loading