Chiron r368

3 views
Skip to first unread message

Kris Kowal

unread,
Nov 23, 2008, 10:21:32 PM11/23/08
to Chiron
Only a couple important announcements for Chiron users today.
Revision 368 brings about a couple backward-incompatible changes. Let
me know if you're using Chiron and don't want this kind of thing to
happen anymore. At the moment, I'm assuming that I'm the only user.

1. All occurrences of "getLength" have been changed to "len"
throughout the entire project. This is more in keeping with Python
and easier to read. Despite my considerable reservations about using
any abbreviations for variable names, I've opted to shorten this
common idiom.

2. The "sliced" operator in the "base" module has been renamed to
"slice" since, in the wild, that felt more natural. There won't ever
be a stateful "slice" operator, although there is a distinction
between "slice" and "sliced" for types. "sliced" returns a new object
and "slice" modifies the context object. This is completely analogous
to "add" and "added" and only a small departure from the pattern with
"sort" and "sorted".

3. The parts of the "boost" module that modify native prototypes like
Array, String, and Function, have been moved to a new "bust" module so
that you can make a fine-grained choice about whether you want those
features or the nifty power-tools in "boost" like "baled" and
"frequency".

4. Just a reminder: all HTTP requests, albeit via "http", "json", or
"jsonp", are all automatically set to "asynchronous" mode if you
observe an event on the request now.

5. Also a reminder: the "select" module is based on
"jamesdonaghue.com/select.js" instead of "jslocum.com/select.js" now.
It's lighter and faster, but that also means it no longer supports
XPATH; you'll need to use Jack's module for that now. "select"
returns a "List", not an "Array", unlike the "peppy" module which I
will be keeping on as "deprecated" until I can ween everyone off it.
That means "select("#id").get(0)" and
"select(".class").forEach(setOpacity(.5))" work with the new "select",
but "select("#id")[0]" will need to be changed when you migrate from
"peppy.js" to "select.js".

6. Another reminder: "getSuper" is gone. Your type constructor
implicitly gets a "supr" object as its second argument (right after
"self"). You no longer need to call a function with your own type to
get the super-type snapshot. This also means you cannot modify your
super-type snapshot and expect polymorphism to work. Oh well, it's a
trade-off.

You can, of course, read the commit message or the source if you're
interested in puzzling this all out yourself:
https://cixar.com/tracs/javascript/changeset/386

That covers all the deliberate breakage issues you might experience if
you're using the library.

=====================

Apart from that, I implemented LZ78 and Huffman encoding this weekend.
LZ77, deflate, and Gzip are now visible on the horizon. Somewhere in
the process, I wrote "struct.js", which is more or less the same thing
you find in Python, with its "pack" and "unpack" routines. I also
added "ord" and "chr" which are builtins in Python, but you have to do
silly String.fromCharCode or charCodeAt calls to emulate the same
behavior in JavaScript. I ported the base64 functions by Tyler Atkins
because they appear to work better than the ones I had copied from
Wikipedia. There are unit tests for the functions in "struct", if you
want a gander. It covers everything including endianness, signage,
and quad ints but excluding floating point.

There's a function called "frequency" in "boost.js". It was handy
when writing Huffman codes. It takes an iterable of arbitrary objects
(so, a String qualifies, but so does a List of Widgets, or an Iter of
Arrays) and returns a Dict mapping values from the iteration to their
corresponding frequency of occurrence. Dict.invoke is an alias to
Dict.get, so you can use a Dict as a relation function, just with a
finite domain. So, "chr" is effectively the same thing as
"zip(range(256), asciiAlphabet).dict()".

I told you that story so I could tell you this one. The "by" function
takes a "relation" and returns a "comparator". This is so you can do
things like sorting objects by a particular key.

sorted([{'a': 10, 'b': 20}, {'a': 0, 'b': 30}], by(function (x)
{return x.a}))

Albiet:

sorted([{'a': 10, 'b': 20}, {'a': 0, 'b': 30}], by(get('a')))

So, putting these two ideas together, you can sort all of the
characters that appear in a string by their frequency of occurrence
really easily:

include("boost.js");
var frequencies = frequency("aaabbaabaaabbbaabadabbbaaab");
return frequencies.keys().sorted(by(frequencies)).join();

Kris

Reply all
Reply to author
Forward
0 new messages