-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35e98db
commit 6f83bbf
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
0056-what-is-a-parser-pt1/Parsers.playground/Contents.swift
This file contains 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
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")) |
4 changes: 4 additions & 0 deletions
4
0056-what-is-a-parser-pt1/Parsers.playground/contents.xcplayground
This file contains 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
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> |
This file contains 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
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. |
This file contains 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