Last active
December 14, 2015 16:19
-
-
Save ritalin/5114252 to your computer and use it in GitHub Desktop.
jQuery iterator for Haxe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import jQuery.JQuery; | |
class JQueryHelper { | |
public static function asIterable(query: JQuery): Iterable<JQuery> { | |
return new JQueryIterable(query); | |
} | |
} | |
private class JQueryIterable { | |
private var query: JQuery; | |
public function new(query: JQuery) { | |
this.query = query; | |
} | |
public inline function iterator(): Iterator<JQuery> { | |
return new JQueryIterator(this.query); | |
} | |
} | |
private class JQueryIterator { | |
private var query: JQuery; | |
private var length: Int; | |
private var p: Int; | |
public function new(query: JQuery) { | |
this.query = query; | |
this.length = query.length; | |
this.p = 0; | |
} | |
public function hasNext(): Bool { | |
return this.p < this.length; | |
} | |
public function next(): JQuery { | |
return new JQuery(this.query[this.p++]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import js.Lib; | |
import jQuery.JQuery; | |
using Lambda; | |
using sample.JQueryHelper; | |
class Main { | |
static function main() { | |
// JQuery -> Array<String> | |
var result = new JQuery(js.Lib.document.body) | |
.asIterable() | |
.map(function(el: JQuery) { return el.text(); }) | |
.array() | |
; | |
trace(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment