mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 06:44:46 +00:00
Merge pull request #2384 from rtfeldman/enewbury/list-sort-asc
Adds List.sortAsc builtin
This commit is contained in:
commit
e7aa65bff0
4 changed files with 52 additions and 1 deletions
|
@ -1302,6 +1302,13 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||||
Box::new(list_type(flex(TVAR1))),
|
Box::new(list_type(flex(TVAR1))),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// sortAsc : List (Num a) -> List (Num a)
|
||||||
|
add_top_level_function_type!(
|
||||||
|
Symbol::LIST_SORT_ASC,
|
||||||
|
vec![list_type(num_type(flex(TVAR1)))],
|
||||||
|
Box::new(list_type(num_type(flex(TVAR1))))
|
||||||
|
);
|
||||||
|
|
||||||
// find : List elem, (elem -> Bool) -> Result elem [ NotFound ]*
|
// find : List elem, (elem -> Bool) -> Result elem [ NotFound ]*
|
||||||
{
|
{
|
||||||
let not_found = SolvedType::TagUnion(
|
let not_found = SolvedType::TagUnion(
|
||||||
|
|
|
@ -51,7 +51,7 @@ macro_rules! macro_magic {
|
||||||
/// even those that are relied on transitively!
|
/// even those that are relied on transitively!
|
||||||
pub fn builtin_dependencies(symbol: Symbol) -> &'static [Symbol] {
|
pub fn builtin_dependencies(symbol: Symbol) -> &'static [Symbol] {
|
||||||
match symbol {
|
match symbol {
|
||||||
// Symbol::LIST_SORT_ASC => &[Symbol::LIST_SORT_WITH, Symbol::NUM_COMPARE],
|
Symbol::LIST_SORT_ASC => &[Symbol::LIST_SORT_WITH, Symbol::NUM_COMPARE],
|
||||||
Symbol::LIST_PRODUCT => &[Symbol::LIST_WALK, Symbol::NUM_MUL],
|
Symbol::LIST_PRODUCT => &[Symbol::LIST_WALK, Symbol::NUM_MUL],
|
||||||
Symbol::LIST_SUM => &[Symbol::LIST_WALK, Symbol::NUM_ADD],
|
Symbol::LIST_SUM => &[Symbol::LIST_WALK, Symbol::NUM_ADD],
|
||||||
Symbol::LIST_JOIN_MAP => &[Symbol::LIST_WALK, Symbol::LIST_CONCAT],
|
Symbol::LIST_JOIN_MAP => &[Symbol::LIST_WALK, Symbol::LIST_CONCAT],
|
||||||
|
@ -141,6 +141,7 @@ pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option<Def>
|
||||||
LIST_WALK_BACKWARDS => list_walk_backwards,
|
LIST_WALK_BACKWARDS => list_walk_backwards,
|
||||||
LIST_WALK_UNTIL => list_walk_until,
|
LIST_WALK_UNTIL => list_walk_until,
|
||||||
LIST_SORT_WITH => list_sort_with,
|
LIST_SORT_WITH => list_sort_with,
|
||||||
|
LIST_SORT_ASC => list_sort_asc,
|
||||||
LIST_ANY => list_any,
|
LIST_ANY => list_any,
|
||||||
LIST_ALL => list_all,
|
LIST_ALL => list_all,
|
||||||
LIST_FIND => list_find,
|
LIST_FIND => list_find,
|
||||||
|
@ -3418,6 +3419,37 @@ fn list_sort_with(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
lowlevel_2(symbol, LowLevel::ListSortWith, var_store)
|
lowlevel_2(symbol, LowLevel::ListSortWith, var_store)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// List.sortAsc : List (Num a) -> List (Num a)
|
||||||
|
fn list_sort_asc(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
|
let list_var = var_store.fresh();
|
||||||
|
let closure_var = var_store.fresh();
|
||||||
|
let ret_var = list_var;
|
||||||
|
|
||||||
|
let function = (
|
||||||
|
var_store.fresh(),
|
||||||
|
Loc::at_zero(Expr::Var(Symbol::LIST_SORT_WITH)),
|
||||||
|
var_store.fresh(),
|
||||||
|
ret_var,
|
||||||
|
);
|
||||||
|
|
||||||
|
let body = Expr::Call(
|
||||||
|
Box::new(function),
|
||||||
|
vec![
|
||||||
|
(list_var, Loc::at_zero(Var(Symbol::ARG_1))),
|
||||||
|
(closure_var, Loc::at_zero(Var(Symbol::NUM_COMPARE))),
|
||||||
|
],
|
||||||
|
CalledVia::Space,
|
||||||
|
);
|
||||||
|
|
||||||
|
defn(
|
||||||
|
symbol,
|
||||||
|
vec![(list_var, Symbol::ARG_1)],
|
||||||
|
var_store,
|
||||||
|
body,
|
||||||
|
ret_var,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// List.any: List elem, (elem -> Bool) -> Bool
|
/// List.any: List elem, (elem -> Bool) -> Bool
|
||||||
fn list_any(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
fn list_any(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
lowlevel_2(symbol, LowLevel::ListAny, var_store)
|
lowlevel_2(symbol, LowLevel::ListAny, var_store)
|
||||||
|
|
|
@ -1110,6 +1110,7 @@ define_builtins! {
|
||||||
52 LIST_ALL: "all"
|
52 LIST_ALL: "all"
|
||||||
53 LIST_DROP_IF: "dropIf"
|
53 LIST_DROP_IF: "dropIf"
|
||||||
54 LIST_DROP_IF_PREDICATE: "#dropIfPred"
|
54 LIST_DROP_IF_PREDICATE: "#dropIfPred"
|
||||||
|
55 LIST_SORT_ASC: "sortAsc"
|
||||||
}
|
}
|
||||||
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
|
||||||
|
|
|
@ -2421,6 +2421,17 @@ fn list_sort_with() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(any(feature = "gen-llvm"))]
|
||||||
|
fn list_sort_asc() {
|
||||||
|
assert_evals_to!("List.sortAsc []", RocList::from_slice(&[]), RocList<i64>);
|
||||||
|
assert_evals_to!(
|
||||||
|
"List.sortAsc [ 4,3,2,1 ]",
|
||||||
|
RocList::from_slice(&[1, 2, 3, 4]),
|
||||||
|
RocList<i64>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm"))]
|
#[cfg(any(feature = "gen-llvm"))]
|
||||||
fn list_any() {
|
fn list_any() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue