mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 15:21:12 +00:00
Branch the dropLast functionality from dropAt, inserting an index gathered by subtracting 1 from the list length
This commit is contained in:
parent
49a832d757
commit
fd83c3b749
4 changed files with 44 additions and 2 deletions
|
@ -34,6 +34,7 @@ interface List
|
|||
sortWith,
|
||||
drop,
|
||||
dropAt,
|
||||
dropLast,
|
||||
swap
|
||||
]
|
||||
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].
|
||||
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.
|
||||
##
|
||||
## >>> List.append [ "a", "b" ] "c"
|
||||
|
|
|
@ -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_DROP: &str = "roc_builtins.list.drop";
|
||||
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_SINGLE: &str = "roc_builtins.list.single";
|
||||
pub const LIST_JOIN: &str = "roc_builtins.list.join";
|
||||
|
|
|
@ -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
|
||||
fn list_append(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||
let list_var = var_store.fresh();
|
||||
|
|
|
@ -42,7 +42,6 @@ pub enum LowLevel {
|
|||
ListSortWith,
|
||||
ListDrop,
|
||||
ListDropAt,
|
||||
ListDropLast,
|
||||
ListSwap,
|
||||
DictSize,
|
||||
DictEmpty,
|
||||
|
@ -130,7 +129,6 @@ macro_rules! first_order {
|
|||
| ListSet
|
||||
| ListDrop
|
||||
| ListDropAt
|
||||
| ListDropLast
|
||||
| ListSingle
|
||||
| ListRepeat
|
||||
| ListReverse
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue