From 6f83bbfa1f7b881e9d1bf8a26881ed2c23ec80ca Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 6 May 2019 10:44:09 -0400 Subject: [PATCH] 56 --- .../Parsers.playground/Contents.swift | 65 +++++++++++++++++++ .../Parsers.playground/contents.xcplayground | 4 ++ 0056-what-is-a-parser-pt1/README.md | 5 ++ README.md | 1 + 4 files changed, 75 insertions(+) create mode 100644 0056-what-is-a-parser-pt1/Parsers.playground/Contents.swift create mode 100644 0056-what-is-a-parser-pt1/Parsers.playground/contents.xcplayground create mode 100644 0056-what-is-a-parser-pt1/README.md diff --git a/0056-what-is-a-parser-pt1/Parsers.playground/Contents.swift b/0056-what-is-a-parser-pt1/Parsers.playground/Contents.swift new file mode 100644 index 00000000..e62adcc6 --- /dev/null +++ b/0056-what-is-a-parser-pt1/Parsers.playground/Contents.swift @@ -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 blob@pointfree.co" +let emailRange = emailString.startIndex.. 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")) diff --git a/0056-what-is-a-parser-pt1/Parsers.playground/contents.xcplayground b/0056-what-is-a-parser-pt1/Parsers.playground/contents.xcplayground new file mode 100644 index 00000000..63b6dd8d --- /dev/null +++ b/0056-what-is-a-parser-pt1/Parsers.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/0056-what-is-a-parser-pt1/README.md b/0056-what-is-a-parser-pt1/README.md new file mode 100644 index 00000000..7cb7ef37 --- /dev/null +++ b/0056-what-is-a-parser-pt1/README.md @@ -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. diff --git a/README.md b/README.md index 8bdfebb7..c39c9f9c 100644 --- a/README.md +++ b/README.md @@ -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)