implementation module UtilArray

import StdBool, StdArray, StdEnum

copyA a	:==	{ e \\ e <-: a }
/* RWS FIXME overloading error
filterA :: (e -> Bool) (a e) -> a e | Array a e;
filterA  predicate array
	= filter
	where
		arraySize
			=	size array

		filter
			| firstFound >= arraySize
				=	array
			| otherwise
				=	shiftElements firstFound (firstFound+1) (copyA array)

		firstFound
			=	findFirst 0
		findFirst i
			|  i >= arraySize || not (predicate array.[i])
				=	i
			| otherwise
				=	findFirst (i+1)

		shiftElements toPos fromPos array
			| fromPos >= arraySize
				=	{e \\ e <-: array & i <- [0..toPos-1]} // RWS +++ chop
			#! element
				=	array.[fromPos]
			| not (predicate element)
				=	shiftElements toPos (fromPos+1) array
			| otherwise
				=	shiftElements (toPos+1) (fromPos+1) {array & [toPos] = element}
*/
// foldrA :: (a -> .(.b -> .b)) .b .(m a) -> .b | ArrayElem a & Array .m
foldrA op r a
	:==	foldrA r (size a - 1)
	where
		arrraySize
			=	size a
		foldrA r i
			| i == (-1)
				=	r
			// otherwise
				=	op a.[i] (foldrA r (i-1))

// foldlA :: (a -> .(.b -> .b)) .b .(m a) -> .b | ArrayElem a & Array .m
foldlA op r a
	:==	foldlA r 0
	where
		arrraySize
			=	size a
		foldlA r i
			| i == arrraySize
				=	r
			// otherwise
				=	op a.[i] (foldlA r (i+1))
