Skip to content
Browse files

first release

  • Loading branch information...
0 parents commit b5693b84b95e377a84ec01d5bd4c7c22205b85db @andyli committed Jun 6, 2010
Showing with 468 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +11 −0 README.textile
  3. +221 −0 Test.hx
  4. +29 −0 compile.hxml
  5. +6 −0 haxelib.xml
  6. +198 −0 hxLINQ/LINQ.hx
3 .gitignore
@@ -0,0 +1,3 @@
+bin
+
+.DS_Store
11 README.textile
@@ -0,0 +1,11 @@
+h1. hxLINQ
+
+HaXe port of the "LINQ to JavaScript (JSLINQ)":http://jslinq.codeplex.com Project.
+
+JSLINQ is licensed under the Microsoft Reciprocal License (Ms-RL).
+Copyright (C) 2009 "Chris Pietschmann":http://pietschsoft.com . All rights reserved.
+The license can be found "here":http://jslinq.codeplex.com/license .
+
+h1. Usage
+
+It is very similat to using "JSLINQ":http://jslinq.codeplex.com/ . You may look at *Test.hx*.
221 Test.hx
@@ -0,0 +1,221 @@
+import hxLINQ.LINQ;
+
+using Lambda;
+
+typedef Person = { id:Int , firstName:String, lastName:String, bookIds:Array<Int> };
+
+class Test extends haxe.unit.TestCase{
+ public function testWhere():Void {
+ var r;
+
+ r = new LINQ(people)
+ .where(function(p:Person, i:Int) return p.firstName == "Chris");
+ this.assertEquals(2,r.count());
+
+ r = new LINQ(people)
+ .where(function(p:Person, i:Int) return p.firstName == "Chris" && i == 0);
+ this.assertEquals(1,r.count());
+ }
+
+ public function testSelect():Void {
+ var r;
+
+ r = new LINQ(people)
+ .select(function(p:Person) return p.firstName);
+ this.assertEquals(10,r.count());
+ this.assertTrue(Std.is(r.first(),String));
+ }
+
+ public function testSelectMany():Void {
+ var r;
+
+ r = new LINQ(people)
+ .selectMany(function(p:Person) return p.bookIds);
+ this.assertEquals(30,r.count());
+ this.assertTrue(Std.is(r.first(),Int));
+ }
+
+ public function testOrderBy():Void {
+ var r;
+
+ r = new LINQ(people)
+ .orderBy(function(p:Person) return p.firstName.charCodeAt(0));
+ this.assertEquals(10,r.count());
+ this.assertEquals("Bernard",r.first().firstName);
+ this.assertEquals("Steve",r.last().firstName);
+ }
+
+ public function testOrderByDescending():Void {
+ var r;
+
+ r = new LINQ(people)
+ .orderByDescending(function(p:Person) return p.firstName.charCodeAt(0));
+ this.assertEquals(10,r.count());
+ this.assertEquals("Bernard",r.last().firstName);
+ this.assertEquals("Steve",r.first().firstName);
+ }
+
+ public function testCount():Void {
+ this.assertEquals(10,new LINQ(people).count());
+
+ var r = new LINQ(people)
+ .count(function (p:Person, i:Int) return p.firstName == "Chris");
+ this.assertEquals(2,r);
+
+ r = new LINQ(people)
+ .count(function (p:Person, i:Int) return p.firstName == "Chris" && i == 4);
+ this.assertEquals(0,r);
+ }
+
+ public function testDistinct():Void {
+ var r;
+
+ r = new LINQ(people)
+ .distinct(function(p:Person) return p.firstName);
+ this.assertEquals(8,r.count());
+ }
+
+ public function testAny():Void {
+ var r;
+
+ r = new LINQ(people)
+ .any(function(p:Person, i:Int) return p.firstName == "Chris");
+ this.assertTrue(r);
+
+ r = new LINQ(people)
+ .any(function(p:Person, i:Int) return p.firstName == "Chris" && i == 4);
+ this.assertFalse(r);
+ }
+
+ public function testAll():Void {
+ var r;
+
+ r = new LINQ(people)
+ .all(function(p:Person, i:Int) return p.firstName == "Chris");
+ this.assertFalse(r);
+
+ r = new LINQ(people)
+ .all(function(p:Person, i:Int) return p.firstName == "Chris" && i == 0);
+ this.assertFalse(r);
+ }
+
+ public function testReverse():Void {
+ var r;
+
+ r = new LINQ(people)
+ .reverse();
+ this.assertEquals(10,r.count());
+ this.assertEquals("Kate",r.first().firstName);
+ this.assertEquals("Chris",r.last().firstName);
+ }
+
+ public function testFirst():Void {
+ var r;
+
+ r = new LINQ(people)
+ .first(function(p:Person,i:Int) return p.firstName == "Chris");
+ this.assertEquals("Chris",r.firstName);
+ this.assertEquals(1,r.id);
+
+ r = new LINQ(people)
+ .first(function(p:Person,i:Int) return p.firstName == "Chris" && i == 0);
+ this.assertEquals("Chris",r.firstName);
+ this.assertEquals(1,r.id);
+ }
+
+ public function testLast():Void {
+ var r;
+
+ r = new LINQ(people)
+ .last(function(p:Person,i:Int) return p.firstName == "Chris");
+ this.assertEquals("Chris",r.firstName);
+ this.assertEquals(8,r.id);
+
+ r = new LINQ(people)
+ .last(function(p:Person,i:Int) return p.firstName == "Chris" && i == 0);
+ this.assertEquals("Chris",r.firstName);
+ this.assertEquals(1,r.id);
+ }
+
+ public function testElementAt():Void {
+ var r;
+
+ r = new LINQ(people)
+ .elementAt(1);
+ this.assertEquals("Kate",r.firstName);
+ this.assertEquals(2,r.id);
+ }
+
+ public function testIntersect():Void {
+ var nameList1 = ["Chris","Steve","John"];
+ var nameList2 = ["Katie","Chris","John", "Aaron"];
+ var sample = new LINQ(nameList1).intersect(nameList2);
+ this.assertEquals(2,sample.count());
+
+ sample = new LINQ(nameList1)
+ .intersect(new LINQ(nameList2));
+ this.assertEquals(2,sample.count());
+
+ var sample2 = new LINQ(people)
+ .intersect(nameList2, function(item:Person, index:Int, item2:String, index2:Int) return item.firstName == item2);
+ this.assertEquals(4,sample2.count());
+ }
+
+ public function testDefaultIfEmpty():Void {
+ var r;
+
+ r = new LINQ([])
+ .defaultIfEmpty(new LINQ(people));
+ this.assertEquals(10,r.count());
+
+ r = new LINQ([])
+ .defaultIfEmpty(people);
+ this.assertEquals(10,r.count());
+ }
+
+ public function testElementAtOrDefault():Void {
+ var r;
+ var defualt = { id: 0, firstName: "", lastName: "", bookIds: [] };
+
+ r = new LINQ(people)
+ .elementAtOrDefault(150,defualt);
+ this.assertEquals(defualt,r);
+ }
+
+ public function testFirstOrDefault():Void {
+ var r;
+ var defualt = { id: 999, firstName: "Johny", lastName: "Stone", bookIds:[999]};
+
+ r = new LINQ(people)
+ .firstOrDefault(defualt);
+ this.assertEquals("Chris",r.firstName);
+ }
+
+ public function testLastOrDefault():Void {
+ var r;
+ var defualt = { id: 999, firstName: "Johny", lastName: "Stone", bookIds:[999]};
+
+ r = new LINQ(people)
+ .lastOrDefault(defualt);
+ this.assertEquals("Kate",r.firstName);
+ }
+
+ static public var people:Array<Person> = [
+ { id: 1, firstName: "Chris", lastName: "Pearson", bookIds: [1001, 1002, 1003] },
+ { id: 2, firstName: "Kate", lastName: "Johnson", bookIds: [2001, 2002, 2003] },
+ { id: 3, firstName: "Josh", lastName: "Sutherland", bookIds: [3001, 3002, 3003] },
+ { id: 4, firstName: "John", lastName: "Ronald", bookIds: [4001, 4002, 4003] },
+ { id: 5, firstName: "Steve", lastName: "Pinkerton", bookIds: [1001, 1002, 1003] },
+ { id: 6, firstName: "Katie", lastName: "Zimmerman", bookIds: [2001, 2002, 2003] },
+ { id: 7, firstName: "Dirk", lastName: "Anderson", bookIds: [3001, 3002, 3003] },
+ { id: 8, firstName: "Chris", lastName: "Stevenson", bookIds: [4001, 4002, 4003] },
+ { id: 9, firstName: "Bernard", lastName: "Sutherland", bookIds: [1001, 2002, 3003] },
+ { id: 10, firstName: "Kate", lastName: "Pinkerton", bookIds: [4001, 3002, 2003] }
+ ];
+
+ public static function main():Void {
+ var runner = new haxe.unit.TestRunner();
+ runner.add(new Test());
+ runner.run();
+ }
+}
29 compile.hxml
@@ -0,0 +1,29 @@
+-main Test
+-cpp bin
+
+-cmd ./bin/Test
+
+--next
+-main Test
+-swf bin/Test.swf
+-swf-version 10
+
+-cmd open bin/Test.swf
+
+--next
+-main Test
+-neko bin/Test.n
+
+-cmd neko bin/Test.n
+
+--next
+-main Test
+-php /Library/WebServer/Documents/TestPHP
+
+-cmd open http://localhost/TestPHP
+
+--next
+-main Test
+-js /Library/WebServer/Documents/TestJS/Test.js
+
+-cmd open http://localhost/TestJS
6 haxelib.xml
@@ -0,0 +1,6 @@
+<project name="hxLINQ" url="http://github.com/andyli/hxLINQ" license="BSD">
+ <user name="andyli"/>
+ <tag v="cross"/>
+ <description>HaXe port of the LINQ to JavaScript (JSLINQ) Project. Tested on Flash/JS/PHP/Neko/C++ targets.</description>
+ <version name="1.0">First release. Complete and roughly tested.</version>
+</project>
198 hxLINQ/LINQ.hx
@@ -0,0 +1,198 @@
+/*
+ hxLINQ
+ HaXe port of the LINQ to JavaScript (JSLINQ) v2.1? Project - http://jslinq.codeplex.com
+
+ JSLINQ is licensed under the Microsoft Reciprocal License (Ms-RL)
+ Copyright (C) 2009 Chris Pietschmann (http://pietschsoft.com). All rights reserved.
+ The license can be found here: http://jslinq.codeplex.com/license
+*/
+
+package hxLINQ;
+
+using Lambda;
+
+class LINQ<T> {
+ public function new(dataItems:Iterable<T>):Void {
+ this.items = dataItems;
+ }
+
+ public function iterator():Iterator<T> {
+ return items.iterator();
+ }
+
+ public function where(clause:T->Int->Bool):LINQ<T> {
+ var i = 0;
+ var newList = new List<T>();
+ for (item in items) {
+ if (clause(item,i++)) {
+ newList.add(item);
+ }
+ }
+ return new LINQ(newList);
+ }
+
+ public function select<F>(clause:T->F):LINQ<F> {
+ var newList = new List<F>();
+
+ for (item in items) {
+ var newItem = clause(item);
+ if (newItem != null) {
+ newList.add(newItem);
+ }
+ }
+ return new LINQ(newList);
+ }
+
+ public function orderBy(clause:T->Int):LINQ<T> {
+ var tempArray = items.array();
+
+ tempArray.sort(function(a,b){
+ var x = clause(a);
+ var y = clause(b);
+ return ((x < y) ? -1 : ((x > y) ? 1 : 0));
+ });
+
+ return new LINQ(tempArray);
+ }
+
+ public function orderByDescending(clause:T->Int):LINQ<T> {
+ var tempArray = items.array();
+
+ tempArray.sort(function(a,b){
+ var x = clause(b);
+ var y = clause(a);
+ return ((x < y) ? -1 : ((x > y) ? 1 : 0));
+ });
+
+ return new LINQ(tempArray);
+ }
+
+ public function selectMany<F>(clause:T->Array<F>):LINQ<F> {
+ var r = new Array<F>();
+ for (item in items){
+ var a = clause(item);
+ r = r.concat(a);
+ }
+ return new LINQ(r);
+ }
+
+ public function count(?clause:T->Int->Bool):Int {
+ if (clause == null) {
+ return items.count();
+ } else {
+ return this.where(clause).items.count();
+ }
+ }
+
+ public function distinct<F>(clause:T->F):LINQ<F> {
+ var newItem;
+ var retVal = new List<F>();
+ for (item in items) {
+ newItem = clause(item);
+ if (!retVal.exists(function (f:F) return f == newItem)) {
+ retVal.add(newItem);
+ }
+ }
+ return new LINQ(retVal);
+ }
+
+ public function any(clause:T->Int->Bool):Bool {
+ var i = 0;
+ for (item in items) {
+ if (clause(item,i++)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public function all(clause:T->Int->Bool):Bool {
+ var i = 0;
+ for (item in items) {
+ if (!clause(item,i++)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public function reverse():LINQ<T> {
+ var tempAry = items.array();
+ tempAry.reverse();
+ return new LINQ(tempAry);
+ }
+
+ public function first(?clause:T->Int->Bool):T {
+ if (clause != null) {
+ return this.where(clause).first();
+ } else {
+ return items.iterator().next();
+ }
+ }
+
+ public function last(?clause:T->Int->Bool):T {
+ if (clause != null) {
+ return this.where(clause).last();
+ } else {
+ if (!items.empty()) {
+ return items.array().pop();
+ } else {
+ return null;
+ }
+ }
+ }
+
+ public function elementAt(i:Int):T {
+ return items.array()[i];
+ }
+
+ public function concat(items:Iterable<T>):LINQ<T> {
+ var tmpAry = this.items.array();
+ return new LINQ(tmpAry.concat(items.array()));
+ }
+
+ public function intersect<T2>(items:Iterable<T2>, ?clause:T->Int->T2->Int->Bool):LINQ<T> {
+ if (clause == null){
+ clause = function (item:T, index:Int, item2:Dynamic, index2:Int) { return item == item2; };
+ }
+
+ var result = new List<T>();
+ var ia = 0;
+ var ib = 0;
+ for (a in this.items) {
+ for (b in items) {
+ if (clause(a,ia,b,ib++)) {
+ result.add(a);
+ }
+ }
+ ++ia;
+ }
+ return new LINQ(result);
+ }
+
+ public function defaultIfEmpty(defaultValue:Iterable<T>):Iterable<T> {
+ if (this.empty()) {
+ return defaultValue;
+ } else {
+ return this;
+ }
+ }
+
+ public function elementAtOrDefault(i:Int, defaultValue:T):T {
+ if (i < 0) return defaultValue;
+ var r = this.elementAt(i);
+ return r == null ? defaultValue : r;
+ }
+
+ public function firstOrDefault(defaultValue:T):T {
+ var r = this.first();
+ return r == null ? defaultValue : r;
+ }
+
+ public function lastOrDefault(defaultValue:T):T {
+ var r = this.last();
+ return r == null ? defaultValue : r;
+ }
+
+ private var items:Iterable<T>;
+}

0 comments on commit b5693b8

Please sign in to comment.
Something went wrong with that request. Please try again.