Skip to content

Commit 4a295d6

Browse files
committed
Optimized version of List:drop
1 parent 094a676 commit 4a295d6

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

List.ark

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,21 @@
6767
# (let cool-stuff [1 2 3 4 5 6 7 8 9])
6868
# (print (list:drop cool-stuff 4)) # [5 6 7 8 9]
6969
# =end
70-
# @author https://github.com/rstefanic
71-
(let list:drop (fun (_L _n) {
72-
(mut _index (math:max 0 _n))
73-
(mut _output [])
74-
(while (< _index (len _L)) {
75-
(set _output (append _output (@ _L _index)))
76-
(set _index (+ 1 _index))
77-
})
78-
_output
79-
}))
70+
# @author https://github.com/rstefanic, https://github.com/SuperFola
71+
(let list:drop (fun (_L _n)
72+
(if (< _n (/ (len _L) 2))
73+
(if (> _n 0)
74+
(list:drop (tail _L) (- _n 1))
75+
_L)
76+
{
77+
(mut _index (math:max 0 _n))
78+
(mut _output [])
79+
(while (< _index (len _L)) {
80+
(set _output (append _output (@ _L _index)))
81+
(set _index (+ 1 _index))
82+
})
83+
_output
84+
})))
8085

8186
# @brief Drop the first elements of a list, while they match a given predicate
8287
# @param _L the list to work on

0 commit comments

Comments
 (0)