diff --git a/src/build/roc/Builtin.roc b/src/build/roc/Builtin.roc index a6fc2bc9b4..404800cf6e 100644 --- a/src/build/roc/Builtin.roc +++ b/src/build/roc/Builtin.roc @@ -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}) } } diff --git a/src/eval/interpreter.zig b/src/eval/interpreter.zig index 3110e1b81a..61cf626daf 100644 --- a/src/eval/interpreter.zig +++ b/src/eval/interpreter.zig @@ -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; diff --git a/test/snapshots/repl/list_drop_first.md b/test/snapshots/repl/list_drop_first.md new file mode 100644 index 0000000000..9ff88b9ae2 --- /dev/null +++ b/test/snapshots/repl/list_drop_first.md @@ -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 diff --git a/test/snapshots/repl/list_drop_first_empty.md b/test/snapshots/repl/list_drop_first_empty.md new file mode 100644 index 0000000000..64f3db1906 --- /dev/null +++ b/test/snapshots/repl/list_drop_first_empty.md @@ -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 diff --git a/test/snapshots/repl/list_drop_last.md b/test/snapshots/repl/list_drop_last.md new file mode 100644 index 0000000000..52ec401893 --- /dev/null +++ b/test/snapshots/repl/list_drop_last.md @@ -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 diff --git a/test/snapshots/repl/list_drop_last_empty.md b/test/snapshots/repl/list_drop_last_empty.md new file mode 100644 index 0000000000..89cef0fac6 --- /dev/null +++ b/test/snapshots/repl/list_drop_last_empty.md @@ -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 diff --git a/test/snapshots/repl/list_sublist_basic.md b/test/snapshots/repl/list_sublist_basic.md new file mode 100644 index 0000000000..57364ee269 --- /dev/null +++ b/test/snapshots/repl/list_sublist_basic.md @@ -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 diff --git a/test/snapshots/repl/list_sublist_empty.md b/test/snapshots/repl/list_sublist_empty.md new file mode 100644 index 0000000000..f87cb720ab --- /dev/null +++ b/test/snapshots/repl/list_sublist_empty.md @@ -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 diff --git a/test/snapshots/repl/list_take_first.md b/test/snapshots/repl/list_take_first.md new file mode 100644 index 0000000000..bcc4666c3c --- /dev/null +++ b/test/snapshots/repl/list_take_first.md @@ -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 diff --git a/test/snapshots/repl/list_take_first_all.md b/test/snapshots/repl/list_take_first_all.md new file mode 100644 index 0000000000..24dd22821a --- /dev/null +++ b/test/snapshots/repl/list_take_first_all.md @@ -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 diff --git a/test/snapshots/repl/list_take_first_empty.md b/test/snapshots/repl/list_take_first_empty.md new file mode 100644 index 0000000000..0f5e987861 --- /dev/null +++ b/test/snapshots/repl/list_take_first_empty.md @@ -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 diff --git a/test/snapshots/repl/list_take_last.md b/test/snapshots/repl/list_take_last.md new file mode 100644 index 0000000000..56cbab46a6 --- /dev/null +++ b/test/snapshots/repl/list_take_last.md @@ -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 diff --git a/test/snapshots/repl/list_take_last_all.md b/test/snapshots/repl/list_take_last_all.md new file mode 100644 index 0000000000..dc27151034 --- /dev/null +++ b/test/snapshots/repl/list_take_last_all.md @@ -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 diff --git a/test/snapshots/repl/list_take_last_empty.md b/test/snapshots/repl/list_take_last_empty.md new file mode 100644 index 0000000000..0f89a16ab2 --- /dev/null +++ b/test/snapshots/repl/list_take_last_empty.md @@ -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