4 ms·
Say you want to connect to a database, fetch some data, and present it using HTML. Here's how it can be done using a DSL based on Scheme macros: http://brl.cod
by brlewis 16y ago
Say you want to connect to a database, fetch some data, and present it using HTML. Here's how it can be done using a DSL based on Scheme macros:
http://brl.codesimply.net/brl_4.html#SEC31 http://brl.codesimply.net/brl_4.html#SEC31
I can't forget to fetch the next row from the database. I can't forget to check that I'm at the end of the result set. Grouping the results is easy and straightforward. The statement and connection objects are automatically closed when the request is done. Compare that to PHP.
I'm getting better, but even today I feel a little irritated when someone says PHP was designed for simple web/database apps. I know what a language designed for simple web/db apps looks like, and PHP is not it.
- jacquesm 16y agosql_apply("select id,somefield from sometable","applicator_function"); PHP is a programming language, it was not designed for 'simple web/database apps' any more than any other language, you can use the building blocks provided to create access at the level you require. But you can customize it so that it does become usable for 'simple web/database apps'. The above is a short sample of how I'd do the thing you describe and I don't have to remember to fetch the next row from the table and I don't have to check that I'm at the end of the result set, grouping is as easy as SQL will make it and all cleanup is automatic.
- brlewis 16y agoI want to abstract away the ways my app is like every other web app. I don't want to abstract away what's unique about it. If you abstract away everything, all languages are the same.
- btilly 16y agoPHP is a programming language, it was not designed for 'simple web/database apps' any more than any other language... Um, yes it was. See http://en.wikipedia.org/wiki/PHP#History http://en.wikipedia.org/wiki/PHP#History to verify that PHP started as a bunch of CGI binaries to serve some simple dynamic web pages. The database bit came later, but at the start it was explicitly designed for quick and dirty web pages.
- jacquesm 16y agoYou can't seriously compare the state of PHP today with the first released version. You could make such statements about every other mainstream language.
- eru 16y agoAt least the Lisp guys have figured out how to write parsers and lexers by now. (They even grok lexical scoping.) I guess we can't blame PHP for preserving the $ in front of variable-names with all the legacy code lying around. But the following should parse in a sane language: $width = getimagesize($filename)[0]; Adding support for this would not break legacy code. Nobody can tell me that naming all intermediate results like $sizes = getimagesize($img); $width = $sizes[0]; is such a preferable style, that the parser should enforce it. By the way, this is how PHP pretends to support higher order functions: function cube($n) {return($n * $n * $n);} $a = array(1, 2, 3, 4, 5); $b = array_map("cube", $a);
- btilly 16y agoI never compared the state of PHP today with the first released version. I merely used the origins of PHP to argue that PHP was, indeed, more designed for simple web applications than most other general purpose programming languages. And yes, you can make such statements about every other mainstream language. For instance Perl was more designed for text manipulation than most other general purpose programming languages. Java was more designed for multi-threading than most other general purpose languages. C was more designed for close to the machine programming than most other general purpose languages. All of these things are true. All of these things still show in various ways. Yet every one of those is a general purpose language that can be used for anything you want.
- wvenable 16y agoHave you even used PHP? There is nothing in your above statement (or linked code sample) that isn't done just as easily in PHP. Iterate over records using foreach, check. Closing database connections automatically (or out of scope), check. I could do grouping with about the same amount of code, too. PHP has it's ugly bits but it's not significantly different in capability from most other procedural/OO languages.
- brlewis 16y agoPlease show me your group-beginning and group-ending code.
- wvenable 16y agoHere's some possible PHP code to do what that Scheme code sample does (I've excluded the HTML for brevity): $result = $conn->execute('select * from favcolor order by color, name'); while($row = $result->next()) { if ($result->groupBeginning('color')) { echo $row['color'].': '.$row['name']; } else { echo ', '.$row['name']; } if ($result->groupEnding('color')) echo "\n"; } echo 'Count:'.$result->count(); I just came up that off the top of my head, but there's nothing impossible in there. Also, instead of field name for groupBeginning(), passing a closure would also be possible.
- shaunxcode 16y agoI'll raise you my bleeding edge dsl: echo $conn->execute('select * from favcolor order by color, name') ->groupBy(L(val)->color) ->map(L(key)->wrap('<li><b>', '</b>') ->append( ': ', L(val)->map(L(val)->name)->join(','), '</li>')) ->join("\n");
- wvenable 16y agoNice. I've done something similar myself but I didn't take it exactly in the same direction. I leave much of grouping, wrapping, appending work to my own template engine layer. Is the source to your DSL available?
- ninjac0der 16y agoThis just looks like a massive step back from a MVC setup to me. How do proponents of using Lisp for web development handle interaction with designers?
- brlewis 16y agoThis is just one style, not representative of all Lisp web development. You can have any style you want. With continuations you can even use a procedural style where your program describes a workflow of multiple pages. (Whether MVC is the right style is a topic for another discussion.)
- ninjac0der 16y agoGood too know. I would like to give Lisp a try, but I've only just being able to sell a php shop on migrating to python/django. Lisp is a near-impossible sell to every web dev shop I've worked in.
- brehaut 16y agoRegarding interaction with designers, I believe the answer is 'It Depends'. As an example, in the Clojure space there are two libraries that are good exemplars of the ends of the spectrum: Hiccup and Enlive. Hiccup (http://github.com/weavejester/hiccup http://github.com/weavejester/hiccup) uses sexpressions to define the (ht|x)ml output. Effectively either the developer and designer roles are fused or the developer gets a design from the designer and encodes it into hiccup manually. Enlive (http://github.com/cgrand/enlive http://github.com/cgrand/enlive) is (in overly simplistic terms) a selector driven substitution engine. You give it an HTML file for a template and some CSS selectors describing what to modify, alternatively you define a snippet in terms of some elements within the page. In this model the designers work is first class resource, and not modified by the developer. This is exactly the same breadth of options as I've seen in just about any real language being used for development tools.
- c141charlie 16y agoThe DSL snippet is every bit as verbose as the PHP code sample, yet somebody had to write the DSL in the first place. Not sure mission was accomplished. I think a better solution is offered by Mustache logic-less templates.