Arc Forumnew | comments | leaders | submit | rocketnia's commentslogin
2 points by rocketnia 5298 days ago | link | parent | on: Sorting a list by most common elements.

I don't know how much this is going to help, but I have several suggestions:

a) Where you say "mappend [do _]", you can just say "apply + nil". Actually, I've been using "apply join", but it turns out that performs much worse:

  arc> (time10:mappend idfn (n-of 10000 nil))
  time: 468 msec.
  nil
  arc> (time10:apply join (n-of 10000 nil))
  time: 50454 msec.
  nil
  arc> (time10:apply + nil (n-of 10000 nil))
  time: 437 msec.
  nil
b) It might help to test with both '< and '>.

c) Not that it'll really save anything when it comes to the overall computational complexity, but this version of list length comparison will only traverse the lists up to the end of the shorter one:

  (def longer-list (a b)
    (if a
      (if b
        (longer-list cdr.a cdr.b)
        t)
      nil))
d) Where you say "(if empty.xs ys <else>)", you can probably squeeze out more performance with "(if xs <else> ys)". You can actually use "(iflet (x) xs <else> ys)" here too, but I don't know what that'll do to the performance.

e) There's no need to call 'testify.

After incorporating a bunch of this feedback--I didn't pit '< against '> --the code might look like this:

  (def sort-by-commonest5r1 (seq)
    (apply + nil
      (sort longer-list
        ( (afn (xs cxs ys)
            (if xs
              (withs (x car.xs
                      f [isnt _ x]
                      r (reclist [if (f:car _) _] xs)
                      cr len.r)
                (self r cr (cons (firstn (- cxs cr) xs) ys)))
              ys))
          (sort > seq) len.seq nil))))
At this point I'd make some more significant changes to get rid of all the length arithmetic:

  (def sort-by-commonest5r2 (seq)
    (apply join
      (sort longer-list
        ( (afn (rest bins)
            (iflet (sample . rest) rest
              ( (rfn self2 (bin rest)
                  (if rest
                    (let first car.rest
                      (if (is sample first)
                        (self2 (cons first bin) cdr.rest)
                        (self rest (cons bin bins))))
                    (cons bin bins)))
                list.sample rest)
              bins))
          (sort > seq) nil))))
However, for some reason those last changes actually perform worse:

  arc> (time:repeat 100 sort-by-commonest5r1.data)
  time: 18390 msec.
  nil
  arc> (time:repeat 100 sort-by-commonest5r2.data)
  time: 20437 msec.
  nil
  arc> (time:repeat 100 sort-by-commonest5.data)
  time: 19594 msec.
  nil
(My Arc setup is apparently slower than yours.)

-----


In short, it's an emoticon, as zck says. ^_^

I use XD to express careless fanaticism. The three pillars of XD are as follows:

- Euphoria: http://www.pixiv.net/member_illust.php?mode=medium&illus...

- Direction in life: http://www.pixiv.net/member_illust.php?mode=medium&illus...

And my favorite,

- Falsifiability: http://www.pixiv.net/member_illust.php?mode=medium&illus...

-----


Inspired by http://arclanguage.org/item?id=14938, I've taken a stab at porting Rainbow from Java to JavaScript. Well, maybe it's not so much a matter of inspiration as it is "Hey, I know Java, I know JavaScript, I know Arc, and I use Rainbow. Do I really expect anyone but me to do this?" :-p

At this point, the basic evaluation model is really buggy, mostly thanks to tons of trivial typos. Nevertheless, there's already a REPL where it's possible to accomplish a few basic calculations: http://rocketnia.github.com/rainbow-js/test/

Be careful if you're on a very stringent data regimen, as the REPL page loads the unminified rainbow.js, which is over half a megabyte in size. :-p I'd minify it, but the last time I tried, the minified version was even more broken. (I'm not doing anything special with property name manipulation--I'm actually writing this with the Closure Compiler in mind, just in case--so I think I must have broken the minifier.)

