mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Array.prototype.findIndex,flatMap
This commit is contained in:
@@ -12,8 +12,8 @@ struct FindState {}
|
||||
impl ArrayMappingState for FindState {
|
||||
fn process(&mut self, _i: usize, element: &Val, mapped: Val) -> Option<Val> {
|
||||
match mapped.is_truthy() {
|
||||
true => None,
|
||||
false => Some(element.clone()),
|
||||
true => Some(element.clone()),
|
||||
false => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
use super::super::vs_value::{Val, ValTrait};
|
||||
use super::super::native_frame_function::NativeFrameFunction;
|
||||
use super::array_mapping_frame::{ArrayMappingState, ArrayMappingFrame};
|
||||
|
||||
pub static FIND_INDEX: NativeFrameFunction = NativeFrameFunction {
|
||||
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(FindIndexState::default()))),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
struct FindIndexState {}
|
||||
|
||||
impl ArrayMappingState for FindIndexState {
|
||||
fn process(&mut self, i: usize, _element: &Val, mapped: Val) -> Option<Val> {
|
||||
match mapped.is_truthy() {
|
||||
true => Some(Val::Number(i as f64)),
|
||||
false => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Val {
|
||||
Val::Number(-1f64)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::super::vs_value::{Val, ValTrait};
|
||||
use super::super::vs_array::VsArray;
|
||||
use super::super::native_frame_function::NativeFrameFunction;
|
||||
use super::array_mapping_frame::{ArrayMappingState, ArrayMappingFrame};
|
||||
|
||||
pub static FLAT_MAP: NativeFrameFunction = NativeFrameFunction {
|
||||
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(FlatMapState::default()))),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
struct FlatMapState {
|
||||
flat_map_results: Vec<Val>,
|
||||
}
|
||||
|
||||
impl ArrayMappingState for FlatMapState {
|
||||
fn process(&mut self, _i: usize, _element: &Val, mapped: Val) -> Option<Val> {
|
||||
match mapped.as_array_data() {
|
||||
None => self.flat_map_results.push(mapped),
|
||||
Some(array_data) => {
|
||||
for el in &array_data.elements {
|
||||
self.flat_map_results.push(el.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return None;
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Val {
|
||||
let mut flat_map_results = Vec::new();
|
||||
std::mem::swap(&mut self.flat_map_results, &mut flat_map_results);
|
||||
return Val::Array(Rc::new(VsArray::from(flat_map_results)));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
mod array_mapping_frame;
|
||||
|
||||
pub mod array_map;
|
||||
pub mod array_every;
|
||||
pub mod array_some;
|
||||
pub mod array_filter;
|
||||
pub mod array_find;
|
||||
mod array_mapping_frame;
|
||||
pub mod array_find_index;
|
||||
pub mod array_flat_map;
|
||||
|
||||
@@ -16,6 +16,8 @@ use super::array_higher_functions::array_every::EVERY;
|
||||
use super::array_higher_functions::array_some::SOME;
|
||||
use super::array_higher_functions::array_filter::FILTER;
|
||||
use super::array_higher_functions::array_find::FIND;
|
||||
use super::array_higher_functions::array_find_index::FIND_INDEX;
|
||||
use super::array_higher_functions::array_flat_map::FLAT_MAP;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct VsArray {
|
||||
@@ -307,17 +309,6 @@ static FILL: NativeFunction = NativeFunction {
|
||||
}
|
||||
};
|
||||
|
||||
static FIND_INDEX: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
Val::Array(_array_data) => {
|
||||
std::panic!("Not implemented: FIND_INDEX");
|
||||
},
|
||||
_ => std::panic!("Not implemented: exceptions/array indirection"),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
static FLAT: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
@@ -348,17 +339,6 @@ static FLAT: NativeFunction = NativeFunction {
|
||||
}
|
||||
};
|
||||
|
||||
static FLAT_MAP: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
Val::Array(_array_data) => {
|
||||
std::panic!("Not implemented: FLAT_MAP");
|
||||
},
|
||||
_ => std::panic!("Not implemented: exceptions/array indirection"),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
static INCLUDES: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
|
||||
Reference in New Issue
Block a user