Skip to content

Commit

Permalink
56
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed May 6, 2019
1 parent 35e98db commit 6f83bbf
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
65 changes: 65 additions & 0 deletions 0056-what-is-a-parser-pt1/Parsers.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

Int("42")
Int("42-")
Double("42")
Double("42.32435")
Bool("true")
Bool("false")
Bool("f")

import Foundation

UUID.init(uuidString: "DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF")
UUID.init(uuidString: "DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEE")
UUID.init(uuidString: "DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEZ")

URL.init(string: "https://www.pointfree.co")
URL.init(string: "^https://www.pointfree.co")

let components = URLComponents.init(string: "https://www.pointfree.co?ref=twitter")
components?.queryItems

let df = DateFormatter()
df.timeStyle = .none
df.dateStyle = .short
type(of: df.date(from: "1/29/17"))
df.date(from: "-1/29/17")


let emailRegexp = try NSRegularExpression(pattern: #"\S+@\S+"#)
let emailString = "You're logged in as [email protected]"
let emailRange = emailString.startIndex..<emailString.endIndex
let match = emailRegexp.firstMatch(
in: emailString,
range: NSRange(emailRange, in: emailString)
)!
emailString[Range(match.range(at: 0), in: emailString)!]

//let scanner = Scanner.init(string: "A42 Hello World")
//var int = 0
//scanner.scanInt(&int)
//int

// 40.6782° N, 73.9442° W
struct Coordinate {
let latitude: Double
let longitude: Double
}

func parseLatLong(_ str: String) -> Coordinate? {
let parts = str.split(separator: " ")
guard parts.count == 4 else { return nil }
guard
let lat = Double(parts[0].dropLast()),
let long = Double(parts[2].dropLast())
else { return nil }
let latCard = parts[1].dropLast()
guard latCard == "N" || latCard == "S" else { return nil }
let longCard = parts[3]
guard longCard == "E" || longCard == "W" else { return nil }
let latSign = latCard == "N" ? 1.0 : -1
let longSign = longCard == "E" ? 1.0 : -1
return Coordinate(latitude: lat * latSign, longitude: long * longSign)
}

print(parseLatLong("40.6782% N- 73.9442% W"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos'>
<timeline fileName='timeline.xctimeline'/>
</playground>
5 changes: 5 additions & 0 deletions 0056-what-is-a-parser-pt1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [Point-Free](https://www.pointfree.co)

> #### This directory contains code from Point-Free Episode: [What Is a Parser?: Part 1](https://www.pointfree.co/episodes/ep56-what-is-a-parser-part-1)
>
> Parsing is a difficult, but surprisingly ubiquitous programming problem, and functional programming has a lot to say about it. Let’s take a moment to understand the problem space of parsing, and see what tools Swift and Apple gives us to parse complex text formats.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ This repository is the home of code written on episodes of
1. [Swift Syntax Enum Properties](0053-swift-syntax-enum-properties)
1. [Advanced Swift Syntax Enum Properties](0054-advanced-swift-syntax-enum-properties)
1. [Swift Syntax Command Line Tool](0055-swift-syntax-command-line-tool)
1. [What Is a Parser?: Part 1](0056-what-is-a-parser-pt1)

0 comments on commit 6f83bbf

Please sign in to comment.