forked from anbinh/dna
/
utils.go
41 lines (37 loc) · 1.2 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package dna
import (
"fmt"
"reflect"
"strconv"
)
// FromCharCode returns char from charcode
func FromCharCode(code Int) String {
return String(fmt.Sprintf("%c", code))
}
// ParseInt returns Int from String with a specific base
func ParseInt(s String, base Int) Int {
result, _ := strconv.ParseInt(string(s), int(base), 0)
return Int(result)
}
// Implement the map utility with anonymous function.
// It is rewritten from http://godoc.org/github.com/BurntSushi/ty/fun.
// The anonymous func has the form: func(func(A) B, []A) []B.
// It maps the every element in array A to other element type B.
// The final result is an array of elements type B
//
// It does not check the values of anonymous function fn and iterated array xs for speed advantage
func Map(xs, fn interface{}) interface{} {
vf, vxs := reflect.ValueOf(fn), reflect.ValueOf(xs)
xsLen := vxs.Len()
vys := reflect.MakeSlice(reflect.SliceOf(vf.Type().Out(0)), xsLen, xsLen)
for i := 0; i < xsLen; i++ {
if vf.Type().NumIn() == 2 {
vy := vf.Call([]reflect.Value{vxs.Index(i), reflect.ValueOf(Int(i))})[0]
vys.Index(i).Set(vy)
} else {
vy := vf.Call([]reflect.Value{vxs.Index(i)})[0]
vys.Index(i).Set(vy)
}
}
return vys.Interface()
}