Example #1
0
// revCons appends acc to the head of curr, where acc is in reverse order.
// acc must never be nil, curr can be.
func revCons(acc, curr ps.List) ps.List {
	if curr == nil {
		return acc.Reverse()
	}
	for !acc.IsNil() {
		acc, curr = acc.Tail(), curr.Cons(acc.Head())
	}
	return curr
}
Example #2
0
// Extend behaves like Append, except it takes a single slice or array value
// which will be concatenated to the named list.
//
// Unlike a variadic call to Append - which requires a []interface{} value -
// Extend accepts slices or arrays of any type.
//
// Extend will panic if the given value is not a slice, array, or nil.
func Extend(builder interface{}, name string, vs interface{}) interface{} {
	if vs == nil {
		return builder
	}

	maybeList, ok := getBuilderMap(builder).Lookup(name)

	var list ps.List
	if ok {
		list, ok = maybeList.(ps.List)
	}
	if !ok {
		list = ps.NewList()
	}

	forEach(vs, func(v interface{}) {
		list = list.Cons(v)
	})

	return Set(builder, name, list)
}
Example #3
0
func listToSlice(list ps.List, arrayType reflect.Type) reflect.Value {
	size := list.Size()
	slice := reflect.MakeSlice(arrayType, size, size)
	for i := size - 1; i >= 0; i-- {
		val := reflect.ValueOf(list.Head())
		slice.Index(i).Set(val)
		list = list.Tail()
	}
	return slice
}