Arc Forumnew | comments | leaders | submitlogin
LavaScript takes the Arc Challenge
2 points by evanrmurphy 5020 days ago | 5 comments
We're not there yet, but this would be LavaScript's [1] entry into the Arc Challenge [2], once the language gets sufficiently developed:

    ($ (fn ()
      (.html ($ 'body)
        (+ (<input>)
           (<button> "submit")))
      (.click ($ 'button) (fn ()
        (= said (.val ($ 'input)))
        (.html ($ 'body)
          (<a> href '#))
        (.click ($ 'a) (fn ()
          (.html ($ 'body)
            (+ "you said: " said))))))))
And the JavaScript output would be:

    $(function() {
      $('body').html(
        '<input></input>' +
        '<button>submit</button>');
      $('button').click(function() {
        said = $('input').val();
        $('body').html(
          '<a href="#">click here</a>');
        $('a').click(function() {
          $('body').html("you said: " + said);
        });
      });
    });
It depends on jQuery and a hypothetical HTML library that can generate strings like '<tag attr1="val1">body</tag>' from calls like (<tag> attr1 'val1 "body").

---

[1] https://github.com/evanrmurphy/lava-script

[2] http://arclanguage.org/item?id=722



1 point by evanrmurphy 5020 days ago | link

The example can be made less verbose with the help of a simple macro to eliminate the (.html ($ 'body) ...) repetition:

  ; don't confuse body the parameter
  ; with 'body the DOM element in
  ; this macro

  (mac draw body
    (.html ($ 'body) @body))

  ($ (fn ()
    (draw
      (+ (<input>) (<button> "submit")))
    (.click ($ 'button) (fn ()
      (= said (.val ($ 'input)))
      (draw (<a> href '#))
      (.click ($ 'a) (fn ()
        (draw
          (+ "you said: " said))))))))
Also note that this is a client-side only solution to the Arc Challenge problem. Since LavaScript itself is written in JavaScript and going to be made available through the NPM, server-side and hybrid solutions will also be possible using a Node web server.

-----

1 point by akkartik 5020 days ago | link

Hmm, could we get rid of the $s and the fns? Not as is, of course, but it's the first thing the eye notices.

I suppose ssyntax helps: $!body, etc.

-----

1 point by evanrmurphy 5020 days ago | link

Yeah, I see what you mean. :) Here are a few macros to combat the jQuery cruft:

  (mac ready body
    ($ (fn () @body)))

  (mac trigger (selector event args...)
    (. ($ selector) (event @args)))

  ; same macro as from http://arclanguage.org/item?id=13776
  ; but redefined in terms of the more general trigger

  (mac draw body
    (trigger 'body html @body))

  (mac handler (selector event body...)
    (trigger selector event (fn ()
      @body)))
And the example re-written using them:

  (ready
    (draw
      (+ (<input>) (<button> "submit")))
    (handler 'button click
      (= said (trigger 'input val))
      (draw 
        (<a> href '# "click here"))
      (handler 'a click
        (draw (+ "you said: " said))))))))
Do you think this is an improvement?

---

Probably the next biggest eyesore would be all the '<' and '>' characters from the html utils. You could make convenience aliases for those (e.g. input for <input> and button for <button>), and then it would look even more fluid:

  (ready
    (draw
      (+ (input) (button "submit")))
    (handler 'button click
      (= said (trigger 'input val))
      (draw 
        (a href '# "click here"))
      (handler 'a click
        (draw (+ "you said: " said))))))))

-----

1 point by evanrmurphy 5019 days ago | link

It looks like our draw macro might as well have the + string concatentation included:

  (mac draw body
    (trigger 'body html 
      (+ @body)))
Then we get even shorter:

  (ready
    (draw (input)
          (button "submit"))
    (handler 'button click
      (= said (trigger 'input val))
      (draw (a href '#
              "click here"))
      (handler 'a click
        (draw "you said: " said)))))))

-----

1 point by akkartik 5019 days ago | link

Yeah, these are certainly looking better. The crux is whether these primitives are part of a basis set (and so don't count against the token count), and whether the rest of the framework is similarly terse (no reason it can't be).

-----