The REPL can't accomplish anything as ambitious as arc.arc... and at this point it can't even accomplish anything as ambitious as 'def! If you're having trouble at the REPL and want to see some expressions that actually work, take a look at rainbow.test.js--which currently has no real tests, just pastes from the REPL.

For further information, take a look at the README. ^_^

-----

2 points by rocketnia 5304 days ago | link

Oh, I should also mention: I've mostly tested this in Chrome. I just tried Firefox, and it mostly accepts the same code, but when there's an error, there seems to be something troublesome about the stack traces. Even an unbound identifier error disrupts the REPL loop and makes it impossible to provide more input.

Opera and IE 8 would be next on my checklist, but at the moment Rainbow.js supports zero platforms, so I might just keep testing on Chrome for a while. :-p

-----

1 point by rocketnia 5301 days ago | link

Rainbow.js can load arc.arc now. ^_^

There's a catch: My reader code is in continuation-passing style, and if the input's too long, it goes into a stack overflow.

Fortunately, the reason it's in CPS is so that it can process the input asynchronously. As long as it's actually working asynchronously, its continuation callbacks occur on fresh event stacks rather than piling up. I could go into more details, but what's important is that for now, submitting the input in smaller pieces is an effective, if ugly, workaround:

  var arcArc = document.getElementsByTagName( "textarea" )[ 0 ].
      value.split( "\n" );
  function readRest() {
      if ( arcArc.length === 0 ) return;
      consoleIn.o.writeString( arcArc.shift() + "\n" );
      setTimeout( readRest, 0 );
  }
  readRest();
For future reference, here's a link to the commit I'm talking about: https://github.com/rocketnia/rainbow-js/commit/60f60282b74e2...

-----

1 point by thaddeus 5301 days ago | link

I must be doing something wrong. I tried entering only 1 line and got:

Message : Unhandled exception on thread#0: Symbol var is not bound....

-----

1 point by rocketnia 5301 days ago | link

I suspect you're entering that JavaScript code at the Arc REPL. ^^; I just pasted that JavaScript here as an excerpt of the full(er) instructions on the REPL page:

"Actually, if you you paste from Java Rainbow's arc.arc source yourself, you'll probably find that Rainbow.js encounters a stack overflow error. At this point it's necessary to enter it in more bite-size pieces. Right now you can accomplish this by pasting all of arc.arc into the input box, then entering the following code into a JavaScript console (not the Arc console!) to have it entered one line at a time:

[the JS code I pasted above]"

In Chrome, the JavaScript console is available under "(wrench icon) > Tools > JavaScript console." On Firefox, I tested it using Firebug's console tab, but I think the Error Console might work too.

Also, pasting the JavaScript snippet all at once should be fine. The point is to enter the Arc code in bite-size pieces, and the JavaScript snippet just automates that process.

I'm considering making this workaround a feature of the REPL interface, as a checkbox or something, but what I'd rather do is fix the issue altogether.

-----

1 point by rocketnia 5298 days ago | link

"what I'd rather do is fix the issue altogether."

Done! Now the reader only uses a number of JavaScript stack frames proportional to the amount of nesting of the program, rather than the number of characters. Feel free to just paste all of arc.arc into the REPL and hit enter. ^_^

I've also tested the REPL on more browsers. The only one that really had a problem was IE 8, and that was because of the code of the REPL interface itself rather than the Rainbow.js runtime. It should work in IE 8 now... except that the performance is terrible there. XD

Any ideas about where this project should go next?

-----

1 point by rocketnia 5290 days ago | link

Rainbow.js is compiling with the Closure Compiler's advanced mode now! The REPL page--which still doesn't load arc.arc--is now down to 146 KB (HTML and JavaScript), rather than over 500.

Furthermore, on most recent browsers, the loading speed seems competitive with, or even better than, the loading speed of Java Rainbow. It might not be a fair comparison, since the contents being loaded are coming through a different kind of input, but I'm still pretty excited. ^_^

-----

