Hey J,

Wanted to share this and see what you think.

For Project Euler solutions in J, I have a single file and run it like this:

    $ ./project-euler.ijs <1> [<m> ..]   # output answers to problem n, m, ...

Initially, I had a scheme where all solutions are named like p1, p2, etc. with
the following at the bottom of the script:

    echo ".&> (#~ 0 = 4!:0) 'p'&,&.> ARGV
    exit@0:^:(e.~ <@'./project-euler.ijs') ARGV

It just straightforwardly tacks 'p' to the beginning of all arguments, filters
out the missing/nonsense stuff, and executes the result. The second line
ensures the script exits when called directly but not when 'load'ed in the
jconsole.

Anyway, the above scheme works but execution starts to get slow as more
solutions accumulate since it ends up computing *all* p1, p2, etc. before
output. So I wanted to "lazily" evaluate only those solutions requested on the
command line.

My first approach was to "lazy load" by making all p1, p2, etc. strings and
evaluating with

    0!:111 ".&> (#~ 0 = 4!:0) 'p'&,&.> ARGV

However, this strikes me as a bit ugly. It also makes it a bit awkward to
investigate and iterate on solutions when loading into jconsole.

So the scheme I came up with is a simple modification of the initial approach.
Instead of p1, p2, etc. we now have [x1], v1, y1, [x2], v2, y2, etc. (where
[x1] means that x1 is optional). Effectively we want to expand the argument '1'
into 'x1 v1 y1' or 'v1 y1' as appropriate and then execute. This is how I am
achieving that:

    echo ". ,"_1 ,&' '&> |: (#~"1 (0 3 0) = 4!:0) 'xvy' ,&.>/ ARGV

It's quite nice how closely this mirrors the original approach. After the hook,
we have a table whose columns encode the executions we want. Finally, a bit of
munging gives the desired result.

Thoughts? Is this Good J TM?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to