Skip to content

Commit

Permalink
use slices for creating ternary-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Nov 8, 2021
1 parent 2e9d10a commit 5ef52b9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
24 changes: 12 additions & 12 deletions src/builtins/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,15 @@ pub fn foldl(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Calcit, Cal
},
) => {
for x in xs {
let values = TernaryTreeList::from(&vec![ret, x.to_owned()]);
let values = TernaryTreeList::from(&[ret, x.to_owned()]);
ret = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?;
}
Ok(ret)
}
(Calcit::List(xs), Calcit::Proc(proc)) => {
for x in xs {
// println!("foldl args, {} {}", ret, x.to_owned());
ret = builtins::handle_proc(proc, &TernaryTreeList::from(&vec![ret, x.to_owned()]), call_stack)?;
ret = builtins::handle_proc(proc, &TernaryTreeList::from(&[ret, x.to_owned()]), call_stack)?;
}
Ok(ret)
}
Expand All @@ -213,15 +213,15 @@ pub fn foldl(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Calcit, Cal
},
) => {
for x in xs {
let values = TernaryTreeList::from(&vec![ret, x.to_owned()]);
let values = TernaryTreeList::from(&[ret, x.to_owned()]);
ret = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?;
}
Ok(ret)
}
(Calcit::Set(xs), Calcit::Proc(proc)) => {
for x in xs {
// println!("foldl args, {} {}", ret, x.to_owned());
ret = builtins::handle_proc(proc, &TernaryTreeList::from(&vec![ret, x.to_owned()]), call_stack)?;
ret = builtins::handle_proc(proc, &TernaryTreeList::from(&[ret, x.to_owned()]), call_stack)?;
}
Ok(ret)
}
Expand All @@ -233,7 +233,7 @@ pub fn foldl(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Calcit, Cal
},
) => {
for (k, x) in xs {
let values = TernaryTreeList::from(&vec![ret, Calcit::List(TernaryTreeList::from(&vec![k.to_owned(), x.to_owned()]))]);
let values = TernaryTreeList::from(&[ret, Calcit::List(TernaryTreeList::from(&[k.to_owned(), x.to_owned()]))]);
ret = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?;
}
Ok(ret)
Expand All @@ -243,7 +243,7 @@ pub fn foldl(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Calcit, Cal
// println!("foldl args, {} {}", ret, x.to_owned());
ret = builtins::handle_proc(
proc,
&TernaryTreeList::from(&vec![ret, Calcit::List(TernaryTreeList::from(&vec![k.to_owned(), x.to_owned()]))]),
&TernaryTreeList::from(&[ret, Calcit::List(TernaryTreeList::from(&[k.to_owned(), x.to_owned()]))]),
call_stack,
)?;
}
Expand Down Expand Up @@ -279,7 +279,7 @@ pub fn foldl_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Ca
) => {
let mut state = acc.to_owned();
for x in xs {
let values = TernaryTreeList::from(&vec![state, x.to_owned()]);
let values = TernaryTreeList::from(&[state, x.to_owned()]);
let pair = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?;
match pair {
Calcit::Tuple(x0, x1) => match &*x0 {
Expand Down Expand Up @@ -316,7 +316,7 @@ pub fn foldl_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Ca
) => {
let mut state = acc.to_owned();
for x in xs {
let values = TernaryTreeList::from(&vec![state, x.to_owned()]);
let values = TernaryTreeList::from(&[state, x.to_owned()]);
let pair = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?;
match pair {
Calcit::Tuple(x0, x1) => match &*x0 {
Expand Down Expand Up @@ -353,7 +353,7 @@ pub fn foldl_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Ca
) => {
let mut state = acc.to_owned();
for (k, x) in xs {
let values = TernaryTreeList::from(&vec![state, Calcit::List(TernaryTreeList::from(&vec![k.to_owned(), x.to_owned()]))]);
let values = TernaryTreeList::from(&[state, Calcit::List(TernaryTreeList::from(&[k.to_owned(), x.to_owned()]))]);
let pair = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?;
match pair {
Calcit::Tuple(x0, x1) => match &*x0 {
Expand Down Expand Up @@ -413,7 +413,7 @@ pub fn foldr_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Ca
let size = xs.len();
for i in 0..size {
let x = xs[size - 1 - i].to_owned();
let values = TernaryTreeList::from(&vec![state, x]);
let values = TernaryTreeList::from(&[state, x]);
let pair = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?;
match pair {
Calcit::Tuple(x0, x1) => match &*x0 {
Expand Down Expand Up @@ -467,7 +467,7 @@ pub fn sort(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Calcit, Calc
) => {
let mut xs2: Vec<&Calcit> = xs.into_iter().collect::<Vec<&Calcit>>();
xs2.sort_by(|a, b| -> Ordering {
let values = TernaryTreeList::from(&vec![a.to_owned().to_owned(), b.to_owned().to_owned()]);
let values = TernaryTreeList::from(&[a.to_owned().to_owned(), b.to_owned().to_owned()]);
let v = runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack);
match v {
Ok(Calcit::Number(x)) if x < 0.0 => Ordering::Less,
Expand All @@ -493,7 +493,7 @@ pub fn sort(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Calcit, Calc
(Calcit::List(xs), Calcit::Proc(proc)) => {
let mut xs2: Vec<&Calcit> = xs.into_iter().collect::<Vec<&Calcit>>();
xs2.sort_by(|a, b| -> Ordering {
let values = TernaryTreeList::from(&vec![a.to_owned().to_owned(), b.to_owned().to_owned()]);
let values = TernaryTreeList::from(&[a.to_owned().to_owned(), b.to_owned().to_owned()]);
let v = builtins::handle_proc(proc, &values, call_stack);
match v {
Ok(Calcit::Number(x)) if x < 0.0 => Ordering::Less,
Expand Down
4 changes: 2 additions & 2 deletions src/builtins/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn to_list(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
Some(Calcit::Map(m)) => {
let mut ys: TernaryTreeList<Calcit> = TernaryTreeList::Empty;
for (k, v) in m {
let zs: TernaryTreeList<Calcit> = TernaryTreeList::from(&vec![k.to_owned(), v.to_owned()]);
let zs: TernaryTreeList<Calcit> = TernaryTreeList::from(&[k.to_owned(), v.to_owned()]);
ys = ys.push(Calcit::List(zs));
}
Ok(Calcit::List(ys))
Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn first(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
match xs.get(0) {
Some(Calcit::Map(ys)) => match ys.iter().next() {
// TODO order may not be stable enough
Some((k, v)) => Ok(Calcit::List(TernaryTreeList::from(&vec![k.to_owned(), v.to_owned()]))),
Some((k, v)) => Ok(Calcit::List(TernaryTreeList::from(&[k.to_owned(), v.to_owned()]))),
None => Ok(Calcit::Nil),
},
Some(a) => CalcitErr::err_str(format!("map:first expected a map, got: {}", a)),
Expand Down
2 changes: 1 addition & 1 deletion src/builtins/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn modify_ref(path: Arc<str>, v: Calcit, call_stack: &CallStackList) -> Result<(
Calcit::Fn {
def_ns, scope, args, body, ..
} => {
let values = TernaryTreeList::from(&vec![v.to_owned(), prev.to_owned()]);
let values = TernaryTreeList::from(&[v.to_owned(), prev.to_owned()]);
runner::run_fn(&values, scope, args, body, def_ns.to_owned(), call_stack)?;
}
a => {
Expand Down
2 changes: 1 addition & 1 deletion src/runner/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ fn process_list_call(
match (head_form.to_owned(), head_evaled) {
(Calcit::Keyword(..), _) => {
if args.len() == 1 {
let code = Calcit::List(TernaryTreeList::from(&vec![
let code = Calcit::List(TernaryTreeList::from(&[
Calcit::Symbol {
sym: String::from("get").into(),
ns: String::from(primes::CORE_NS).into(),
Expand Down

0 comments on commit 5ef52b9

Please sign in to comment.