package vals

// Iterator wraps the Iterate method.
type Iterator interface {
	// Iterate calls the passed function with each value within the receiver.
	// The iteration is aborted if the function returns false.
	Iterate(func(v interface{}) bool)
}

type cannotIterate struct{ kind string }

func ( cannotIterate) () string { return "cannot iterate " + .kind }

// CanIterate returns whether the value can be iterated. If CanIterate(v) is
// true, calling Iterate(v, f) will not result in an error.
func ( interface{}) bool {
	switch .(type) {
	case Iterator, string, List:
		return true
	}
	return false
}

// Iterate iterates the supplied value, and calls the supplied function in each
// of its elements. The function can return false to break the iteration. It is
// implemented for the builtin type string, the List type, and types satisfying
// the Iterator interface. For these types, it always returns a nil error. For
// other types, it doesn't do anything and returns an error.
func ( interface{},  func(interface{}) bool) error {
	switch v := .(type) {
	case string:
		for ,  := range  {
			 := (string())
			if ! {
				break
			}
		}
	case List:
		for  := .Iterator(); .HasElem(); .Next() {
			if !(.Elem()) {
				break
			}
		}
	case Iterator:
		.Iterate()
	default:
		return cannotIterate{Kind()}
	}
	return nil
}

// Collect collects all elements of an iterable value into a slice.
func ( interface{}) ([]interface{}, error) {
	var  []interface{}
	if  := Len();  >= 0 {
		 = make([]interface{}, 0, )
	}
	 := Iterate(, func( interface{}) bool {
		 = append(, )
		return true
	})
	return , 
}