Planet Rosetta Code

February 28, 2010

Kevin Reid

Things I had not done before entering college

Normal People Things:

  • Gone to a party unrelated to my family.

Never thought I'd do:

  • Worn a t-shirt with text on it.
  • Written an essay structured using a gratuitous extended metaphor.

Extra nerd points:

  • Programmed a number type which carries units and error values, to reduce the tedium of lab reports.
  • Learned to write all my assignments in LATEX.

February 28, 2010 02:34 AM

February 25, 2010

Kevin Reid

Hvm.hs: an exercise in Haskell golf

import Char
import Debug.Trace
import System.Environment
import Control.Monad.Fix

{-
  Hvm.hs
  By Kevin Reid, 2008-06-15

  This is an implementation of the virtual machine described at 
  <http://www.hacker.org/hvm/>. It was designed to fit into a single IRC line
  so that its definition could be put into lambdabot
  <http://www.haskell.org/haskellwiki/Lambdabot>, and its inspiration and
  creation was documented at <http://swhack.com/logs/2008-06-15>.

  Haskell source not in HTML: <http://switchb.org/kpreid/2008/Hvm.hs>
  Non-blog location: <http://switchb.org/kpreid/2008/Hvm.html>

  Explanation of variables:

    r = VM execution function of call stack, memory, instruction pointer, and 
        operand stack
    c = call stack
    m = memory
    i = instruction pointer
    j = instruction pointer plus one
    o = operand stack

    a = arithmetic handler
    h = proceed with instruction pointer and operation stack modification
    g = proceed with operand stack modification

    z:x = one value popped from operand stack
    z:w:e = two values popped from operand stack
    k:l = one value popped from call stack

    d, t: slice lists using z as length

  To make it fit in IRC, discard comments, imports, and "main", replace 
  all line breaks with semicolons as appropriate, and discard the ' ' and _ 
  cases (i.e. the interpreter will not support whitespace or report invalid 
  characters).
    perl -0777 -pe 's/\A.*(?=^s)//ms; s/\s*main .*//; s/--.*$//mg;
                    s/\n */;/g; s/[;\s]*(where|let)[;\s]*/$1 /g;
                    s/;'\'' '\''->g o;_->error\$show\(p!!i\)//;'
-}

s p=r[](fix(0:))0[]
  where
   r c m i o=--trace (show (m,i,(if length p<=i then '!' else p !! i),(reverse o),c)) $
     let a(&)=g$w&z:e
         j=1+i
         h=r c m
         g=h j
         z:x=o
         w:e=x
         k:l=c
         d=drop(1+z)
         t=take z
      in case(p++"!")!!i of
         'p'->show z++g x
         'P'->chr(mod z 128):g x
         d|isDigit d->g$ord d-48:o
         '+'->a(+)
         '-'->a(-)
         '*'->a(*)
         '/'->a div
         ':'->g$signum(w-z):e
         'g'->h(z+j)x
         '?'->h(i+case w of 0->z;_->1)e
         'c'->r(j:c)m z x
         '$'->r l m k o
         '<'->g$m!!z:x
         '>'->r c(t m++w:d m)j e
         '^'->g$x!!z:x
         'v'->g$x!!z:t x++d x
         'd'->g x
         '!'->""
         ' '->g o
         _->error$show(p!!i)

main = do [p] <- getArgs; putStrLn $ s p

February 25, 2010 03:07 AM

February 16, 2010

Kevin Reid

Where's my new PDA?

My Palm TX is noticeably wearing out (power button dead and worked around, cover hinge fraying, direction pad starting to go) and doesn't have a decent modern web browser. I might have gotten a Palm Pre by now if it weren't for the lack of full-size external (Bluetooth) keyboard support (notetaking is one of the major use cases) — though I hear there might be an SDK coming out which would let third parties build one. (Lesser issues are the cost of a phone contract (though Internet-anywhere makes that have a good bit of value) and the battery life.) I get the impression that I should be looking at the Nokia N900 or one of its relatives, but I haven't gotten around to investigating that.

