------
<html><head><title>MochiKit Test</title>
<script src="MochiKit.js" type="text/javascript"></script>
</head><body>
<h2>Test 1</h2><p id="test1"/>
<h2>Test 2</h2><p id="test2"/>
<script type="text/javascript">
text ="This\nis\na\ntest";
text = text.split('\n');
test1 = zip(text, [BR(), BR(), BR(), BR()]);
appendChildNodes("test1", test1);
test2 = zip(text, repeat(BR()));
appendChildNodes("test2", test2);
</script>
</body></html>
------
Right, this works, since you're using an array here. This is basically
the same as my Test 1.
Thanks for pointing out that zip should only be used with arrays, not
with iterators. I did not expect that the docs for zip need to be taken
so literally, and I'm still not sure that this is the problem. For
instance, the following works as expected:
zip(text, repeat("*"));
And if you replace zip with izip (which *is* an Iter library function),
the Test 2 does not work either. As I understand the difference between
zip and izip is that izip returns an iterator instead of an array, but I
would expect of an Iter lib function that it also takes iterators as
input, and the documentation does not claim otherwise here.
The following does not work, either:
list(izip(text, repeat(BR()));
So I'm still puzzled.
> I think appendChildNodes handles flattening that mess for you, but if
> not just use the function.
Yes, as I already wrote, you don't need to flatten since this is done
implicitly. My Test 1 works nicely, without flattening.
-- Christoph
Ah, yes! Thanks for your analysis, now everything makes sense.
I could use the excuse that I usually program in Python, not in
Javascript, but in fact repeat() does not behave differently there ;-)
To summarize, the Test2 code based on two subtle misconceptions. First,
the implicit idea that in the expression repeat(BR()) the BR() is either
evaluated on every repetition, or copies of the BR() object are created
on every repetition, which both is of course not the case. Second, the
idea that you don't need to care whether elements you're adding to the
DOM are already used elsewhere ;-)
Your solution
var brAdder = function(x) { return [x, BR()]; }
appendChildNodes("test2", map(brAdder, text));
is nice. Alternatively, the Test2 code be fixed as follows:
appendChildNodes("test2", izip(text, imap(BR, repeat())));
Just to make use of repeat() against these odds...
-- Christoph
function appendChildNodeClones(parent, /* ... */) {
var args = flattenArguments(arguments);
for (var i = 1; i < args.length; i++) {
if (args[i] != null && typeof(args[i].cloneNode) == "function") {
parent.appendChild(args[i].cloneNode(true));
} else {
appendChildNodes(parent, args[i]);
}
}
}
/Per