adding List.sortAsc builtin

This commit is contained in:
Eric Newbury 2022-01-21 15:34:24 -05:00
parent c0ea664141
commit 05c01a81f5
4 changed files with 52 additions and 1 deletions

View File

@ -1302,6 +1302,13 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
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 ]*
{
let not_found = SolvedType::TagUnion(

View File

@ -51,7 +51,7 @@ macro_rules! macro_magic {
/// even those that are relied on transitively!
pub fn builtin_dependencies(symbol: Symbol) -> &'static [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_SUM => &[Symbol::LIST_WALK, Symbol::NUM_ADD],
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_UNTIL => list_walk_until,
LIST_SORT_WITH => list_sort_with,
LIST_SORT_ASC => list_sort_asc,
LIST_ANY => list_any,
LIST_ALL => list_all,
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)
}
/// 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
fn list_any(symbol: Symbol, var_store: &mut VarStore) -> Def {
lowlevel_2(symbol, LowLevel::ListAny, var_store)

View File

@ -1110,6 +1110,7 @@ define_builtins! {
52 LIST_ALL: "all"
53 LIST_DROP_IF: "dropIf"
54 LIST_DROP_IF_PREDICATE: "#dropIfPred"
55 LIST_SORT_ASC: "sortAsc"
}
5 RESULT: "Result" => {
0 RESULT_RESULT: "Result" imported // the Result.Result type alias

View File

@ -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]
#[cfg(any(feature = "gen-llvm"))]
fn list_any() {