Skip to content

Commit 7226e60

Browse files
committed
implement list skip
1 parent 594ba21 commit 7226e60

File tree

1 file changed

+134
-0
lines changed
  • eo-runtime/src/main/eo/org/eolang/structs

1 file changed

+134
-0
lines changed

eo-runtime/src/main/eo/org/eolang/structs/list.eo

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,33 @@
266266
index > idx!
267267
origin.length.minus idx > start!
268268

269+
# Skip the first `index` elements and return the rest of the list.
270+
[index] > skip
271+
if. > @
272+
0.gt idx
273+
error
274+
sprintf
275+
"Can't skip negative number of elements: %d"
276+
* idx
277+
if.
278+
idx.eq 0
279+
^
280+
if.
281+
(number idx).gte origin.length
282+
list *
283+
list
284+
rec-skip origin
285+
index > idx!
286+
287+
[tup] > rec-skip
288+
if. > @
289+
idx.gte tup.length
290+
*
291+
if.
292+
(origin.length.minus tup.length).eq idx
293+
tup
294+
rec-skip tup.tail
295+
269296
# Tests that a list with elements is not empty.
270297
[] +> tests-list-should-not-be-empty
271298
not. > @
@@ -772,3 +799,110 @@
772799
* 1 2 3 4 5
773800
10
774801
* 1 2 3 4 5
802+
803+
# Tests that skipping 2 elements from a list of strings works correctly.
804+
[] +> tests-simple-skip
805+
eq. > @
806+
skip.
807+
list
808+
* "banana" "apple" "orange"
809+
2
810+
* "orange"
811+
812+
# Tests that skipping zero elements returns the entire list.
813+
[] +> tests-skip-zero
814+
eq. > @
815+
skip.
816+
list
817+
* 1 2 3 4 5
818+
0
819+
* 1 2 3 4 5
820+
821+
# Tests that skipping negative number produces an error.
822+
[] +> tests-skip-negative-error
823+
try. > @
824+
skip.
825+
list
826+
* "foo" "bar" "baz"
827+
-1
828+
[e]
829+
true > @
830+
false
831+
832+
# Tests that skipping all elements returns an empty list.
833+
[] +> tests-skip-all
834+
is-empty. > @
835+
skip.
836+
list
837+
* 1 2 3
838+
3
839+
840+
# Tests that skipping more than length returns an empty list.
841+
[] +> tests-skip-more-than-length
842+
is-empty. > @
843+
skip.
844+
list
845+
* "a" "b"
846+
5
847+
848+
# Tests that skipping from an empty list returns an empty list.
849+
[] +> tests-skip-empty-list
850+
is-empty. > @
851+
skip.
852+
list *
853+
1
854+
855+
# Tests that skipping works with complex data types.
856+
[] +> tests-skip-complex-types
857+
eq. > @
858+
skip.
859+
list
860+
* "text" 3.14 00-FF (* 1 2)
861+
1
862+
* 3.14 00-FF (* 1 2)
863+
864+
# Tests that skipping one element from a list works correctly.
865+
[] +> tests-skip-one
866+
eq. > @
867+
skip.
868+
list
869+
* 10 20 30 40
870+
1
871+
* 20 30 40
872+
873+
# Tests that skipping from a single-element list returns empty.
874+
[] +> tests-skip-single-element
875+
is-empty. > @
876+
skip.
877+
list
878+
* "only"
879+
1
880+
881+
# Tests that skipping works with nested lists.
882+
[] +> tests-skip-nested-lists
883+
eq. > @
884+
skip.
885+
list
886+
* (* 1 2) (* 3 4) (* 5 6)
887+
2
888+
* (* 5 6)
889+
890+
# Tests that skipping with large negative number produces an error.
891+
[] +> tests-skip-large-negative-error
892+
try. > @
893+
skip.
894+
list
895+
* "a" "b" "c"
896+
-100
897+
[e]
898+
true > @
899+
false
900+
901+
# Tests that skipping exactly length minus one returns last element.
902+
[] +> tests-skip-to-last
903+
eq. > @
904+
skip.
905+
list
906+
* "first" "second" "third" "fourth" "last"
907+
4
908+
* "last"

0 commit comments

Comments
 (0)