Comments (6)
Whoa, also I just realized that computed properties can be recursive, and so you don't even listParser
to be a function:
let eol = Parser<Void> { str in str == "" ? () : nil }
var listParser: Parser<List> {
oneOf([
eol.map { .empty },
zip(int, oneOf([literal(","), eol]))
.flatMap { head, _ in
listParser
.map { tail in .cons(head, tail) }
}
])
}
Only strange thing is that Swift has this warning even though the recursion is safe :/
from episode-code-samples.
Hey @tapcast-kevin, good question!
We really hope to get back to parsers soon, there's still so much left to talk about, including this recursion problem. I'll sketch out a simplified example of how to do recursive parsing and maybe you can extrapolate it to your use case.
Say you had this recursive enum:
indirect enum List {
case empty
case cons(Int, List)
}
And you wanted to parse strings like "1,2,3,4"
into it. You can actually construct a parser that calls itself, you just have to express the parser as a function and make sure to call itself lazily:
let eol = Parser<Void> { str in str == "" ? () : nil }
func listParser() -> Parser<List> {
oneOf([
// If there is nothing left to parse then we can end with the `.empty` List
eol.map { .empty },
// If there's stuff left to parse then we can parse off a single int and
// an (optional) delimiter, and then `flatMap` onto that so that we
// can lazily run the parser again
zip(int, oneOf([literal(","), eol]))
.flatMap { head, _ in
listParser().map { tail in .cons(head, tail) }
}
])
}
And then to use it:
let p = listParser()
print(p.run("1,2,3").match!)
Hope that helps!
(NB: these code samples are based off where we left off in episode 64)
from episode-code-samples.
Thanks @mbrandonw! I'm getting closer, but I need to study this a bit more to fully grok it. Thanks for the pointer
from episode-code-samples.
I ended up writing a similar "parsing" system for reading binary data from a mutable instance of Data (NSData). There wasn't any recursion to deal with, so that kept things simple. That exercise greatly increased my understanding of zip, map, flatMap, and even contraMap (pullbacks that I used for going in the other direction; writing binary). After that exercise, I came back to this problem and your solution now makes sense to me AND it works too! ;) I have a simple JSON parser in not much code, which is amazing to me. I have wanted to dive into this (combinator) style of parsing for a while, but it has been so foreign to my OOP/imperative/LALR mindset. Thanks again for the help and all of the videos!
from episode-code-samples.
@tapcast-kevin this is really awesome to hear, I'm glad stuff is clicking!
(pullbacks that I used for going in the other direction; writing binary)
Interesting! When we get back into parsing episodes (hopefully a few months from now) we will be discussing the idea of "invertible parsing", which is the ability to simultaneously parse and print. Sounds like you are experimenting with something similar, which is cool!
from episode-code-samples.
I'm not there yet, but I'm working my way in that direction. That is indeed a fascinating topic.
from episode-code-samples.
Related Issues (20)
- 0167 Navigation is weird on iPad HOT 1
- Please fix the error Episode #76 ~ HOT 2
- [0171-modularization-pt1] Setting Up HOT 3
- modularization folders architecture HOT 1
- Missing episode 187: URL Route? HOT 1
- Cannot convert value of type - Examples will not compile using Xcode 13.3 HOT 2
- Request: Re-Record Episode 100 to 103 and Update Source Code HOT 3
- Episode 196: Runtime warning work only in half of the cases HOT 1
- minor bug in episode 198 sample HOT 1
- Code sample for episode 220 is missing HOT 1
- Strange navigation on episode 220 HOT 1
- Episode 2 -> Fish playground -> Ambiguous use of 'init(utf8String:)'
- Episode 0228 - Composable Navigation: Destinations sample code not found HOT 1
- navigationDestination not triggered HOT 3
- Modern SwiftUI: Standups App HOT 1
- 0148 Derived Behaviour - Animation HOT 2
- Is the source code for episode 150 unavailable? HOT 1
- Missing samples episodes code HOT 1
- @FocusState not working with new @BindableState API HOT 5
- UIApplication.shared.windows deprecated in iOS 15 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from episode-code-samples.