forked from elmcraft/core-extra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertAt.elm
More file actions
115 lines (87 loc) · 2.67 KB
/
InsertAt.elm
File metadata and controls
115 lines (87 loc) · 2.67 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module List.Extra.InsertAt exposing
( insertAtRecursion
, insertAtRecursion2
, insertAtRecursion3
, insertAtSplitAt
, insertAtTakeDrop
)
import List.Extra
insertAtRecursion : Int -> a -> List a -> List a
insertAtRecursion index value list =
if index <= -1 then
list
else
let
go : Int -> List a -> List a -> List a
go i rest acc =
if i == index then
List.reverse acc ++ (value :: rest)
else
case rest of
[] ->
-- index > length list
list
head :: newRest ->
go (i + 1) newRest (head :: acc)
in
go 0 list []
insertAtRecursion2Help : Int -> a -> List a -> Int -> List a -> List a -> List a
insertAtRecursion2Help index value list i rest acc =
if i == index then
List.foldl (::) (value :: rest) acc
else
case rest of
[] ->
-- index > length list
list
head :: newRest ->
insertAtRecursion2Help index value list (i + 1) newRest (head :: acc)
insertAtRecursion2 : Int -> a -> List a -> List a
insertAtRecursion2 index value list =
if index <= -1 then
list
else
insertAtRecursion2Help index value list 0 list []
insertAtTakeDrop : Int -> a -> List a -> List a
insertAtTakeDrop index value list =
if index <= -1 then
list
else
let
length =
List.length list
in
if length < index then
list
else
List.take index list ++ (value :: List.drop index list)
insertAtSplitAt : Int -> a -> List a -> List a
insertAtSplitAt index value list =
if index <= -1 then
list
else
let
( before, after ) =
List.Extra.splitAt index list
in
if List.isEmpty after && List.length before < index then
list
else
before ++ (value :: after)
insertAtRecursion3Help : a -> List a -> Int -> List a -> List a -> List a
insertAtRecursion3Help value list i rest acc =
if i == 0 then
List.foldl (::) (value :: rest) acc
else
case rest of
[] ->
-- index > length list
list
head :: newRest ->
insertAtRecursion3Help value list (i - 1) newRest (head :: acc)
insertAtRecursion3 : Int -> a -> List a -> List a
insertAtRecursion3 index value list =
if index <= -1 then
list
else
insertAtRecursion3Help value list index list []