Skip to content

Commit

Permalink
Merge branch 'release/0.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
highluck committed Jul 3, 2021
2 parents afcdcc6 + 3ee676e commit 5b6df4d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
16 changes: 7 additions & 9 deletions dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package hy_go_function
type operation string

const (
filter = operation("filter")
maps = operation("map")
flatMap = operation("flatmap")
filter = operation("filter")
maps = operation("map")
flatMap = operation("flatmap")
)

type DSL struct {
function []interface{}
value interface{}
operations []operation
function []interface{}
value interface{}
operations []operation
}

func Of(value interface{}) DSL {
Expand All @@ -36,6 +36,4 @@ func (d DSL) Map(function interface{}) DSL {
d.operations = append(d.operations, maps)
d.function = append(d.function, function)
return d
}


}
21 changes: 19 additions & 2 deletions dsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func Test_map(t *testing.T) {

println(fmt.Sprintf("result : %v", result))

result2 := internal.DoMap(1, func(i int) int { return i + 1})
result2 := internal.DoMap(1, func(i int) int { return i + 1 })

println(fmt.Sprintf("result2 : %v", result2))
}
Expand All @@ -106,8 +106,25 @@ func Test_flatmap(t *testing.T) {

println(fmt.Sprintf("result : %v", result))

result2 := internal.DoMap(1, func(i int) int { return i + 1})
result2 := internal.DoMap(1, func(i int) int { return i + 1 })

println(fmt.Sprintf("result2 : %v", result2))
}

func Test_reduce(t *testing.T) {
array := []int{
1, 2, 3,
}

var result = internal.DoReduce(array, func(x int, y int) int {
return x + y
}, 0).(int)

println(fmt.Sprintf("result : %v", result))

var result2 = internal.DoReduce(array, func(x string, y int) string {
return x + "," + strconv.Itoa(y)
}, "").(string)

println(fmt.Sprintf("result2 : %v", result2))
}
5 changes: 5 additions & 0 deletions execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ func (d DSL) Execute() interface{} {
}
return d.value
}

func (d DSL) Reduce(function interface{}, initial interface{}) interface{} {
results := d.Execute()
return internal.DoReduce(results, function, initial)
}
21 changes: 21 additions & 0 deletions internal/do_reduce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package internal

import "reflect"

func DoReduce(input interface{}, function interface{}, initial interface{}) interface{} {
funcValue := reflect.ValueOf(function)

inValue := reflect.ValueOf(input)
inType := inValue.Type()
if inType.Kind() != reflect.Slice && inType.Kind() != reflect.Array {
result := funcValue.Call([]reflect.Value{inValue})[0]
return result.Interface()
}

initialValue := reflect.ValueOf(initial)
for i := 0; i < inValue.Len(); i++ {
elem := inValue.Index(i)
initialValue = funcValue.Call([]reflect.Value{initialValue, elem})[0]
}
return initialValue.Interface()
}

0 comments on commit 5b6df4d

Please sign in to comment.