My additional requirements for a New PDA are that it sync calendars/todos with iCal, sync the abovementioned notes to my Mac (I don't care what software), fit in my shirt pocket, and have mostly-not-locked-down-and-expensive software development/installation (so that customized/open-source/the-obscure-niche-I-happen-to-need software can happen).

(Apple mobile products fail on the openness criterion and either Bluetooth keyboard (iPhone/iPod touch) or fitting in the shirt pocket (iPad).)

February 16, 2010 06:48 PM

February 14, 2010

Rosetta Code.org subreddit

February 07, 2010

Michael Mol

"real men just upload their important stuff on ftp, and let the rest of the world mirror it ;) "

First: No, the purpose of this post isn't to ask for volunteers.
Second: No, I wouldn't expect (or necessarily want) the average Bittorrent user to participate in the swarm.
Third: Data volume currently sists at 3.5GB uncompressed.

I've been thinking about a recurring issue...Namely that none of RCo's remote backup targets are very good. My home system has a problem remaining alive for any extended duration, and I don't have any other good prospective places to I can trust to send the data. (No offense to anyone who's offered, but it's hard for me to totally trust someone I've never met in person.*)

I may have hit on a novel solution, but I want to run it past a bunch of people (namely, you), before I do something this crazy.

Step 1: Take backup on server
Step 2: Compress backup to tarball.
Step 3: Encrypt tarball using GPG and a long, long public key.
Step 4: Build a torrent.
Step 5: Add torrent to RSS feed.
Step 6: Anyone who wants to help can point their torrent client at the RSS feed. Data's encrypted, so I don't have to worry. With enough seeder boxes out there, there can be several full copies out there.

Bonus: Server data migration happens much, much faster. :)

Steps 2 and 3 can be munched a bit by nesting the encryption in the
tarball, using separate keys for each subdirectory, or even splitting
a .tar.lzma, separately encrypting each chunk with a unique key,
tarring that and using another key for encrypting that final tarball.

And, yes, generating that many keys is problematic; It took me about five minutes to generate a 4kbit key yesterday as a test, as my home system didn't have enough entropy. That's solvable with either bulk data from random.org or using a hardware RNG. There's also key transport, but that's somewhat alleviated if I generate the key pairs at home, and then copy only the public keys to the server.

* Yes, this is me we're talking about, and I realize the irony.

February 07, 2010 04:00 PM

January 26, 2010

Michael Mol

mmol_6453 @ 2010-01-25T23:41:00

Want to catch the early wave when new tasks are added to RosettaCode? Pretty much what this is for: http://reddit.rosettacode.org/

January 26, 2010 05:41 AM

Rosetta Code.org subreddit

January 25, 2010

Rosetta Code.org subreddit

January 22, 2010

Rosetta Code.org subreddit

December 21, 2009

Kevin Reid

Improving my web presence, out of necessity. Also, Apache configuration and LJ OpenID

A machine I used to use to host some web services, bots, and repositories became no longer accessible from the Internet, as a result of which I've had to move what I was serving from it; some to switchb.org, some to personal machines.

