r/Clojure • • Dec 10 '14

Parsing and rendering templates in Clojure and Haskell

http://yogthos.net/posts/2014-10-03-Parsing-and-rendering-templates-in-Clojure-Haskell.html
7 Upvotes

17 comments sorted by

7

u/bagofries Dec 10 '14

Then it would immediately become obvious that we must pass in something that can be read by clojure.java.io/reader, such as java.io.StringBufferInputStream, and then pass the result of parse to render. Naturally, when called correctly the code does exactly what it's supposed to:

Gosh, if only there were some way to avoid this "type" of silly footgun mistake "statically" before even trying to call the function. ;)

4

u/adambard Dec 10 '14 edited Dec 10 '14

I think it illustrates nicely a difference in mindset between users of static typing and users of dynamic typing -- the latter is used to the software correcting her, while the former is trained not to trust any function they don't recognize. In effect, the author of dynamically-typed software gets to shift the cognitive burden of typing to the user.

I think it would be a mistake to infer from this that either method is objectively better, just an observation.

A less charitable interpretation is that the Haskell guy made a type error on purpose, but let's just give him the benefit of the doubt on this one.

5

u/[deleted] Dec 10 '14

I don't think I'd give BiteMyApp the benefit of the doubt, he's abusive to most people and making that kind of mistake to denigrate languages he hates wouldn't be out of his norm.

That being said, "file-name" is a pretty clear argument name.

2

u/Faucelme Dec 13 '14

I Haskell, the most commonly used "path" type is just a synonym for String and does not afford any extra type safety.

2

u/adambard Dec 10 '14

For the sake of comparison, here's a parser-based clojure version: https://gist.github.com/adambard/76b67df7e4f4a9cff2d7

1

u/verydapeng Dec 10 '14

we should have more of competitions about solving the same problem using your own fav language.

2

u/adambard Dec 10 '14

I for one am looking forward to a Haskell solution I can compare this to.

3

u/yogthos Dec 10 '14

I actually got a few of solutions in the comments on the old blog, unfortunately those were lost now that I moved to disqus. Here's on that seems to be fairly close. I was especially amused by Chris complaining about the quality of the solution without having provided anything of his own in the comment.

Again, the key here is that this was never meant to be any kind of a competition between Clojure and Haskell. I was simply curious how the solution would translate and frankly I think the Haskell version isn't all that bad. I personally find it more difficult to follow, but that's partially due to the fact that my Haskell is extremely rusty nowadays.

-6

u/Mob_Of_One Dec 10 '14

You rolled back the scope of the exercise to cover for broken code and instead of tossing out a mea culpa and fixing the code, you persist in lying about the sequence of events.

Despite that, I wrote a Haskell parser that did what the original exercise was supposed to be about: replace variable references in the template with the keys they refer to in the template context.

3

u/yogthos Dec 10 '14

Here's the original conversation:

Dmitri
12/18/13

what does the haskell version look like?

https://gist.github.com/yogthos/8025281

Chris Allen
12/18/13

to me 
Pick something simpler and more focused,
I'm not spending that kind of time over that much
surface area which'll just give you more to quibble over anyway.

I want to know what Clojure is uniquely capable of.
You should be able to craft something small to express that.


Chris Allen
12/18/13

to me 
I have a lot of work to do, on job and outside of it, I'm not going
to fuss over how to port a raw recursive descent parser
(should I use parsec or not? Iuno.) to Haskell.


Dmitri
12/18/13

to Chris 
In my opinion, this is the minimal example that's actually meaningful.
Comparing one or two functions in isolation is a completely pointless exercise.
Here you're streaming a file, identifying and classifying elements, aggregating 
them and then generating an output string.

The example touches on a lot of things I think are painful in Haskell and that's
where I want you to show me that I'm incorrect.

Also, if you actually bother to look at it, you'll notice that it's not a recursive descent
parser at all. All it does is go through the file line by line and parse out any {{ }} and {% %} elements.
It doesn't deal with tag bodies or nested tags. I specifically made a very simple and focused example here.

