Arc Forumnew | comments | leaders | submitlogin
A parser-combinator approach to parsing JSON (catdancer.ws)
10 points by CatDancer 5631 days ago | 6 comments


3 points by ecmanaut 5627 days ago | link

That's a beautiful example of adapting the underlying language until it makes the implementation practically a descriptive syntax for spelling out the solution in text.

I think I'll route my manager to that post, as he not ten hours ago wondered about what functional programming buys you which is difficult to do with the kind of imperative heritage he has (he grew up on Fortran, back in the day).

-----

1 point by CatDancer 5626 days ago | link

Wow, thanks!

This may not an issue for you or for your own work environment, but if I continue to publish techniques that people wish to share professionally I'm imagining that some may find the name "Cat Dancer" to be an unnecessary distraction. So I put up another copy here in case:

http://andrewwilcox.name/parser-combinator-approach-to-json....

-----

1 point by ecmanaut 5626 days ago | link

Unrelated, but I would advise against working for or with people that would either choke on an online alias (that is not even inflammatory :-), and/or fail to grasp at the quality of your work, reasoning and communication about the concepts therein, measuring your aptitude from that data.

...but I'll definitely hand you that signing it with your contact information likely makes it easier for those that do spot you to get in touch, for whichever reason. Which might sometimes be more interesting than vetting for ideal recruiters, especially if you want to work someplace that delegates recruiting to people outside of where the actual work happens, so the above paragraph gets half moot.

-----

1 point by phr 5626 days ago | link

I don't understand the use of iflet here:

  arc> (def show-parse (parser str)
          (let p (coerce str 'cons)
            (iflet (p2 r) (parser p)
               (do (pr "match: ")
                   (write r)
                   (prn " remaining: " (coerce p2 'string)))
                   (prn "no match")))
           nil)

From the Arc Cross Reference http://practical-scheme.net/wiliki/arcxref?iflet the '(p2 r) should be a variable, but I'm not seeing it.

What am I missing?

-----

3 points by rntz 5626 days ago | link

Wherever you have a "variable" to be bound in arc, you can instead use an expression containing variables. So, for example, consider the following code:

    arc> (let x '(1 2) x)
    (1 2)
    arc> (let (x y) '(1 2) x)
    1
    arc> (let (x y) '(1 2) y)
    2
The first example is straightforward: we bind the variable x to the value (1 2). In the second and third examples, we bind the variables x and y to 1 and 2 respectively, by matching the value (1 2) against the binding expression (x y). In a fairly similar way, consider the following:

    arc> (let (head . tail) '(1 2 3) (list head tail))
    (1 (2 3))
In this case, we remember that (1 2 3) is really shorthand for (1 . (2 . (3 . nil))), and so we bind head to 1 and tail to (2 . (3 . nil)), or, abbreviated, to (2 3). Hence (list head tail) returns (1 (2 3)).

This simple but powerful concept is called "destructuring", because it takes a complex structure apart into its component values.

(I'm hoping that's the part you didn't understand... sorry if I missed the point.)

-----

1 point by phr 5626 days ago | link

Thanks. Makes sense now.

-----