|
| 1 | +module Data.Array where |
| 2 | + |
| 3 | +import Prelude |
| 4 | +import Data.Maybe |
| 5 | + |
| 6 | +head :: forall a. [a] -> Maybe a |
| 7 | +head (x : _) = Just x |
| 8 | +head _ = Nothing |
| 9 | + |
| 10 | +tail :: forall a. [a] -> Maybe [a] |
| 11 | +tail (_ : xs) = Just xs |
| 12 | +tail _ = Nothing |
| 13 | + |
| 14 | +map :: forall a b. (a -> b) -> [a] -> [b] |
| 15 | +map _ [] = [] |
| 16 | +map f (x:xs) = f x : map f xs |
| 17 | + |
| 18 | +foreign import length |
| 19 | + "function length(xs) {\ |
| 20 | + \ return xs.length;\ |
| 21 | + \}" :: forall a. [a] -> Number |
| 22 | + |
| 23 | +foreign import indexOf |
| 24 | + "function indexOf(l) {\ |
| 25 | + \ return function (e) {\ |
| 26 | + \ return l.indexOf(e);\ |
| 27 | + \ };\ |
| 28 | + \}" :: forall a. [a] -> a -> Number |
| 29 | + |
| 30 | +foreign import lastIndexOf |
| 31 | + "function lastIndexOf(l) {\ |
| 32 | + \ return function (e) {\ |
| 33 | + \ return l.lastIndexOf(e);\ |
| 34 | + \ };\ |
| 35 | + \}" :: forall a. [a] -> a -> Number |
| 36 | + |
| 37 | +foreign import concat |
| 38 | + "function concat(l1) {\ |
| 39 | + \ return function (l2) {\ |
| 40 | + \ return l1.concat(l2);\ |
| 41 | + \ };\ |
| 42 | + \}" :: forall a. [a] -> [a] -> [a] |
| 43 | + |
| 44 | +foreign import joinS |
| 45 | + "function joinS(l) {\ |
| 46 | + \ return l.join();\ |
| 47 | + \}" :: [String] -> String |
| 48 | + |
| 49 | +foreign import joinWith |
| 50 | + "function joinWith(l) {\ |
| 51 | + \ return function (s) {\ |
| 52 | + \ return l.join(s);\ |
| 53 | + \ };\ |
| 54 | + \}" :: [String] -> String -> String |
| 55 | + |
| 56 | +foreign import push |
| 57 | + "function push(l) {\ |
| 58 | + \ return function (e) {\ |
| 59 | + \ var l1 = l.slice();\ |
| 60 | + \ l1.push(e); \ |
| 61 | + \ return l1;\ |
| 62 | + \ };\ |
| 63 | + \}" :: forall a. [a] -> a -> [a] |
| 64 | + |
| 65 | +foreign import reverse |
| 66 | + "function reverse(l) {\ |
| 67 | + \ var l1 = l.slice();\ |
| 68 | + \ l1.reverse(); \ |
| 69 | + \ return l1;\ |
| 70 | + \}" :: forall a. [a] -> [a] |
| 71 | + |
| 72 | +foreign import shift |
| 73 | + "function shift(l) {\ |
| 74 | + \ var l1 = l.slice();\ |
| 75 | + \ l1.shift();\ |
| 76 | + \ return l1;\ |
| 77 | + \}" :: forall a. [a] -> [a] |
| 78 | + |
| 79 | +foreign import slice |
| 80 | + "function slice(s) {\ |
| 81 | + \ return function(e) {\ |
| 82 | + \ return function (l) {\ |
| 83 | + \ return l.slice(s, e);\ |
| 84 | + \ };\ |
| 85 | + \ };\ |
| 86 | + \}" :: forall a. Number -> Number -> [a] -> [a] |
| 87 | + |
| 88 | +foreign import sort |
| 89 | + "function sort(l) {\ |
| 90 | + \ var l1 = l.slice();\ |
| 91 | + \ l1.sort();\ |
| 92 | + \ return l1;\ |
| 93 | + \}" :: forall a. [a] -> [a] |
| 94 | + |
| 95 | +foreign import insertAt |
| 96 | + "function insertAt(index) {\ |
| 97 | + \ return function(a) {\ |
| 98 | + \ return function(l) {\ |
| 99 | + \ var l1 = l.slice();\ |
| 100 | + \ l1.splice(index, 0, a);\ |
| 101 | + \ return l1;\ |
| 102 | + \ }; \ |
| 103 | + \ };\ |
| 104 | + \}":: forall a. Number -> a -> [a] -> [a] |
| 105 | + |
| 106 | +foreign import deleteAt |
| 107 | + "function deleteAt(index) {\ |
| 108 | + \ return function(n) {\ |
| 109 | + \ return function(l) {\ |
| 110 | + \ var l1 = l.slice();\ |
| 111 | + \ l1.splice(index, n);\ |
| 112 | + \ return l1;\ |
| 113 | + \ }; \ |
| 114 | + \ };\ |
| 115 | + \}":: forall a. Number -> Number -> [a] -> [a] |
| 116 | + |
| 117 | +foreign import updateAt |
| 118 | + "function updateAt(index) {\ |
| 119 | + \ return function(a) {\ |
| 120 | + \ return function(l) {\ |
| 121 | + \ var l1 = l.slice();\ |
| 122 | + \ l1[index] = a;\ |
| 123 | + \ return l1;\ |
| 124 | + \ }; \ |
| 125 | + \ };\ |
| 126 | + \}":: forall a. Number -> a -> [a] -> [a] |
| 127 | + |
| 128 | +infixr 6 : |
| 129 | + |
| 130 | +(:) :: forall a. a -> [a] -> [a] |
| 131 | +(:) a = concat [a] |
| 132 | + |
| 133 | +singleton :: forall a. a -> [a] |
| 134 | +singleton a = [a] |
| 135 | + |
| 136 | +concatMap :: forall a b. (a -> [b]) -> [a] -> [b] |
| 137 | +concatMap _ [] = [] |
| 138 | +concatMap f (a:as) = f a `concat` concatMap f as |
| 139 | + |
| 140 | +filter :: forall a. (a -> Boolean) -> [a] -> [a] |
| 141 | +filter _ [] = [] |
| 142 | +filter p (x:xs) | p x = x : filter p xs |
| 143 | +filter p (_:xs) = filter p xs |
| 144 | + |
| 145 | +isEmpty :: forall a. [a] -> Boolean |
| 146 | +isEmpty [] = true |
| 147 | +isEmpty _ = false |
| 148 | + |
| 149 | +range :: Number -> Number -> [Number] |
| 150 | +range lo hi | lo > hi = [] |
| 151 | +range lo hi = lo : range (lo + 1) hi |
| 152 | + |
| 153 | +zipWith :: forall a b c. (a -> b -> c) -> [a] -> [b] -> [c] |
| 154 | +zipWith f (a:as) (b:bs) = f a b : zipWith f as bs |
| 155 | +zipWith _ _ _ = [] |
| 156 | + |
| 157 | +drop :: forall a. Number -> [a] -> [a] |
| 158 | +drop 0 xs = xs |
| 159 | +drop _ [] = [] |
| 160 | +drop n (x:xs) = drop (n - 1) xs |
| 161 | + |
| 162 | +take :: forall a. Number -> [a] -> [a] |
| 163 | +take 0 _ = [] |
| 164 | +take _ [] = [] |
| 165 | +take n (x:xs) = x : take (n - 1) xs |
| 166 | + |
| 167 | +nub :: forall a. (Eq a) => [a] -> [a] |
| 168 | +nub = nubBy (==) |
| 169 | + |
| 170 | +nubBy :: forall a. (a -> a -> Boolean) -> [a] -> [a] |
| 171 | +nubBy _ [] = [] |
| 172 | +nubBy (==) (x:xs) = x : nubBy (==) (filter (\y -> not (x == y)) xs) |
| 173 | + |
| 174 | +instance showArray :: (Show a) => Show [a] where |
| 175 | + show xs = "[" ++ joinWith (map show xs) "," ++ "]" |
| 176 | + |
| 177 | +instance eqArray :: (Eq a) => Eq [a] where |
| 178 | + (==) [] [] = true |
| 179 | + (==) (x:xs) (y:ys) = x == y && xs == ys |
| 180 | + (==) _ _ = false |
| 181 | + (/=) xs ys = not (xs == ys) |
| 182 | + |
| 183 | +instance monadArray :: Monad [] where |
| 184 | + return = singleton |
| 185 | + (>>=) = flip concatMap |
| 186 | + |
| 187 | +instance functorArray :: Functor [] where |
| 188 | + (<$>) = map |
| 189 | + |
| 190 | +instance alternativeArray :: Alternative [] where |
| 191 | + empty = [] |
| 192 | + (<|>) = concat |
0 commit comments