Finishing up implementation for take/drop first/last functions and snapshot testing

This commit is contained in:
Edwin Santos 2025-11-30 14:25:11 -05:00
parent a208eb05a8
commit cff062cca9
14 changed files with 162 additions and 11 deletions

View file

@ -162,31 +162,27 @@ Builtin :: [].{
take_first : List(a), U64 -> List(a)
take_first = |list, n| {
config = {start: 0, len: n}
list.sublist( config)
List.sublist(list, {len: n, start: 0})
}
take_last : List(a), U64 -> List(a)
take_last = |list, n| {
len = List.len(list)
start = if (len < n) 0 else len - n
config = {start: start, len: len}
list.sublist( config)
start = if (len <= n) 0 else len - n
List.sublist(list, {start: start, len: len})
}
drop_first : List(a), U64 -> List(a)
drop_first = |list, n| {
len = List.len(list)
config = {start: n, len: len}
list.sublist( config)
List.sublist(list, {start: n, len: len})
}
drop_last : List(a), U64 -> List(a)
drop_last = |list, n| {
len = List.len(list)
take_len = if (len < n) 0 else len - n
config = {start: 0, len: take_len}
list.sublist( config)
take_len = if (len <= n) 0 else len - n
List.sublist(list, {start: 0, len: take_len})
}
}

View file

@ -2047,7 +2047,6 @@ pub const Interpreter = struct {
const sublist_len_stack = sublist_config.getFieldByIndex(0) catch unreachable;
const sublist_start: u64 = @intCast(sublist_start_stack.asI128());
const sublist_len: u64 = @intCast(sublist_len_stack.asI128());
std.debug.print("\nConfig Record: {{start: {d}, len: {d} }}\n", .{ sublist_start, sublist_len });
// Get element layout from the list layout
const elem_layout_idx = list_arg.layout.data.list;

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.drop_first returns a list omitting the first n elements of the input list
type=repl
~~~
# SOURCE
~~~roc
» List.drop_first([0, 1, 2, 3, 4, 5, 6], 5)
~~~
# OUTPUT
[5, 6]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.drop_first returns an empty list when dropping more elements than in original list
type=repl
~~~
# SOURCE
~~~roc
» List.drop_first([0, 1, 2, 3, 4, 5, 6], 50)
~~~
# OUTPUT
[]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.drop_last returns a list omitting the last n elements of the list
type=repl
~~~
# SOURCE
~~~roc
» List.drop_last([0, 1, 2, 3, 4, 5, 6], 4)
~~~
# OUTPUT
[0, 1, 2]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.drop_last dropping more elements than in list returns an empty list
type=repl
~~~
# SOURCE
~~~roc
» List.drop_last([0, 1, 2, 3, 4, 5, 6], 10)
~~~
# OUTPUT
[]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.sublist returns a continuous subsection of the original list
type=repl
~~~
# SOURCE
~~~roc
» List.sublist([0, 1, 2, 3, 4, 5, 6], {start: 2, len: 3})
~~~
# OUTPUT
[2, 3, 4]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.sublist on an empty list yields an empty list
type=repl
~~~
# SOURCE
~~~roc
» List.sublist([], {start: 2, len: 3})
~~~
# OUTPUT
[]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.take_first returns a list with the first n elements of the input list
type=repl
~~~
# SOURCE
~~~roc
» List.take_first([0, 1, 2, 3, 4, 5, 6], 3)
~~~
# OUTPUT
[0, 1, 2]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.take_first returns entire list when more elements requested than in original list
type=repl
~~~
# SOURCE
~~~roc
» List.take_first([0, 1, 2, 3, 4, 5, 6], 300)
~~~
# OUTPUT
[0, 1, 2, 3, 4, 5, 6]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.take_first 0 elements returns empty list
type=repl
~~~
# SOURCE
~~~roc
» List.take_first([0, 1, 2, 3, 4, 5, 6], 0)
~~~
# OUTPUT
[]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.take_last returns a list of the last n elements of the input list
type=repl
~~~
# SOURCE
~~~roc
» List.take_last([0, 1, 2, 3, 4, 5, 6], 2)
~~~
# OUTPUT
[5, 6]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.take_last returns entire list when requesting more elements than in original list
type=repl
~~~
# SOURCE
~~~roc
» List.take_last([0, 1, 2, 3, 4, 5, 6], 200)
~~~
# OUTPUT
[0, 1, 2, 3, 4, 5, 6]
# PROBLEMS
NIL

View file

@ -0,0 +1,13 @@
# META
~~~ini
description=List.take_last returns an empty list if taking 0 elements
type=repl
~~~
# SOURCE
~~~roc
» List.take_last([0, 1, 2, 3, 4, 5, 6], 0)
~~~
# OUTPUT
[]
# PROBLEMS
NIL