-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayExtra.elm
64 lines (47 loc) · 1.71 KB
/
ArrayExtra.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module ArrayExtra exposing (..)
{-| This module exists because core's Array is broken and elm-community/array-extra is only for core's Array, and does not contain `concat`. -}
import Array.Hamt exposing (..)
concat : Array (Array a) -> Array a
concat = foldr append empty
concatMap : (a -> Array b) -> Array a -> Array b
concatMap f a = concat (map f a)
-- Everything after this point is copied from elm-community/array-extra.
{-| Unsafe version of get, don't use this unless you know what you're doing!
-}
getUnsafe : Int -> Array a -> a
getUnsafe n xs =
case get n xs of
Just x ->
x
Nothing ->
Debug.crash ("Index " ++ Basics.toString n ++ " of Array with length " ++ Basics.toString (length xs) ++ " is not reachable.")
{-| Apply an array of functions to an array of values.
-}
apply : Array (a -> b) -> Array a -> Array b
apply fs xs =
let
l =
min (length fs) (length xs)
fs_ =
slice 0 l fs
in
indexedMap (\n f -> f (getUnsafe n xs)) fs_
{-| Combine two arrays, combining them with the given function.
If one array is longer, the extra elements are dropped.
map2 (+) [1,2,3] [1,2,3,4] == [2,4,6]
map2 (,) [1,2,3] ['a','b'] == [ (1,'a'), (2,'b') ]
pairs : Array a -> Array b -> Array (a,b)
pairs lefts rights =
map2 (,) lefts rights
-}
map2 : (a -> b -> result) -> Array a -> Array b -> Array result
map2 f ws =
apply (map f ws)
{-| -}
map3 : (a -> b -> c -> result) -> Array a -> Array b -> Array c -> Array result
map3 f ws xs =
apply (map2 f ws xs)
{-| -}
map4 : (a -> b -> c -> d -> result) -> Array a -> Array b -> Array c -> Array d -> Array result
map4 f ws xs ys =
apply (map3 f ws xs ys)