1module List.Ext exposing (..)
2
3import List.Extra as List
4
5
6{-| Flipped version of `append`.
7
8 >>> add [2, 3] [1]
9 [1, 2, 3]
10
11-}
12add : List a -> List a -> List a
13add a b =
14 List.append b a
15
16
17{-| Flipped version of (::).
18
19 >>> addTo [2, 3] 1
20 [1, 2, 3]
21
22-}
23addTo : List a -> a -> List a
24addTo list item =
25 item :: list
26
27
28{-| Move an item "from" an index "to" another index.
29Putting the item in front of the `to` index.
30
31 >>> move { from = 0, to = 2, amount = 1 } [1, 2, 3]
32 [2, 1, 3]
33
34 >>> move { from = 2, to = 0, amount = 1 } [1, 2, 3]
35 [3, 1, 2]
36
37 >>> move { from = 2, to = 7, amount = 3 } [0, 1, 2, 3, 4, 5, 6, 7]
38 [0, 1, 5, 6, 2, 3, 4, 7]
39
40 >>> move { from = 2, to = 1, amount = 3 } [0, 1, 2, 3, 4, 5, 6, 7]
41 [0, 2, 3, 4, 1, 5, 6, 7]
42
43-}
44move : { amount : Int, from : Int, to : Int } -> List a -> List a
45move { from, to, amount } list =
46 []
47 ++ (list |> List.take (min from to))
48 ++ (list |> List.take to |> List.drop (from + amount))
49 ++ (list |> List.drop from |> List.take amount)
50 ++ (list |> List.take from |> List.drop to)
51 ++ (list |> List.drop (max (from + amount) to))
52
53
54pickIndexes : List Int -> List a -> List a
55pickIndexes indexes items =
56 List.foldr
57 (\idx acc ->
58 items
59 |> List.getAt idx
60 |> Maybe.map (addTo acc)
61 |> Maybe.withDefault acc
62 )
63 []
64 indexes
65
66
67{-| Exclude a list from another list.
68
69 >>> without [ 2 ] [ 1, 2, 3 ]
70 [ 1, 3 ]
71
72-}
73without : List a -> List a -> List a
74without exclude =
75 List.filter (\c -> List.notMember c exclude)