summaryrefslogtreecommitdiff
blob: 26d13f60445d29169bd64704089e6485966775c2 (plain)
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
module Util.Array exposing
   (
      update,
      update_unsafe,
      filter_first,
      indexed_search
   )

import List
import Array

update : (
      Int ->
      ((Maybe t) -> (Maybe t)) ->
      (Array.Array t) ->
      (Array.Array t)
   )
update index fun array =
   case (fun (Array.get index array)) of
      Nothing -> array
      (Just e) -> (Array.set index e array)

update_unsafe : (
      Int ->
      (t -> t) ->
      (Array.Array t) ->
      (Array.Array t)
   )
update_unsafe index fun array =
   case (Array.get index array) of
      Nothing -> array
      (Just e) -> (Array.set index (fun e) array)

filter_first : (t -> Bool) -> (Array.Array t) -> (Maybe t)
filter_first fun array =
   (Array.get 0 (Array.filter fun array))

indexed_search : (t -> Bool) -> (Array.Array t) -> (Maybe (Int, t))
indexed_search fun array =
   (List.foldl
      (\v res ->
         (
            case res of
               (Just e) -> res
               Nothing ->
                  let (index, value) = v in
                     if (fun value)
                     then (Just v)
                     else Nothing
         )
      )
      Nothing
      (Array.toIndexedList array)
   )