Permalink
Browse files

Changed type param of LINQ. Use Grouping instead of IGrouping.

  • Loading branch information...
1 parent e2092af commit 91a3b30fd7191fe9d424c5e560a8e947abe86350 @andyli committed Oct 6, 2012
Showing with 54 additions and 84 deletions.
  1. +1 −0 compile.hxml
  2. +36 −40 hxLINQ/LINQ.hx
  3. +17 −44 { → test}/Test.hx
View
@@ -1,4 +1,5 @@
-main Test
-cpp bin
+-cp test
-cmd ./bin/Test
View
@@ -11,16 +11,16 @@ package hxLINQ;
using Lambda;
-class LINQ<T> {
- public function new(dataItems:Iterable<T>):Void {
+class LINQ<T,C:Iterable<T>> {
+ public function new(dataItems:C):Void {
this.items = dataItems;
}
public function iterator():Iterator<T> {
return items.iterator();
}
- public function where(clause:T->Int->Bool):LINQ<T> {
+ public function where(clause:T->Int->Bool):LINQ<T,List<T>> {
var i = 0;
var newList = new List<T>();
for (item in items) {
@@ -31,7 +31,7 @@ class LINQ<T> {
return new LINQ(newList);
}
- public function select<F>(clause:T->F):LINQ<F> {
+ public function select<F>(clause:T->F):LINQ<F,List<F>> {
var newList = new List<F>();
for (item in items) {
@@ -43,7 +43,7 @@ class LINQ<T> {
return new LINQ(newList);
}
- public function orderBy<T2>(clause:T->T2):OrderedLINQ<T> {
+ public function orderBy<T2>(clause:T->T2):OrderedLINQ<T,Array<T>> {
var tempArray = items.array();
var sortFn = function(a, b) {
var x = clause(a);
@@ -55,7 +55,7 @@ class LINQ<T> {
return new OrderedLINQ(tempArray, [sortFn]);
}
- public function orderByDescending<T2>(clause:T->T2):OrderedLINQ<T> {
+ public function orderByDescending<T2>(clause:T->T2):OrderedLINQ<T,Array<T>> {
var tempArray = items.array();
var sortFn = function(a, b) {
var x = clause(b);
@@ -67,23 +67,23 @@ class LINQ<T> {
return new OrderedLINQ(tempArray, [sortFn]);
}
- public function groupBy<F>(clause:T->F) : LINQ<IGrouping<F,T>> {
+ public function groupBy<F>(clause:T->F) : LINQ<Grouping<F,T>,Array<Grouping<F,T>>> {
var lists = new Array<Grouping<F,T>>();
for (item in items) {
var f = clause(item);
- var list = new LINQ(lists).where(function(g:IGrouping<F,T>, i:Int) return g.key == f).first();
+ var list = new LINQ(lists).where(function(g:Grouping<F,T>, i:Int) return g.key == f).first();
if (list == null) {
list = new Grouping<F,T>(f);
lists.push(list);
}
list.add(item);
}
- return new LINQ(cast lists);
+ return new LINQ(lists);
}
- public function selectMany<F>(clause:T->Array<F>):LINQ<F> {
+ public function selectMany<F>(clause:T->Array<F>):LINQ<F,Array<F>> {
var r = new Array<F>();
for (item in items){
var a = clause(item);
@@ -136,7 +136,7 @@ class LINQ<T> {
return this.sum(clause)/this.count();
}
- public function distinct<F>(clause:T->F):LINQ<F> {
+ public function distinct<F>(clause:T->F):LINQ<F,List<F>> {
var newItem;
var retVal = new List<F>();
for (item in items) {
@@ -168,7 +168,7 @@ class LINQ<T> {
return true;
}
- public function reverse():LINQ<T> {
+ public function reverse():LINQ<T,Array<T>> {
var tempAry = items.array();
tempAry.reverse();
return new LINQ(tempAry);
@@ -202,12 +202,12 @@ class LINQ<T> {
return null;
}
- public function concat(items:Iterable<T>):LINQ<T> {
+ public function concat(items:Iterable<T>):LINQ<T,Array<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> {
+ public function intersect<T2>(items:Iterable<T2>, ?clause:T->Int->T2->Int->Bool):LINQ<T,List<T>> {
if (clause == null){
clause = function (item:T, index:Int, item2:Dynamic, index2:Int) { return item == item2; };
}
@@ -250,7 +250,7 @@ class LINQ<T> {
return r == null ? defaultValue : r;
}
- private var items:Iterable<T>;
+ public var items(default,null):C;
static private function indexOf<F>(items:Iterable<F>, item:F):Int {
var i = 0;
@@ -265,22 +265,23 @@ class LINQ<T> {
}
}
-private class OrderedLINQ<T> extends LINQ<T> {
+class OrderedLINQ<T,C:Iterable<T>> extends LINQ<T,C> {
private var sortFns:Array<T->T->Int>;
- public function new(dataItems:Iterable<T>, sortFns:Array<T->T->Int>) {
+ public function new(dataItems:C, sortFns:Array<T->T->Int>) {
super(dataItems);
this.sortFns = sortFns;
}
- public function thenBy<T2>(clause:T->T2):OrderedLINQ<T> {
+ public function thenBy<T2>(clause:T->T2):OrderedLINQ<T,Array<T>> {
var tempArray:Array<T> = items.array();
- var _sortFns = sortFns.copy();
- _sortFns.push(function(a, b) {
- var x = clause(a);
- var y = clause(b);
- return Reflect.compare(x,y);
- });
+ var _sortFns = sortFns.concat([
+ function(a, b) {
+ var x = clause(a);
+ var y = clause(b);
+ return Reflect.compare(x,y);
+ }
+ ]);
tempArray.sort(function(a, b) {
var r:Int = 0;
@@ -295,14 +296,15 @@ private class OrderedLINQ<T> extends LINQ<T> {
return new OrderedLINQ(tempArray, _sortFns);
}
- public function thenByDescending<T2>(clause:T->T2):OrderedLINQ<T> {
+ public function thenByDescending<T2>(clause:T->T2):OrderedLINQ<T,Array<T>> {
var tempArray:Array<T> = items.array();
- var _sortFns = sortFns.copy();
- _sortFns.push(function(a, b) {
- var x = clause(b);
- var y = clause(a);
- return Reflect.compare(x,y);
- });
+ var _sortFns = sortFns.concat([
+ function(a, b) {
+ var x = clause(b);
+ var y = clause(a);
+ return Reflect.compare(x,y);
+ }
+ ]);
tempArray.sort(function(a, b) {
var r:Int = 0;
@@ -318,14 +320,8 @@ private class OrderedLINQ<T> extends LINQ<T> {
}
}
-
-interface IGrouping<K,V> {
- public var key(default,null):K;
-
- public function iterator():Iterator<V>;
-}
-
-private class Grouping<K,V> implements IGrouping<K,V> {
+@:allow(hxLINQ.LINQ)
+private class Grouping<K,V> {
public var key(default,null):K;
private var values:List<V>;
@@ -334,7 +330,7 @@ private class Grouping<K,V> implements IGrouping<K,V> {
values = new List<V>();
}
- public function add(val:V):Void {
+ private function add(val:V):Void {
values.add(val);
}
View
@@ -8,67 +8,55 @@ class Test extends haxe.unit.TestCase{
public function testWhere():Void {
var r;
- r = new LINQ(people)
+ r = new LINQ(people)
.where(function(p:Person, i:Int) return p.firstName == "Chris");
this.assertEquals(2,r.count());
- r = new LINQ(people)
+ 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)
+ var 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)
+ var 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)
+ var 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)
+ var 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 testOrderByString():Void {
- var r;
-
- r = new LINQ(people)
+ var r = new LINQ(people)
.orderBy(function(p:Person) return p.firstName);
this.assertEquals(10, r.count());
this.assertEquals("Bernard",r.first().firstName);
this.assertEquals("Steve",r.last().firstName);
}
public function testOrderByStringDescending():Void {
- var r;
-
- r = new LINQ(people)
+ var r = new LINQ(people)
.orderByDescending(function(p:Person) return p.firstName);
this.assertEquals(10,r.count());
this.assertEquals("Bernard",r.last().firstName);
@@ -134,9 +122,7 @@ class Test extends haxe.unit.TestCase{
}
public function testDistinct():Void {
- var r;
-
- r = new LINQ(people)
+ var r = new LINQ(people)
.distinct(function(p:Person) return p.firstName);
this.assertEquals(8,r.count());
}
@@ -166,9 +152,7 @@ class Test extends haxe.unit.TestCase{
}
public function testReverse():Void {
- var r;
-
- r = new LINQ(people)
+ var r = new LINQ(people)
.reverse();
this.assertEquals(10,r.count());
this.assertEquals("Kate",r.first().firstName);
@@ -204,9 +188,7 @@ class Test extends haxe.unit.TestCase{
}
public function testElementAt():Void {
- var r;
-
- r = new LINQ(people)
+ var r = new LINQ(people)
.elementAt(1);
this.assertEquals("Kate",r.firstName);
this.assertEquals(2,r.id);
@@ -240,45 +222,38 @@ class Test extends haxe.unit.TestCase{
}
public function testElementAtOrDefault():Void {
- var r;
var defualt = { id: 0, firstName: "", lastName: "", bookIds: [] };
- r = new LINQ(people)
+ var 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)
+ var 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)
+ var r = new LINQ(people)
.lastOrDefault(defualt);
this.assertEquals("Kate",r.firstName);
}
public function testGroupBy():Void {
- var r;
-
- r = new LINQ(people)
+ var r = new LINQ(people)
.groupBy(function(p:Person) return p.firstName.charAt(0));
this.assertEquals(6,r.count());
}
public function testThenBy():Void {
- var r;
-
- r = new LINQ(people)
+ var r = new LINQ(people)
.orderBy(function(p:Person) return p.firstName.charCodeAt(0))
.thenBy(function(p:Person) return p.lastName.charCodeAt(0))
.select(function(p:Person) return p.id);
@@ -287,9 +262,7 @@ class Test extends haxe.unit.TestCase{
}
public function testThenByDescending():Void {
- var r;
-
- r = new LINQ(people)
+ var r = new LINQ(people)
.orderBy(function(p:Person) return p.firstName.charCodeAt(0))
.thenByDescending(function(p:Person) return p.lastName.charCodeAt(0))
.select(function(p:Person) return p.id);

0 comments on commit 91a3b30

Please sign in to comment.