Live data from Hacker News

I’ve Consed Every Pair

medium.com

61–70 of 228 posts

Re: I’ve Consed Every Pair

#61
post #36

Earlier quoted context omitted.

the speed you gain by being able to whip up a feature in 10 minutes is worth the pain of an occasional dead link This is everything that is wrong with the software industry, summarized in one sentence. Speed gain enjoyed by developers is paid for by the users in pain. It used to be that developers would go through tremendous amounts of pain just to squeeze out a few instructions from a UI drawing routine in order to…

Not at all. I share your frustration. Closures on a server are a powerful way of representing data flows. But they come at a cost: the links expire after some time. How do you strike a balance? The simplest way is to put in the extra time to make everything into a persistent link. But, that's equivalent to removing all the benefits of lexical scoping. If you've ever created an inner function before, you know how powe…

Since this is getting a surprising amount of interest, let me sum up the technique here. It's really not hard to implement it in Javascript using Express.

1. inside of your express endpoint, create a closure that captures some state. For example, the user's IP address.

(This is a dumb example, but the point is that you can capture whatever state you want. Some cases are mentioned here: http://paulgraham.com/road.html and here: http://ep.yimg.com/ty/cdn/paulgraham/bbnexcerpts.txt)

EDIT: I updated this to capture the date + time the original page was loaded, which is slightly more compelling than a mere IP address.

  app.get('/', function (req, res) {
    let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;

    let now = new Date();
    let date = now.getFullYear()+'-'+(now.getMonth()+1)+'-'+now.getDate();
    let time = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();

    let fn = (req, res) => {
      res.send(`hello ${req.query.name}. On ${date} at {time}, your IP address was ${ip}`)
    }
    ... see below ...
  })
2. insert that closure into a global hash table, keyed by a random ID.

  g_fnids = {};

  app.get('/', function (req, res) {
    let fn = ...
    let id = 
    g_fnids[id] = fn;
    res.send(`Say hello`);
  })
3. create an endpoint called /x which works like `/x?fnid=&foo=1&bar=2`. Use to look up the closure. Call the closure, passing the request to it:

  app.get('/x', function (req, res) {
    let id = req.query.fnid;
    let fn = g_fnids[id];
    fn(req, res)
  }
Done.

Congratulations, your closure is now an express endpoint. Except you didn't have to name it. You can link users to it like `&name=bob">Say hello`.

The reason this is a powerful technique is that you can use it with forms. The form target can be /x, and the query params are whatever the user types into the form fields.

I bet you already see a few interesting use cases. And you might notice that this makes scaling the server a little more difficult, since incoming requests have to be routed to the server containing the actual closure. But in the meantime, you now have "inner functions" in your web framework. It makes implementing password reset functionality completely trivial, and no database required.

If it seems slightly annoying to use – "I thought you said this was a productivity boost. But it's annoying to type all of that!" – lisp macros hide all of this boilerplate code, so there's zero extra typing. You can get lisp macros for Javascript using Lumen lisp: https://github.com/sctb/lumen

Even without macros, though, I bet this technique is shorter. Suppose you had to store the date + time + IP address somewhere. Where would you put it? I assume some sort of nosql database like firebase. But wouldn’t that code be much longer and more annoying to write? So this technique has tremendous value, and I’m amazed no one is using it circa 2020.

Re: I’ve Consed Every Pair

#62

Arc is underrated as an information management tool. There's something to be said for having a web framework that works out of the box. Rails is probably the only other framework that makes it as easy to "just make some forms that pass data around and run some code on that data." But not quite -- I haven't seen arc's closure-storing technique used in any other web framework. The main issue that arc solves is that it…

> I haven't seen arc's closure-storing technique used in any other web framework.

Apache Cocoon?

https://cocoon.apache.org/2.2/core-modules/core/2.2/1373_1_1...

Re: I’ve Consed Every Pair

#63
post #49

Earlier quoted context omitted.

There are about three possible choices 1) he knew he did LISP like languages and wanted to share 2) he didn't know he did LISP like languages and wanted to share 3) he was showing off. 4) the other choices. I went to 3) based on my 40 years experience in computer science.

Maybe he just didn't recognize his face? Or might have recognized the name but lacked knowledge about his career? Remember Hanlon's razor: "Never attribute to malice that which is adequately explained by stupidity." Or, more politely and from a different angle, don't assume that the collection of facts that you happen to be aware of is obvious to everyone.

Ironically, this is wise advice.

Re: I’ve Consed Every Pair

#64

I'll admit I don't think I've really noticed the presence of lisp online other than when people want to talk about lisp. Can someone share some practical examples of where lisp is being used? Maybe a popular open source project I never realised was written in a lisp family language?

It's a little sad that it's declined so far. In a way, it's a victim of its own success--it's so easy to write a lisp system that literally dozens, if not hundreds, of variants sprang up. The community was divided, and it never quite came through.

IIRC, our fearless leader PG made his zillions using Lisp.

Re: I’ve Consed Every Pair

#65
post #49

Earlier quoted context omitted.

There are about three possible choices 1) he knew he did LISP like languages and wanted to share 2) he didn't know he did LISP like languages and wanted to share 3) he was showing off. 4) the other choices. I went to 3) based on my 40 years experience in computer science.

Maybe he just didn't recognize his face? Or might have recognized the name but lacked knowledge about his career? Remember Hanlon's razor: "Never attribute to malice that which is adequately explained by stupidity." Or, more politely and from a different angle, don't assume that the collection of facts that you happen to be aware of is obvious to everyone.

mentos xkcd applies

Re: I’ve Consed Every Pair

#67

I'll admit I don't think I've really noticed the presence of lisp online other than when people want to talk about lisp. Can someone share some practical examples of where lisp is being used? Maybe a popular open source project I never realised was written in a lisp family language?

GDB and GIMP both embed a Scheme interpreter.

Re: I’ve Consed Every Pair

#68

I'll admit I don't think I've really noticed the presence of lisp online other than when people want to talk about lisp. Can someone share some practical examples of where lisp is being used? Maybe a popular open source project I never realised was written in a lisp family language?

There's a list of some companies that use Clojure here[1].

I use it for my startup. It's great for web dev. There are some really cool databases written with Clojure also.[2][3]

[1] https://clojure.org/community/success_stories

[2] https://opencrux.com/

[3] https://www.datomic.com/

Re: I’ve Consed Every Pair

#69

I'll admit I don't think I've really noticed the presence of lisp online other than when people want to talk about lisp. Can someone share some practical examples of where lisp is being used? Maybe a popular open source project I never realised was written in a lisp family language?

It's a little sad that it's declined so far. In a way, it's a victim of its own success--it's so easy to write a lisp system that literally dozens, if not hundreds, of variants sprang up. The community was divided, and it never quite came through. IIRC, our fearless leader PG made his zillions using Lisp.

> it never quite came through.

Hard disagree. The Clojure ecosystem is great.

Re: I’ve Consed Every Pair

#70

I remember reading something someplace that was something like ... the problem with being old and full of wisdom is it's hard to share that wisdom with the people that need it most without sounding like an old condescending jerk. Plus, they won't listen anyways. The best you can hope for is that when they are old and full of wisdom they'll remember you and think "hey that old guy was right" . I know when I was 25 I w…

A while ago I was arguing with someone online about the best way to structure a matrix library. Then around four years later I realized he was completely right and I just didn't know enough to realize why. I'm sure this is a universal experience.
Post reply on HN