-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extras.elm
63 lines (40 loc) · 1.19 KB
/
Extras.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
module Extras exposing (..)
-- derived from http://stackoverflow.com/a/33625733/4496839
-- has less helper functions than the List.Extra version
indexOfhelper : List a -> a -> Int -> Maybe Int
indexOfhelper lst elem offset =
case lst of
[] ->
Nothing
x :: xs ->
if x == elem then
Just offset
else
indexOfhelper xs elem (offset + 1)
indexOf : List a -> a -> Maybe Int
indexOf lst element =
indexOfhelper lst element 0
-- A lot of the time this default is fine
indexOfDefault : List a -> a -> Int
indexOfDefault lst element =
indexOfhelper lst element 0
|> Maybe.withDefault -1
-- from https://github.com/elm-community/list-extra
find : (a -> Bool) -> List a -> Maybe a
find predicate list =
case list of
[] ->
Nothing
first :: rest ->
if predicate first then
Just first
else
find predicate rest
-- from https://github.com/elm-community/maybe-extra
orElseLazy : (() -> Maybe a) -> Maybe a -> Maybe a
orElseLazy fma mb =
case mb of
Nothing ->
fma ()
Just _ ->
mb