3 points by rocketnia 5307 days ago | link | parent | on: Currying

I usually don't care about currying, but I like this. XD There are places I might miss the ability to use _ from inside other parentheses, like [... (foo _ bar) ...], but it would be interesting to explore the advantages.

Anarki[1] already lets us change the square bracket behavior by redefining the 'make-br-fn macro. In ar[2], it's the 'square-bracket macro.

Here's my take on your idea. When the operator is actually a macro, I just treat it the old way rather than unsuccessfully calling 'apply on it:

  (mac make-br-fn contents
    (iflet (op . args) contents
      (if (isa (bound&eval op) 'mac)
        `(fn (_) ,contents)
        (w/uniq g-args
          `(fn ,g-args
             (apply ,@(map (fn (x) (case x _ `(pop ,g-args) x))
                           contents)
                    ,g-args))))
      `(fn args (whenlet (op . args) args (apply op args)))))
Here's a half-demonstration thanks to http://tryarc.org/. It doesn't provide 'make-br-fn or 'square-bracket, so I haven't bothered to actually use the square brakcets:

  arc> ((make-br-fn car) '(1 2 3))
  1
  arc> ((make-br-fn list 1 _ _ 4 _) 2 3 5 6)
  (1 2 3 4 5 6)
  arc> (macex '(make-br-fn list 1 _ _ 4 _))
  (fn gs1022 (apply list 1 (pop gs1022) (pop gs1022) 4 (pop gs1022) gs1022))
  arc> (macex '(make-br-fn list))
  (fn gs1023 (apply list gs1023))
  arc> (macex '(make-br-fn))
  (fn args (whenlet (op . args) args (apply op args)))
  arc> ((make-br-fn))
  nil
  arc> ((make-br-fn) + 1 2)
  3
  arc> (((make-br-fn fn (it) _.it) inc) 4)
  Error: "reference to undefined identifier: _fn"
  arc> (((make-br-fn afn (it) _.it) inc) 4)
  5
As you can see, my approach has a little bit of room for improvement. The 'fn special form isn't actually a macro, so this treats it like a function. :-p (Also, I don't do anything about ssyntax, like [a:b c d] or [a&b c].)

---

[1] https://github.com/nex3/arc

[2] https://github.com/awwx/ar

-----

3 points by rocketnia 5308 days ago | link | parent | on: Remove-if-not

I think newlines you're entering into your console are \r\n, and you're using a version of 'readline that only supports \n newlines (and treats \r as a normal character). Andrew Wilcox has made a fixed version: http://awwx.ws/readline1

Furthermore, you're reading from stdin, the same stream the commands are read from, so what you're reading actually starts right after the ')' character and includes the same newline you used to enter the command. I think someone here might have made a fix for this too (probably Pauan or aw), but you might be able to work around it like this:

  arc> (do (readline) (...now your *actual* stdin-reading code...))
The first (readline) should hopefully get rid of whatever was left over from the input after reading the command.

-----

1 point by ly47 5308 days ago | link

Hi readline1 dose not function as read-line in common lisp. I type (read-line) and and arc is waiting to enter something so I type my string including spaces like: Hi there. output is: "Hi there" NIL Thanks ly

-----

1 point by rocketnia 5308 days ago | link

Right, I expect the version I linked you to doesn't behave exactly like Common Lisp's readline. I just hope it can help. :)

I don't fully understand your example. Did you try something like (do (readline) (readline)), like I was talking about? I'm guessing that since Arc's REPL takes commands and other input from the same stream, it's already different from the REPL you're used to, regardless of how readline works.

-----

1 point by ly47 5308 days ago | link

Hi (do (readline) (readline)) works !!! Thanks ly

-----

1 point by rocketnia 5308 days ago | link

Awesome! ^_^

-----

3 points by rocketnia 5309 days ago | link | parent | on: Remove-if-not

You can insert links like this... http://arclanguage.org/formatdoc

...separate paragraphs like this...

...and indent code like this:

  You can insert links like *this*... http://arclanguage.org/formatdoc
  
  ...separate paragraphs like *this*...
  
  ...and indent code like *this*:
  
    You can insert links like *this*... http://arclanguage.org/formatdoc
    
    ...separate paragraphs like *this*...
    
    ...and indent code like *this*:

-----

4 points by akkartik 5308 days ago | link

Yeah I took the liberty of adding the whitespace to make it look like ly seemed to intend.

-----


"I guess I should have used strings in all of those cases, but I was taking advantage of the fact that I could actually bind values to the symbols to store some metadata."

If you don't mind, how does/did it seem easier to bind values to symbols than to bind them to strings?

-----

2 points by Pauan 5312 days ago | link

I'm not shader, but I'm guessing that it's because... you can't.... bind values to strings...?

I mean, yeah, you could have a table where the keys are strings, and treat that as the metadata, but uuugh it's clunky and I've found myself disliking the whole "global table to hold data" thing more and more, recently.

Then again, I don't know how their compiler works, so I might be completely off-base here...

-----

1 point by rocketnia 5315 days ago | link | parent | on: possible bug involving len and nil?

Apparently Samuel Christie added it in order for Anarki's ppr.arc to work: https://github.com/nex3/arc/commit/fccf160fee607373ac15105e3...

I don't like len!sym being 1 either. I think ppr.arc is the thing that should be fixed, perhaps by having it use its own spinoff of 'len. But I didn't try to fix that; I just did the minimal change needed to get len.nil to work properly again.

It's kinda funny... I just noticed that both of my len.nil fixes (https://github.com/nex3/arc/commits/9c0f4fb2b54f4ceb11d0a2d2...) are currently living alongside each other, the older one just being dead code. :-p

-----

1 point by akkartik 5314 days ago | link

Perhaps he just wanted dotted lists to count the final element. I fixed that without breaking len!sym.

http://github.com/nex3/arc/commit/2d86c3

-----


"conses-as-vectors"

My stance is that they're intuitively separate things. If you take the length of a vector of length 2, you get 2. If you take the length of a linked list node, that's a different story. Still, if a linked list node is implemented as a wrapper around a vector, that's fine.

-----


"This decision was made for convenience, as aw illustrates here: http://arclanguage.org/item?id=10802 "

"Conditionally include things in a string"

Sorry if I enter into a bit of drama here.... I don't have anything against you or aw, but every time I see that comment, something nags at me. :)

At the time, there were about six reasons I didn't respond to that comment (which was a reply to a comment of mine):

1) I didn't consider it to be a "brilliant use," since the workaround without it would be negligible:

  (string "You are going "
          (if (> speed 100) "very " "")
          "fast")
2) Were I to have accepted the brilliance of this use case, I'd only have been more interested in why 'pr didn't have the same behavior (which was my main gripe to begin with):

  arc> (= speed 2)
  2
  arc> (prn "You are going " (if (> speed 100) "very ") "fast")
  You are going nilfast
  "You are going "
3) It was actually something I had already thought of and cast aside for the above reasons. (So it didn't even qualify as something "I'm not thinking of.") If aw was grasping for straws like I was, there probably wasn't much more point in talking about it.

4) It had upvotes too--at least one upvote at the time I first read it. Apparently, what I thought was common sense would be outnumbered.

5) I hadn't programmed in Arc that much yet. Maybe the upvoter(s) saw something there I didn't. At any rate, I'd probably be able to discuss the pros and cons better once I'd used Arc for a while.

6) I did say "I can't think of a single time," and this was a single example. I kinda decided to eat my words on account of that. :-p

Turns out (string:accum ...) is the use case I found that convinced me. The key point was that 'string wasn't consistent for symbols because it was too busy being consistent for lists.

-----

1 point by aw 5315 days ago | link

Just to be clear :-), I wasn't advocating for the behavior, merely answering the question if was there any use for it ^_^

-----

1 point by rocketnia 5315 days ago | link

Thanks for not taking that too harshly. XD

-----

More