3{-| A library for manipulating arrays of elements. The elements in an
4array must have the same type. The arrays are implemented in Relaxed
5Radix Balanced-Trees for fast updating and concating.
7If you use more then one map or zip function on an array, consider turning it
8into a list before operating on it, and then turning it back into an array.
11@docs empty, length, get, safeGet, getWithDefault
13# Putting Arrays Together
14@docs fill, fromList, concat, set, updates, push
17@docs elements, indices, assocs, slice
19# Mapping, folding, zipping
20@docs map, assocMap, foldl, foldr, zip, zipWith
30{-| Fills an array with a given length and a default element.
32 fill 3 5 == fromList [5,5,5]
34fill : Int -> a -> Array a
35fill len e = fromList <| List.map (always e) [1..len]
37-- TODO: make this a native function.
38{-| Create an array from a list. -}
39fromList : [a] -> Array a
40fromList = List.foldl (Native.Array.push) Native.Array.empty
42{-| Create a list of elements from an array.
44 elements (fromList [3,5,8]) == [3,5,8]
46elements : Array a -> [a]
47elements = Native.Array.toList
49{-| Create a list of the possible indices of an array.
51 indices (fromList [3,5,8] == [0,1,2])
53indices : Array a -> [Int]
54indices a = [0..Native.Array.length a - 1]
56{-| Create a list of tuples (Index, Element) from an array.
58 assocs (fromList [3,5,8]) == [(0,3), (1,5), (2,8)]
60assocs : Array a -> [(Int, a)]
61assocs a = List.zip (indices a) (Native.Array.elements a)
63{-| Apply a function on every element in an array.
65 map (\x -> x*x) (fromList [1,2,3]) == fromList [1,4,9]
67map : (a -> b) -> Array a -> Array b
70{-| Apply a function with an index on every element in an array.
72 assocMap (*) (fromList [5,5,5]) == fromList [0,5,10]
74assocMap : (Int -> a -> b) -> Array a -> Array b
75assocMap = Native.Array.assocMap
77{-| Reduce an array from the left.
79 foldl (+) 0 (fill 3 5) == 15
81foldl : (a -> b -> b) -> b -> Array a -> b
82foldl = Native.Array.foldl
84{-| Reduce an array from the right.
86 foldr (::) [] (fromList [1,2,3]) == [3,2,1]
88foldr : (a -> b -> b) -> b -> Array a -> b
89foldr = Native.Array.foldr
91{-| Zip the elements of two arrays via a tuple into a new array. If one array is
92longer, the extra elements are dropped.
94 zip (fromList [1,2,3,4]) (fromList [0,0,0]) = fromList [(1,0), (2,0), (3,0)]
96zip : Array a -> Array b -> Array (a,b)
99{-| Zips the elements of two arrays via a function into a new array. If one array
100is longer, the extra elements are dropped.
102 zipWith (+) (fill 3 5) (fill 5 8) == fill 3 13
104zipWith : (a -> b -> c) -> Array a -> Array b -> Array c
106 fromList <| List.zipWith f (Native.Array.elements a) (Native.Array.elements b)
108{-| Return an empty array.
113empty = Native.Array.empty
115{-| Push an element to the end of an array.
117 push 3 (fromList [1,2]) == fromList [1,2,3]
119push : a -> Array a -> Array a
120push = Native.Array.push
122{-| Return the element at the index. Breaks, if index is out of range. If the
123array length is unkown, use safeGet oder getWithDefault.
125 get 2 (A.fromList [3,2,1]) == 1
127get : Int -> Array a -> a
128get = Native.Array.get
130{-| Return Just the element at the index or Nothing if the index is out of range.
132 getMaybe 2 (A.fromList [3,2,1]) == Just 2
133 getMaybe 5 (A.fromList [3,2,1]) == Nothing
135getMaybe : Int -> Array a -> Maybe a
136getMaybe i array = if Native.Array.length array > i
137 then Just (Native.Array.get i array)
140{-| Get the element at the index. If the index is out of range, the given default
143 getSafe 0 2 (A.fromList [3,2,1]) == 1
144 getSafe 0 5 (A.fromList [3,2,1]) == 0
146getSafe : a -> Int -> Array a -> a
147getSafe default i array = if Native.Array.length array > i
148 then Native.Array.get i array else default
150{-| Set the element at the index. Returns the updated array, or if the index is
151out of range, the unaltered array.
153 set 1 7 (fromList [1,2,3]) == fromList [1,7,3]
155set : Int -> a -> Array a -> Array a
156set = Native.Array.set
158{-| Update an array with a a list tuples, wherein the first Int is the index.
160 updates [(1,7),(2,8)] (fill 3 1) == fromList [1,7,8]
162updates : [(Int, a)] -> Array a -> Array a
163updates assocs array = List.foldl (uncurry Native.Array.set) array assocs
165{-| Slice an array given a range. The selection is inclusive, so the last
166element in the selection will also be in the new array. This may change in the
168You can select from the end by giving a negative Int.
170 slice 1 2 (fromList [0,1,2,3,4]) == fromList [1,2]
171 slice 1 -2 (fromList [0,1,2,3,4]) == fromList [1,2,3]
172 slice -3 -2 (fromList [0,1,2,3,4]) == fromList [2,3]
174slice : Int -> Int -> Array a -> Array a
175slice = Native.Array.slice
177{-| Return the length of an array.
179 length (fromList [1,2,3]) == 3
181length : Array a -> Int
182length = Native.Array.length
184{-| Concat two arrays to a new one.
186 concat (fill 3 1) (fill 4 2) == fromList [1,1,1,2,2,2,2]
188concat : Array a -> Array a -> Array a
189concat = Native.Array.concat
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list