Link(arr1, arr2, arr3) .where(...) .etc()
var neoHippos = [ { name: "maggie", fatness: 812 }, { name: "Beverly", fatness: 999 }, { name: "Gail", fatness: 1208 }, { name: "Burke", fatness: 500 }, { name: "Machel", fatness: 100 }, ]; link(neoHippos) .where(nh => nh.name != "maggie") .execute(nh => DebugPrint("neo-Hippo member:", nh.name, nh.fatness)) .each(nh => Print(nh.name + " is really hungry!");
link(eventData.action.effects) .where(it => it.targetHint == 'selected') .where(it => it.type == 'damage') .each(effect =>{ var oldPower = effect.power; effect.power = Math.round(effect.power / this.fatigue); if (effect.power != oldPower) { console.log("Outgoing POW modified by Final Stand to " + effect.power); console.append("was: " + oldPower); }});
var isPhysical = link(action.effects).filterBy('type', 'damage').pluck('damageType').contains('physical') || link(action.effects).filterBy('type', 'damage').pluck('element').contains('earth');
if (this.tactics === null) { var targets = link(this.aic.battle.enemiesOf(this.aic.unit)).shuffle(); var combos = link(link(this.combos) .where(function(combo) { return phase >= combo.phase; }.bind(this)) .random(targets.length)) .sort(function(a, b) { return b.rating - a.rating; }); this.tactics = []; for (var i = 0; i < targets.length; ++i) { this.tactics.push({ moves: combos[i].moves, moveIndex: 0, unit: targets[i] }); }}this.tactics = link(this.tactics) .where(function(tactic) { return tactic.unit.isAlive(); }) .where(function(tactic) { return tactic.moveIndex < tactic.moves.length; }) .toArray();var tactic;do { tactic = RNG.sample(this.tactics);} while (tactic === this.tactics[0] && tactic.moveIndex == tactic.moves.length - 1 && this.tactics.length > 1);this.aic.queueSkill(tactic.moves[tactic.moveIndex], tactic.unit.id);++tactic.moveIndex;if (this.tactics[0].moveIndex == this.tactics[0].moves.length) { this.tactics = null;}
<neoHippos> <pig name="maggie" weight="812 tons"> <food>Lizzie</food> <food>Chiroptera</food> <food>Gail</food> <food>non-kitty-eating cows</food> </pig> <cow name="Kittycow"> <food>Prance</food> <food>Shrek</food> <food>kitties</food> </cow></neoHippos>
Link(dom).recurse('nodes').where(function(item) { return item.name == 'pig'; }).each(function(item) { console.log(item); });
Link(dom) .recurse('nodes') .where(x => x.type == 'tag' && x.name == 'neoHippos') .recurse('nodes') .where(x => x.type == 'tag' && x.name == 'pig') .recurse('nodes') .where(x => x.type == 'tag' && x.name == 'food') .recurse('nodes') .where(x => x.type == 'text') .each(element =>{ console.log("the pig eats: " + element.text);});
Does .recurse() unroll the whole structure, or only one level at a time? i.e. Could I do this:Code: (javascript) [Select]Link(dom) .recurse('nodes') .where(x => x.type == 'tag' && x.name == 'neoHippos') .recurse('nodes') .where(x => x.type == 'tag' && x.name == 'pig') .recurse('nodes') .where(x => x.type == 'tag' && x.name == 'food') .recurse('nodes') .where(x => x.type == 'text') .each(element =>{ console.log("the pig eats: " + element.text);});
Link(dom) .pluck('nodes') .where(x => x.type == 'tag' && x.name == 'neoHippos') .pluck('nodes') .where(x => x.type == 'tag' && x.name == 'pig') .pluck('nodes') .where(x => x.type == 'tag' && x.name == 'food') .pluck('nodes') .where(x => x.type == 'text') .each(element =>{ console.log("the pig eats: " + element.text);});
If I use recurse, is there a way to figure out where I came from? Since it unrolls everything each() only sees the element at the lowest level, with no idea where we came from. That's why I was looking for a way to selectively drill down.
// old way:Link(my_array).each(function(item) { var found = Link(other_array).first(function(i) { return i.ID == item; }); if (found) { found.added = 1; }});// new way:Link(my_array).cross(other_array, function(item1, item2) { if (item2.ID == item1) { item2.added = 1; }});// experimental non-blocking cross product, creates an interstitial cross-result object.Link(my_array).crossObject(other_array).each(function(result) { if (result.B.ID == result.A) { result.B.added = 1; }});// or, using .where:Link(my_array).crossObject(other_array).where(function(result) { return result.B.ID == result.A; }).each(function(result) { result.B.added = 1; });// in typescript:Link(my_array).crossObject(other_array).where(result => result.B.ID == result.A).each(result => result.B.added = 1);
var test = Link(array1).where(even);Link(test).each(print);
Not sure I understand the cross-product thing. I know what a cross product is in math, just not sure how it pertains here.
For the resolving thing, I think it would be nice to maintain the lazy evaluation behavior of possible. Not sure how difficult that'd be to implement though.
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];var ctxt = Link(array).where(even).yield('toArray');var v1 = ctxt.step().step().step().step().step().end(); // [0, 2, 4]var v2 = ctxt.step(5).end(); // [0, 2];var v3 = ctxt.run(); // [0, 2, 4, 6, 8, 10]