I took the opportunity to clean things up a bit, as a result of which I now have better backups, more polished services, and know a little bit more about configuring Apache — though not as much as I perhaps should.

  • My Subversion repositories are now served over HTTP, and therefore browsable; and they are now backed up daily (using svnsync triggered by a cron job) to my laptop, and thence to all its backups.

    (I wasted several minutes on remembering that cron will ignore the last line of a crontab file if it doesn't end with a newline; after listening to me grumbling about this, someone made a suggestion to end the file with a comment, so that the last line is harmless whether ignored or not, and also reminds one of the issue.)

    If you have a working copy of one of my repositories (E-on-CL, E-on-JavaScript, MudWalker, Den, etc.), here's a guide to the changed URLs.

  • My other Tahoe-LAFS volunteer grid storage node is now residing on a machine on my home LAN.

  • Finally, some simple data-querying web services I wrote for Waterpoint's word games have now been moved to switchb.org; I also took the time to prettify their URLs (no cgi-bin or .cgi) and write documentation.

I haven't yet gotten to working on the bots, darcs repositories, or miscellaneous other stuff I had there.

(Pondering moving my blog over to switchb.org as well so as to not have ads, especially now that I found I can still have LJ-friends by way of OpenID. (Hm, but reading friends-locked posts over RSS might not work since there's no username+password for LJ to accept. Anyone have experience with that situation?))

Apache configuration questions:

  1. If I have multiple variants of a document (e.g. foo.html foo.pdf foo.txt) handled by MultiViews, so the canonical URL of the document is extensionless (“foo”), how do I properly control the default variant to serve in the event that the client does not express a preference via the Accept header? (Without doing so, I found that it would serve the .txt version, whereas I would prefer the HTML.) All that I found that worked was to create a type map file named “foo” with quality values, and force it to be interpreted as a type map using <FilesMatch>. This seems kludgy to me.
  2. What is the right way to serve CGIs, not in a designated cgi-bin directory, and without any .cgi extension in the URL? I initially tried to apply mod_rewrite, but I couldn't get it to work such that /foo internally contacted foo.cgi whereas /foo.cgi redirected to /foo. I resorted to another <FilesMatch> explicitly listing the name of each CGI and doing SetHandler cgi-script.
  3. What is the right way to handle “testing” vs. “deployment” configurations, where the relevant Directory, Location, etc may be different depending on which server or subdirectory the site is set up on? I see that one may use environment variables — should I just set up variables containing the path prefixes for the particular host before including the generic configuration file?

December 21, 2009 02:02 AM

December 14, 2009

Kevin Reid

Unifying status messages?

Has anyone invented a common protocol for, in a desktop environment, updating status (available/away/do-not-disturb and “what I'm doing” and so on) in social applications? I have 5 different such applications (iChat, Colloquy, Skype, MudWalker, Steam), and I usually don't bother to update all of them since it's too much trouble to do regularly. Is there some protocol that, say, the Linux-on-the-desktop folks have invented for having just one widget to inform everything?

[edit] Clarification: I mention Linux-on-the-desktop just because I don't know much aboutit and there seems to be some innovation from that direction in this kind of area. The desktop in question is Mac OS X; I'm expecting to have to write the software myself; I just want to not reinvent the wheel with regard to protocol.

December 14, 2009 05:45 PM

November 27, 2009

Kevin Reid

ASDF PSA: defpackage in .asd is not obligatory

There seems to be a recurring misconception about writing .asd files. It is not necessary to start your asd with (defpackage :foo.system ...) (in-package :foo.system) to “avoid polluting the package asdf loads your system in”.

Every time an .asd file is loaded, ASDF creates a fresh package to load it in. The relevant code from ASDF is:

(defun make-temporary-package ()
  (flet ((try (counter)
           (ignore-errors
                   (make-package (format nil "ASDF~D" counter)
                                 :use '(:cl :asdf)))))
    (do* ((counter 0 (+ counter 1))
          (package (try counter) (try counter)))
         (package package))))

(defun find-system (...)
  ...
      (let ((package (make-temporary-package)))
        (unwind-protect
             (let ((*package* package))
               ...
               (load on-disk))
          (delete-package package)))
  ...)

So, whenever you load an asd file, the package is fresh and :uses CL and ASDF.

There are reasons to define a package for your asdf system definition.

  • If you are creating any classes, such as custom ASDF component types, then, since classes are not garbage collected (MOP specifies that the list of a class's subclasses can be retrieved, and no implementation I know of uses weak references for the purpose), naming them with symbols in a consistent package (rather than the asdf temporary package) ensures that reloading the system definition does not create useless additional copies of the classes.
  • If you are defining functions or other named entities in your system definition (which should of course only be done if they are also necessary to load the system) and want to be able to refer to them, such as for debugging, with nice interned symbol names.
  • If you want to be able to (load "foo.asd") rather than going through asdf:find-system.

The disadvantages I can think of for defining a package for your system definitions are that the package list becomes more cluttered (only noticeably if for some strange reason you're loading many system definitions and not loading the defined systems), you're using more of the global package namespace (probably not a significant concern if you use a naming scheme like foo and foo.system), and your code is marginally more complex.

So there are good reasons for (and maybe some against) defpackage in your asd, but package hygiene is not one of them.

[Edited 2009-11-26 to be less worded against defpackage after consideration and feedback.]

November 27, 2009 12:54 AM

November 18, 2009

Kevin Reid

Avoiding repeating myself (on the command line)

I was working on some automated document generation, building the process, revising the input document, and checking the results, and got tired enough of typing "make output/foo.txt && open output/foo.txt" and then going back and editing that line when I wanted to check the PDF version etc. that I wrote a tool which I hope will have more general application as well. I called it “maken”:

#!/bin/sh
# Make files and view the results.
make "$@" && open "$@"

Or it could be generalized into being a combinator, running any two commands, rather than just make and open, specified as the first two arguments:

#!/bin/sh
ca="$1"
shift
cb="$1"
shift
"$ca" "$@" && "$cb" "$@"

(Which, for the Haskell folks, should be called &&&, except that that would be annoying to enter in the shell.)

November 18, 2009 02:14 AM

September 22, 2009

Kevin Reid

kpreid @ 2009-09-21T23:18:00

Every time you write “ | ” in shell, that's concurrent programming.

September 22, 2009 03:20 AM

This is not a LaTeX quine

\documentclass{article}
\usepackage{verbatim}
\begin{document}
  \verbatiminput{\jobname.tex}
\end{document}

September 22, 2009 01:45 AM

September 18, 2009

Kevin Reid

What do you call a method, er, header in Java?

What do practicing Java programmers call the part of a method definition that isn't the { body }?

__________/ this bit here \___________
public static void main(String[] args) {
  // not this part
}

A quick search turned up the Java Language Specification, Second Edition calling it a MethodHeader in the grammar, but it doesn't use that term in the text and I want to know what term(s) are used in practice, not solely by language lawyers.

(“Signature” is not correct, as that includes only the method name and parameter types, not modifiers and result type.)

September 18, 2009 03:54 AM

It's that time of day again...

Both of the non-internet-based computer-related classes I'm taking this semester are (only offered as) once-per-week night classes, nominally ending at 9:30. So I'm talking with someone after class — — gee, it's bedtime already.

September 18, 2009 03:29 AM

September 11, 2009

Kevin Reid

GSoC conclusion.

Late update on Caja-CapTP:

Google Summer of Code is over. I passed based on revised goals, but I'm not happy with the state of the code and I did not complete any significant part of the original plan.

Regarding the items mentioned in my last update:

  • Write more documentation/comments.
  • Commit as much of the work-in-progress as I can.
    • ...including the incomplete CapTPConnection, even though its tests don't pass yet, so that the partial work can be counted.

I committed CapTPConnection, and found and fixed enough infrastructure (whenResolved, CommTable, SwissTable, deSubgraphKit, etc.) bugs that it starts up and can do a basic operation or two. It's not useful for anything, but it's a lot closer to running than it was at the time of my last update.

Also, I removed dependencies on 'window' so in principle it will operate on a non-browser (server) JavaScript implementation. This has not been exercised because there is no browserless driver for the test scripts yet.

I stated that I would continue working on Caja-CapTP past the GSoC work period; however, with the time occupied by schoolwork, I have not had time or brain space to do so yet. I am not going to abandon the project.

September 11, 2009 01:09 AM

an approximation, the usefulness of which depends on the application

Java programming class (yes, I know Java already) today, introducing loops. Task: write a factorial program. I wrote mine to use int arithmetic if possible, floating-point if not. Test the edge cases, of course. It says:

500! ≈ Infinity

I'm glad I used the “approximately equals” sign.

September 11, 2009 12:56 AM

September 08, 2009

Michael Mol

Not done *yet*, but...

I sprung for a Linode 540 account. I copied RC over, and have been tuning everything all evening. The new VPS server is serving up 5-7Mb/s to my stress testing. Of dynamically-generated pges.

Points of interest:

  • 540MB of RAM.
  • memcached -k -m128M
  • php5-xcache
  • Tuned MySQL parameters
  • The site's code is on a tmpfs mount
  • Not using Squid
  • My stress testing was done from a connection capped at 6Mb/s
  • My stress testing was eight concurrent runs of wget -m --spider http://hostname

Kinda pleased.

Still have to set up all the subdomains, but the site should run a lot faster once I'm ready to switch DNS over. I'm also spending most of my CPU time in system, and that's coming from the apache forked processes (post-fork, mind you). I haven't figured out why.

You might have noticed the mention of tmpfs...I had a lot of spare RAM. I have a hard upper limit on the number of running forked PHP processes, an upper limit on memcached, and MySQL doesn't seem to be able to be coaxed into keeping its M_DRS memory resident. So I had about 300MB of RAM used for file cache.

Since I don't need to change the PHP files often, losing the contents of the tmpfs mount in an outage isn't a major concern, and I can cron an rsync to disk.

September 08, 2009 11:24 PM