Branch the dropLast functionality from dropAt, inserting an index gathered by subtracting 1 from the list length

This commit is contained in:
Chelsea Troy 2021-10-23 00:17:08 -05:00
parent 49a832d757
commit fd83c3b749
No known key found for this signature in database
GPG key ID: A631885A970636C2
4 changed files with 44 additions and 2 deletions

View file

@ -34,6 +34,7 @@ interface List
sortWith, sortWith,
drop, drop,
dropAt, dropAt,
dropLast,
swap swap
] ]
imports [] imports []
@ -439,6 +440,9 @@ drop : List elem, Nat -> List elem
## To replace the element at a given index, instead of dropping it, see [List.set]. ## To replace the element at a given index, instead of dropping it, see [List.set].
dropAt : List elem, Nat -> List elem dropAt : List elem, Nat -> List elem
## Drops the last element in a List.
dropLast : List elem -> List elem
## Adds a new element to the end of the list. ## Adds a new element to the end of the list.
## ##
## >>> List.append [ "a", "b" ] "c" ## >>> List.append [ "a", "b" ] "c"

View file

@ -177,6 +177,7 @@ pub const LIST_APPEND: &str = "roc_builtins.list.append";
pub const LIST_PREPEND: &str = "roc_builtins.list.prepend"; pub const LIST_PREPEND: &str = "roc_builtins.list.prepend";
pub const LIST_DROP: &str = "roc_builtins.list.drop"; pub const LIST_DROP: &str = "roc_builtins.list.drop";
pub const LIST_DROP_AT: &str = "roc_builtins.list.drop_at"; pub const LIST_DROP_AT: &str = "roc_builtins.list.drop_at";
pub const LIST_DROP_LAST: &str = "roc_builtins.list.drop_last";
pub const LIST_SWAP: &str = "roc_builtins.list.swap"; pub const LIST_SWAP: &str = "roc_builtins.list.swap";
pub const LIST_SINGLE: &str = "roc_builtins.list.single"; pub const LIST_SINGLE: &str = "roc_builtins.list.single";
pub const LIST_JOIN: &str = "roc_builtins.list.join"; pub const LIST_JOIN: &str = "roc_builtins.list.join";

View file

@ -2004,6 +2004,45 @@ fn list_drop_at(symbol: Symbol, var_store: &mut VarStore) -> Def {
) )
} }
/// List.dropLast: List elem -> List elem
fn list_drop_last(symbol: Symbol, var_store: &mut VarStore) -> Def {
let list_var = var_store.fresh();
let body = RunLowLevel {
op: LowLevel::ListDropAt,
args: vec![
(list_var, Var(Symbol::ARG_1)),
(index_var,
// Num.sub (List.len list) 1
RunLowLevel {
op: LowLevel::NumSubWrap,
args: vec![
(
arg_var,
// List.len list
RunLowLevel {
op: LowLevel::ListLen,
args: vec![(list_var, Var(Symbol::ARG_1))],
ret_var: len_var,
},
),
(arg_var, int(num_var, num_precision_var, 1)),
],
ret_var: len_var,
},
),
],
ret_var: list_var,
};
defn(
symbol,
vec![(list_var, Symbol::ARG_1), (index_var, Symbol::ARG_2)],
var_store,
body,
list_var,
)
}
/// List.append : List elem, elem -> List elem /// List.append : List elem, elem -> List elem
fn list_append(symbol: Symbol, var_store: &mut VarStore) -> Def { fn list_append(symbol: Symbol, var_store: &mut VarStore) -> Def {
let list_var = var_store.fresh(); let list_var = var_store.fresh();

View file

@ -42,7 +42,6 @@ pub enum LowLevel {
ListSortWith, ListSortWith,
ListDrop, ListDrop,
ListDropAt, ListDropAt,
ListDropLast,
ListSwap, ListSwap,
DictSize, DictSize,
DictEmpty, DictEmpty,
@ -130,7 +129,6 @@ macro_rules! first_order {
| ListSet | ListSet
| ListDrop | ListDrop
| ListDropAt | ListDropAt
| ListDropLast
| ListSingle | ListSingle
| ListRepeat | ListRepeat
| ListReverse | ListReverse