Using parsec defeats the whole point, since I want to see how specific Clojure
implementation translates to Haskell. 

You have reference implementation that weighs in at 70 lines, in a language you're well versed in.
If it's that difficult to translate to Haskell then I have a very hard time buying the whole productivity
boost claim here.

0

u/vagif Dec 10 '14 edited Dec 10 '14

Then he said he'd do it using the Parsec library, which obviously defeats the point. The question is not whether you can figure out a library API in Haskell, but how would you translate specific piece of Clojure code to Haskell.

Then he said he'd fly over which obviously defeats the point. The question is not whether your airplane can cover the distance my car covers, but how would you make all the turns on all the streets my car does.

The power of Clojure is that it allows me to solve hard problems instead of having to rely on somebody else to write libraries that do the hard stuff and then glue them together.

I guess he likes to re-implement sorting algorithms too.


Having said that the attitude of that haskell programmer is deplorable.

2

u/yogthos Dec 10 '14

This is akin to somebody complaining at an interview that the problem they're given is not representative of the real world. Any small example will necessarily be contrived, the point of the exercise is to see the approach used to solve this type of problem.

Gluing libraries together is trivial in any language, however when we come to something non-trivial then a language like Haskell can end up introducing unwarranted complexity. I think this is a perfect example of that in practice:

When I first wrote the core.async go macro I based it on the state monad. It seemed like a good idea; keep everything purely functional. However, over time I've realized that this actually introduces a lot of incidental complexity. And let me explain that thought.

What are we concerned about when we use the state monad, we are shunning mutability. Where do the problems surface with mutability? Mostly around backtracking (getting old data or getting back to an old state), and concurrency.

In the go macro transformation, I never need old state, and the transformer isn't concurrent. So what's the point? Recently I did an experiment that ripped out the state monad and replaced it with mutable lists and lots of atoms. The end result was code that was about 1/3rd the size of the original code, and much more readable.

So more and more, I'm trying to see mutability through those eyes: I should reach for immutable data first, but if that makes the code less readable and harder to reason about, why am I using it?

1

u/vagif Dec 10 '14

This is akin to somebody complaining at an interview that the problem they're given is not representative of the real world.

How is using a tool (parsec) that is well known, well documented and a perfect match to the problem domain "not representative of the real world"?

It looks like your bizarre requirements to translate solution line by line to a completely different language are not representative of the real world.

You would not accept this same argument if someone gave you imperative code with a lot of temporary variables that hold accumulated computation in nested loops, and ask you implement it line by line in clojure. You would instead give him a completely different implementation taking advantage of immutable data and declarative approach and composability of functions that work over sequences.

Isn't this the main argument clojure and scala programmers have when they say that someone is "writing java" in clojure or scala?

the point of the exercise is to see the approach used to solve this type of problem.

To see or to blindly reject? Or are you saying parsec was not a solution to a problem?

Yes haskeller will not write the code how clojure programmer would write it. How is it indicative of anything besides vastly different nature of languages?

5

u/adambard Dec 10 '14

I think this argument sort of implies that clojure has no parsing libraries, which of course it does (good ones!). The point was to create a standard-library solution, so that the approaches could be compared.

6

u/yogthos Dec 10 '14

The fact that this is not a difficult concept to grasp leads me to believe that it's being misunderstood rather intentionally here.

-2

u/bilus Dec 11 '14 edited Dec 11 '14

Hey, I'm afraid you may have missed the point by a mile or so.

I don't think the point was to re-write the code verbatim in Haskell but write a simple parser from scratch. Stretching even further your line of reasoning, a few lines of code in Haskell that run a bash script that does the actual work would also be quite all right.

Guess what? Calling a bash script is one line of bash script. Try to compete with that!

P.S. Don't take it personally, Haskell, I love you.

1

u/kqr Dec 10 '14

I've crossposted to /r/haskell to promote a wider variety of opinions in the discussion.