Esempio n. 1
0
func TestJoin(t *testing.T) {
	a := []string{"satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh"}
	b := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	cjoin := crowd.From(&a).Join(&b, func(x, y interface{}) bool {
		xi := len(x.(string))
		yi := y.(int)
		return xi == yi
	}, nil).Group(func(x interface{}) interface{} {
		return x.(toolkit.M).Get("data2")
	}, func(x interface{}) interface{} {
		return x.(toolkit.M).Get("data1")
	}).Exec()
	check(t, cjoin.Error, "")
	toolkit.Printfn("Data:\n%s", toolkit.JsonString(cjoin.Result.Data()))
}
Esempio n. 2
0
func TestWhereSelectGroup(t *testing.T) {
	skipIfNil(t)

	c1 := *c
	cwhere := c1.Where(func(x interface{}) interface{} {
		if x.(Obj).I < 200 {
			return true
		}
		return false
	}).Exec()
	check(t, cwhere.Error, "")
	toolkit.Println("First 20 data: ", toolkit.JsonString(cwhere.Result.Data().([]Obj)))

	cselect := cwhere.Apply(func(x interface{}) interface{} {
		return x.(Obj).F
	}).Exec()
	check(t, cselect.Error, "")
	toolkit.Println("Select : First 20 data: ", toolkit.JsonString(cselect.Result.Data().([]float64)[:20]))

	cgroup := cselect.Group(func(x interface{}) interface{} {
		return (x.(float64) - math.Mod(x.(float64), float64(100))) / float64(100)
	}, nil).Exec()
	check(t, cgroup.Error, "")
	datas := cgroup.Result.Data().([]crowd.KV)
	for _, v := range datas {
		toolkit.Printf("Group %2.0f: %d data: %v\n",
			v.Key,
			len(v.Value.([]float64)),
			v.Value.([]float64))
	}

	cgroupaggr := cselect.Apply(func(x interface{}) interface{} {
		kv := x.(crowd.KV)
		vs := kv.Value.([]float64)
		sum := crowd.From(&vs).Sum(nil).Exec().Result.Sum
		return crowd.KV{kv.Key, sum}
	}).Exec()
	check(t, cgroupaggr.Error, "")
	toolkit.Println("GroupAggr: First 20 data: ", toolkit.JsonString(cgroupaggr.Result.Data().([]crowd.KV)))

	cgroupaggrmax := cgroupaggr.Max(func(x interface{}) interface{} {
		return x.(crowd.KV).Value
	}).Exec()
	check(t, cgroupaggrmax.Error, "")
	toolkit.Println("GroupAggrMax: ", cgroupaggrmax.Result.Max)

	//c = crowd.From(&objs)
	toolkit.Printfn("Data len: %d", c.Len())
	c2 := *c
	oneshot := c2.Apply(func(x interface{}) interface{} {
		return float64(x.(Obj).I)
	}).Group(func(x interface{}) interface{} {
		return (x.(float64) - math.Mod(x.(float64), float64(100))) / float64(100)
	}, nil).Apply(func(x interface{}) interface{} {
		kv := x.(crowd.KV)
		vs := kv.Value.([]float64)
		sum := crowd.From(&vs).Sum(nil).Exec().Result.Sum
		return crowd.KV{kv.Key, sum}
	}).Sum(func(x interface{}) interface{} {
		return x.(crowd.KV).Value
	}).Exec()
	check(t, oneshot.Error, "")
	toolkit.Println("Oneshot: ", oneshot.Result.Sum)
}