Skip to content

Commit

Permalink
Try not reversing
Browse files Browse the repository at this point in the history
  • Loading branch information
jneem committed Oct 10, 2024
1 parent 2a40f16 commit 4c6bf05
Show file tree
Hide file tree
Showing 2,321 changed files with 420 additions and 59,413 deletions.
11 changes: 0 additions & 11 deletions .nickel/doc/main.md

This file was deleted.

5 changes: 0 additions & 5 deletions .nickel/doc/test.md

This file was deleted.

26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
members = [
"core",
"cli",
"funcarray",
"vector",
"lsp/nls",
"lsp/lsp-harness",
"utils",
Expand All @@ -23,7 +23,7 @@ readme = "README.md"

[workspace.dependencies]
nickel-lang-core = { version = "0.9.0", path = "./core", default-features = false }
nickel-lang-funcarray = { version = "0.1.0", path = "./funcarray" }
nickel-lang-vector = { version = "0.1.0", path = "./vector" }
nickel-lang-utils = { version = "0.1.0", path = "./utils" }
lsp-harness = { version = "0.1.0", path = "./lsp/lsp-harness" }

Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ codespan.workspace = true
codespan-reporting.workspace = true
cxx = { workspace = true, optional = true }
logos.workspace = true
nickel-lang-funcarray.workspace = true
nickel-lang-vector.workspace = true
smallvec.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
Expand Down
7 changes: 2 additions & 5 deletions core/benches/arrays.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#![cfg_attr(feature = "benchmark-ci", allow(unused_imports))]

use criterion::criterion_main;
use nickel_lang_core::term::{
array::{Array, ArrayAttrs},
Number, RichTerm, Term,
};
use nickel_lang_core::term::{array::ArrayAttrs, Number, RichTerm, Term};
use nickel_lang_utils::{bench::criterion_config, bench::EvalMode, ncl_bench_group};
use pretty::{BoxAllocator, DocBuilder, Pretty};

Expand All @@ -24,7 +21,7 @@ fn ncl_random_array(len: usize) -> String {
}

let xs = RichTerm::from(Term::Array(
Array::collect(numbers.into_iter()),
numbers.into_iter().collect(),
ArrayAttrs::default(),
));
let doc: DocBuilder<_, ()> = xs.pretty(&BoxAllocator);
Expand Down
4 changes: 1 addition & 3 deletions core/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::parser::{lexer::Lexer, ErrorTolerantParser};
use crate::position::TermPos;
use crate::program::FieldPath;
use crate::stdlib::{self as nickel_stdlib, StdlibModule};
use crate::term::array::Array;
use crate::term::record::{Field, RecordData};
use crate::term::{RichTerm, SharedTerm, Term};
use crate::transform::import_resolution;
Expand Down Expand Up @@ -606,8 +605,7 @@ impl Cache {
} else {
Ok((
attach_pos(
Term::Array(Array::collect(terms.into_iter()), Default::default())
.into(),
Term::Array(terms.into_iter().collect(), Default::default()).into(),
),
ParseErrors::default(),
))
Expand Down
29 changes: 10 additions & 19 deletions core/src/eval/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2332,7 +2332,7 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {
BinaryOp::ArrayConcat => match_sharedterm!(match (t1) {
Term::Array(ts1, attrs1) => match_sharedterm!(match (t2) {
Term::Array(ts2, attrs2) => {
let mut ts2 = ts2;
let mut ts1 = ts1;
// NOTE: the [eval_closure] function in [eval] should've made sure
// that the array is closurized. We leave a debug_assert! here just
// in case something goes wrong in the future. If the assert failed,
Expand Down Expand Up @@ -2412,25 +2412,16 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {
let ctrs_left_empty = ctrs_left_dedup.is_empty();

let arr = if ctrs_right_empty && ctrs_left_empty {
ts2.prepend(ts1);
ts2
} else if ctrs_right_empty {
let ts1_vec: Vec<_> = ts1
.into_iter()
.map(|t| {
RuntimeContract::apply_all(
t,
ctrs_left_dedup.iter().cloned(),
pos1,
)
ts1.extend(ts2);
ts1
} else if ctrs_left_empty {
ts1.extend(ts2.into_iter().map(|t| {
RuntimeContract::apply_all(t, ctrs_right_dedup.clone(), pos1)
.closurize(&mut self.cache, env1.clone())
})
.collect();

ts2.prepend_iter(ts1_vec.into_iter());
ts2
}));
ts1
} else {
let mut ts: Vec<RichTerm> = Vec::with_capacity(ts1.len() + ts2.len());
let mut ts = Array::default();

ts.extend(ts1.into_iter().map(|t| {
RuntimeContract::apply_all(t, ctrs_left_dedup.iter().cloned(), pos1)
Expand All @@ -2442,7 +2433,7 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {
.closurize(&mut self.cache, env2.clone())
}));

Array::collect(ts.into_iter())
ts
};

let attrs = ArrayAttrs {
Expand Down
3 changes: 1 addition & 2 deletions core/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use super::utils::{build_record, FieldPathElem};
use crate::error::ParseError;
use crate::identifier::LocIdent;
use crate::parser::{error::ParseError as InternalParseError, ErrorTolerantParser};
use crate::term::array::Array;
use crate::term::Number;
use crate::term::Term::*;
use crate::term::{make as mk_term, Term};
Expand Down Expand Up @@ -54,7 +53,7 @@ fn mk_symbolic_single_chunk(prefix: &str, s: &str) -> RichTerm {
(
FieldPathElem::Ident("fragments".into()),
Field::from(RichTerm::from(Array(
Array::collect(std::iter::once(mk_single_chunk(s))),
std::iter::once(mk_single_chunk(s)).collect(),
Default::default(),
))),
),
Expand Down
14 changes: 2 additions & 12 deletions core/src/term/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nickel_lang_funcarray::FunctionalArray;
use nickel_lang_vector::Slice;

use super::*;

Expand Down Expand Up @@ -40,14 +40,4 @@ impl ArrayAttrs {
}
}

pub type Array = FunctionalArray<RichTerm, 32>;

// TODO: one common use of this collect function is `arr.into_iter().map(|_| ...).collect()`.
// Maybe it's worth having an optimized `map_in_place` function, or an `iter_mut`.
impl FromIterator<RichTerm> for Array {
fn from_iter<T: IntoIterator<Item = RichTerm>>(iter: T) -> Self {
// This needs an extra allocation, because FunctionalArray only supports non-allocating construction from reversed iterators.
let items = iter.into_iter().collect::<Vec<_>>();
Array::collect(items.into_iter())
}
}
pub type Array = Slice<RichTerm, 32>;
4 changes: 1 addition & 3 deletions core/src/term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2737,9 +2737,7 @@ pub mod make {
};
( $( $terms:expr ),* ) => {
{
let ts = $crate::term::array::Array::collect(
[$( $crate::term::RichTerm::from($terms) ),*].into_iter()
);
let ts = [$( $crate::term::RichTerm::from($terms) ),*].into_iter().collect();
$crate::term::RichTerm::from(Term::Array(ts, ArrayAttrs::default()))
}
};
Expand Down
14 changes: 7 additions & 7 deletions core/stdlib/std.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@
# => [ 3, 2, 1 ]
```
"%
= fun l => fold_left (fun acc e => [e] @ acc) [] l,
= fun l => fold_right (fun e acc => acc @ [e]) [] l,

filter
: forall a. (a -> Bool) -> Array a -> Array a
Expand All @@ -439,7 +439,7 @@
# => [ 3, 2, 1 ]
```
"%
= fun pred l => fold_right (fun x acc => if pred x then [x] @ acc else acc) [] l,
= fun pred l => fold_left (fun acc x => if pred x then acc @ [x] else acc) [] l,

flatten
: forall a. Array (Array a) -> Array a
Expand All @@ -453,7 +453,7 @@
# => [1, 2, 3, 4]
```
"%
= fun l => fold_right (fun l acc => l @ acc) [] l,
= fun l => fold_left (fun acc l => acc @ l) [] l,

all
: forall a. (a -> Bool) -> Array a -> Bool
Expand Down Expand Up @@ -532,13 +532,13 @@
```
"%
= fun pred l =>
let aux = fun x acc =>
let aux = fun acc x =>
if (pred x) then
{ right = [x] @ acc.right, wrong = acc.wrong }
{ right = acc.right @ [x], wrong = acc.wrong }
else
{ right = acc.right, wrong = [x] @ acc.wrong }
{ right = acc.right, wrong = acc.wrong @ [x] }
in
fold_right aux { right = [], wrong = [] } l,
fold_left aux { right = [], wrong = [] } l,

generate
: forall a. (Number -> a) -> Number -> Array a
Expand Down
68 changes: 0 additions & 68 deletions funcarray/benches/array.rs

This file was deleted.

Loading

0 comments on commit 4c6bf05

Please sign in to comment.