behrang / SwiftParsec

Parsec implementation in Swift
MIT License
52 stars 5 forks source link

How do you feel about moving the functions into a namespace? #5

Open seivan opened 7 years ago

seivan commented 7 years ago

It's always hard with discoverability if you want to learn a library or a tool if things don't have a good access point. For me, it's usually a namespace where I let autocomplete teach me what I can use and find.

How do you feel about moving the global functions into a struct or an enum just like I did with YamlSwift?

behrang commented 7 years ago

This is almost a mirror of Parsec in Haskell. That is a functional language, and everything is a function, and the discoverability problem you mentioned is one of the key differences between functional and OO languages. Since Swift had enough functional support, and I really admire Haskell and Parsec, I chose to write this functional too.

I think it is very readable in this style, for example between(quote, quote, many(quotedCharacter) is clear and to the point, but adding a struct or enum makes reading parsers harder.

I think the current main problem in this project is performance. It is really slow, and I think the reason is that it is a complete copy of Haskell's Parsec. In Haskell, since everything is immutable, functions don't mutate variables, instead they return new values. Haskell is optimized for this and is fast when used like this, but I think Swift is not optimized for this, and so every time the input string is used, for example when one character is parsed correctly, a new copy of the input string without that character is created and returned. I think this is the main performance bottleneck.

seivan commented 7 years ago

It would rarely be a new copy unless you write to the string. Now, I understand the API would be uglier if you were to nest the functions, but at the same time, it would make it easier for beginners to come into the concept.

I'd investigate if there is a way to "unwrap" all nested functions under a namespace un the local scope so you'd get to pick which way you'd want to go.