Skip to content

Commit cf7e7a0

Browse files
committed
Initial commit
0 parents  commit cf7e7a0

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/js/
2+
/externs/
3+
/node_modules/
4+
/bower_components/

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 PureScript
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

bower.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "purescript-arrays",
3+
"version": "0.0.0",
4+
"homepage": "https://github.com/purescript/purescript-arrays",
5+
"description": "Array utility functions",
6+
"keywords": [
7+
"purescript"
8+
],
9+
"license": "MIT",
10+
"ignore": [
11+
"**/.*",
12+
"node_modules",
13+
"bower_components",
14+
"examples",
15+
"externs",
16+
"js"
17+
],
18+
"dependencies": {
19+
"purescript-maybe": "*"
20+
}
21+
}

src/Data/Array.purs

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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

src/Data/Array/Unsafe.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Data.Array.Unsafe where
2+
3+
head :: forall a. [a] -> a
4+
head (x : _) = x
5+
6+
tail :: forall a. [a] -> [a]
7+
tail (_ : xs) = xs

0 commit comments

Comments
 (0)