mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-22 00:09:33 +03:00
Add dropLast to tests and parser
This commit is contained in:
parent
51de420ee7
commit
49a832d757
@ -934,6 +934,13 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
|||||||
Box::new(list_type(flex(TVAR1))),
|
Box::new(list_type(flex(TVAR1))),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// dropLast : List elem -> List elem
|
||||||
|
add_top_level_function_type!(
|
||||||
|
Symbol::LIST_DROP_LAST,
|
||||||
|
vec![list_type(flex(TVAR1))],
|
||||||
|
Box::new(list_type(flex(TVAR1))),
|
||||||
|
);
|
||||||
|
|
||||||
// swap : List elem, Nat, Nat -> List elem
|
// swap : List elem, Nat, Nat -> List elem
|
||||||
add_top_level_function_type!(
|
add_top_level_function_type!(
|
||||||
Symbol::LIST_SWAP,
|
Symbol::LIST_SWAP,
|
||||||
|
@ -341,6 +341,28 @@ pub fn list_drop_at<'a, 'ctx, 'env>(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// List.dropLast : List elem -> List elem
|
||||||
|
pub fn list_drop_last<'a, 'ctx, 'env>(
|
||||||
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
|
layout_ids: &mut LayoutIds<'a>,
|
||||||
|
original_wrapper: StructValue<'ctx>,
|
||||||
|
count: IntValue<'ctx>,
|
||||||
|
element_layout: &Layout<'a>,
|
||||||
|
) -> BasicValueEnum<'ctx> {
|
||||||
|
let dec_element_fn = build_dec_wrapper(env, layout_ids, element_layout);
|
||||||
|
call_bitcode_fn_returns_list(
|
||||||
|
env,
|
||||||
|
&[
|
||||||
|
pass_list_cc(env, original_wrapper.into()),
|
||||||
|
env.alignment_intvalue(element_layout),
|
||||||
|
layout_width(env, element_layout),
|
||||||
|
count.into(),
|
||||||
|
dec_element_fn.as_global_value().as_pointer_value().into(),
|
||||||
|
],
|
||||||
|
bitcode::LIST_DROP_LAST,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// List.set : List elem, Nat, elem -> List elem
|
/// List.set : List elem, Nat, elem -> List elem
|
||||||
pub fn list_set<'a, 'ctx, 'env>(
|
pub fn list_set<'a, 'ctx, 'env>(
|
||||||
env: &Env<'a, 'ctx, 'env>,
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
|
@ -42,6 +42,7 @@ pub enum LowLevel {
|
|||||||
ListSortWith,
|
ListSortWith,
|
||||||
ListDrop,
|
ListDrop,
|
||||||
ListDropAt,
|
ListDropAt,
|
||||||
|
ListDropLast,
|
||||||
ListSwap,
|
ListSwap,
|
||||||
DictSize,
|
DictSize,
|
||||||
DictEmpty,
|
DictEmpty,
|
||||||
@ -129,6 +130,7 @@ macro_rules! first_order {
|
|||||||
| ListSet
|
| ListSet
|
||||||
| ListDrop
|
| ListDrop
|
||||||
| ListDropAt
|
| ListDropAt
|
||||||
|
| ListDropLast
|
||||||
| ListSingle
|
| ListSingle
|
||||||
| ListRepeat
|
| ListRepeat
|
||||||
| ListReverse
|
| ListReverse
|
||||||
|
@ -1015,6 +1015,7 @@ define_builtins! {
|
|||||||
32 LIST_DROP: "drop"
|
32 LIST_DROP: "drop"
|
||||||
33 LIST_SWAP: "swap"
|
33 LIST_SWAP: "swap"
|
||||||
34 LIST_DROP_AT: "dropAt"
|
34 LIST_DROP_AT: "dropAt"
|
||||||
|
35 LIST_DROP_LAST: "dropLast"
|
||||||
}
|
}
|
||||||
5 RESULT: "Result" => {
|
5 RESULT: "Result" => {
|
||||||
0 RESULT_RESULT: "Result" imported // the Result.Result type alias
|
0 RESULT_RESULT: "Result" imported // the Result.Result type alias
|
||||||
|
@ -947,6 +947,7 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
|
|||||||
ListAppend => arena.alloc_slice_copy(&[owned, owned]),
|
ListAppend => arena.alloc_slice_copy(&[owned, owned]),
|
||||||
ListDrop => arena.alloc_slice_copy(&[owned, irrelevant]),
|
ListDrop => arena.alloc_slice_copy(&[owned, irrelevant]),
|
||||||
ListDropAt => arena.alloc_slice_copy(&[owned, irrelevant]),
|
ListDropAt => arena.alloc_slice_copy(&[owned, irrelevant]),
|
||||||
|
ListDropLast => arena.alloc_slice_copy(&[owned]),
|
||||||
ListSwap => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
|
ListSwap => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
|
||||||
|
|
||||||
Eq | NotEq => arena.alloc_slice_copy(&[borrowed, borrowed]),
|
Eq | NotEq => arena.alloc_slice_copy(&[borrowed, borrowed]),
|
||||||
|
@ -3733,6 +3733,18 @@ mod solve_expr {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_drop_at() {
|
||||||
|
infer_eq_without_problem(
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
List.dropLast
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
"List a -> List a",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn function_that_captures_nothing_is_not_captured() {
|
fn function_that_captures_nothing_is_not_captured() {
|
||||||
// we should make sure that a function that doesn't capture anything it not itself captured
|
// we should make sure that a function that doesn't capture anything it not itself captured
|
||||||
|
@ -235,6 +235,38 @@ fn list_drop_at_mutable() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_drop_last() {
|
||||||
|
assert_evals_to!(
|
||||||
|
"List.dropLast [1, 2, 3]",
|
||||||
|
RocList::from_slice(&[1, 2]),
|
||||||
|
RocList<i64>
|
||||||
|
);
|
||||||
|
assert_evals_to!("List.dropLast []", RocList::from_slice(&[]), RocList<i64>);
|
||||||
|
assert_evals_to!("List.dropLast [0]", RocList::from_slice(&[]), RocList<i64>);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_drop_last_mutable() {
|
||||||
|
assert_evals_to!(
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
list : List I64
|
||||||
|
list = [ if True then 4 else 4, 5, 6 ]
|
||||||
|
|
||||||
|
{ newList: List.dropLast list, original: list }
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
(
|
||||||
|
// new_list
|
||||||
|
RocList::from_slice(&[4, 5]),
|
||||||
|
// original
|
||||||
|
RocList::from_slice(&[4, 5, 6]),
|
||||||
|
),
|
||||||
|
(RocList<i64>, RocList<i64>,)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn list_swap() {
|
fn list_swap() {
|
||||||
assert_evals_to!("List.swap [] 0 1", RocList::from_slice(&[]), RocList<i64>);
|
assert_evals_to!("List.swap [] 0 1", RocList::from_slice(&[]), RocList<i64>);
|
||||||
|
Loading…
Reference in New Issue
Block a user