pub fn map_indexed<T, U>(
    list: ReadSignal<Vec<T>>,
    map_fn: impl Fn(&T) -> U + 'static,
) -> impl FnMut() -> Vec<U>
where
    T: PartialEq + Clone,
    U: Clone + 'static,
{
    let parent_scope = current_scope();

    // Previous state used for diffing.
    let mut items = Rc::new(Vec::new());
    let mapped = Rc::new(RefCell::new(Vec::new()));
    let mut scopes = Vec::new();

    move || {
        let new_items = list.get(); // Subscribe to list.
        untrack(|| {
            if new_items.is_empty() {
                // Fast path for removing all items.
                scopes = Vec::new();
                items = Rc::new(Vec::new());
                *mapped.borrow_mut() = Vec::new();
            } else {
                // Pre-allocate space needed
                if new_items.len() > items.len() {
                    let new_count = new_items.len() - items.len();
                    mapped.borrow_mut().reserve(new_count);
                    scopes.reserve(new_count);
                }

                for (i, new_item) in new_items.iter().enumerate() {
                    let item = items.get(i);

                    if item.is_none() {
                        let new_scope = create_child_scope_in(&parent_scope, || {
                            mapped.borrow_mut().push(map_fn(new_item));
                        });
                        scopes.push(new_scope);
                    } else if item != Some(new_item) {
                        let new_scope = create_child_scope_in(&parent_scope, || {
                            mapped.borrow_mut()[i] = map_fn(new_item);
                        });
                        scopes[i] = new_scope;
                    }
                }

                if new_items.len() < items.len() {
                    for _i in new_items.len()..items.len() {
                        scopes.pop();
                    }
                }

                // In case the new set is shorter than the old, set the length of the mapped array.
                mapped.borrow_mut().truncate(new_items.len());
                scopes.truncate(new_items.len());

                // save a copy of the mapped items for the next update.
                items = Rc::clone(&new_items);
                debug_assert!([items.len(), mapped.borrow().len(), scopes.len()]
                    .iter()
                    .all(|l| *l == new_items.len()));
            }

            mapped.borrow().clone()
        })
    }
}
/// Function that maps a `Vec` to another `Vec` via a map function. The mapped `Vec` is lazy
/// computed, meaning that it's value will only be updated when requested. Modifications to the
/// input `Vec` are diffed by index to prevent recomputing values that have not changed.
///
/// Generally, it is preferred to use [`map_keyed`] instead when a key function is available.
///
/// This function is the underlying utility behind `Indexed`.
///
/// # Params
/// * `list` - The list to be mapped. The list must be a [`ReadSignal`] (obtained from a [`Signal`])
///   and therefore reactive.
/// * `map_fn` - A closure that maps from the input type to the output type.
Function that maps a `Vec` to another `Vec` via a map function. The mapped `Vec` is lazy
computed, meaning that it's value will only be updated when requested. Modifications to the
input `Vec` are diffed by index to prevent recomputing values that have not changed.

Generally, it is preferred to use [`map_keyed`] instead when a key function is available.

This function is the underlying utility behind `Indexed`.
Function that maps a ` Vec ` to another ` Vec ` via a map function . The mapped ` Vec ` is lazy computed meaning that it ' s value will only be updated when requested . Modifications to the input ` Vec ` are diffed by index to prevent recomputing values that have not changed . Generally it is preferred to use [ ` map_keyed ` ] instead when a key function is available . This function is the underlying utility behind ` Indexed ` .
pub fn map_indexed<T, U>(
    list: ReadSignal<Vec<T>>,
    map_fn: impl Fn(&T) -> U + 'static,
) -> impl FnMut() -> Vec<U>
where
    T: PartialEq + Clone,
    U: Clone + 'static,
{
    let parent_scope = current_scope();
    // Previous state used for diffing.
    let mut items = Rc::new(Vec::new());
    let mapped = Rc::new(RefCell::new(Vec::new()));
    let mut scopes = Vec::new();
    move || {
        let new_items = list.get(); // Subscribe to list.
        untrack(|| {
            if new_items.is_empty() {
                // Fast path for removing all items.
                scopes = Vec::new();
                items = Rc::new(Vec::new());
                *mapped.borrow_mut() = Vec::new();
            } else {
                // Pre-allocate space needed
                if new_items.len() > items.len() {
                    let new_count = new_items.len() - items.len();
                    mapped.borrow_mut().reserve(new_count);
                    scopes.reserve(new_count);
                }
                for (i, new_item) in new_items.iter().enumerate() {
                    let item = items.get(i);
                    if item.is_none() {
                        let new_scope = create_child_scope_in(&parent_scope, || {
                            mapped.borrow_mut().push(map_fn(new_item));
                        });
                        scopes.push(new_scope);
                    } else if item != Some(new_item) {
                        let new_scope = create_child_scope_in(&parent_scope, || {
                            mapped.borrow_mut()[i] = map_fn(new_item);
                        });
                        scopes[i] = new_scope;
                    }
                }
                if new_items.len() < items.len() {
                    for _i in new_items.len()..items.len() {
                        scopes.pop();
                    }
                }
                // In case the new set is shorter than the old, set the length of the mapped array.
                mapped.borrow_mut().truncate(new_items.len());
                scopes.truncate(new_items.len());
                // save a copy of the mapped items for the next update.
                items = Rc::clone(&new_items);
                debug_assert!([items.len(), mapped.borrow().len(), scopes.len()]
                    .iter()
                    .all(|l| *l == new_items.len()));
            }
            mapped.borrow().clone()
        })
    }
}
pub fn map_indexed < T , U > ( list : ReadSignal < Vec < T > > , map_fn : impl Fn ( & T ) -> U + ' static , ) -> impl FnMut ( ) -> Vec < U > where T : PartialEq + Clone , U : Clone + ' static , { let parent_scope = current_scope ( ) ; // Previous state used for diffing. let mut items = Rc :: new ( Vec :: new ( ) ) ; let mapped = Rc :: new ( RefCell :: new ( Vec :: new ( ) ) ) ; let mut scopes = Vec :: new ( ) ; move | | { let new_items = list . get ( ) ; // Subscribe to list. untrack ( | | { if new_items . is_empty ( ) { // Fast path for removing all items. scopes = Vec :: new ( ) ; items = Rc :: new ( Vec :: new ( ) ) ; * mapped . borrow_mut ( ) = Vec :: new ( ) ; } else { // Pre-allocate space needed if new_items . len ( ) > items . len ( ) { let new_count = new_items . len ( ) - items . len ( ) ; mapped . borrow_mut ( ) . reserve ( new_count ) ; scopes . reserve ( new_count ) ; } for ( i , new_item ) in new_items . iter ( ) . enumerate ( ) { let item = items . get ( i ) ; if item . is_none ( ) { let new_scope = create_child_scope_in ( & parent_scope , | | { mapped . borrow_mut ( ) . push ( map_fn ( new_item ) ) ; } ) ; scopes . push ( new_scope ) ; } else if item != Some ( new_item ) { let new_scope = create_child_scope_in ( & parent_scope , | | { mapped . borrow_mut ( ) [ i ] = map_fn ( new_item ) ; } ) ; scopes [ i ] = new_scope ; } } if new_items . len ( ) < items . len ( ) { for _i in new_items . len ( ) .. items . len ( ) { scopes . pop ( ) ; } } // In case the new set is shorter than the old, set the length of the mapped array. mapped . borrow_mut ( ) . truncate ( new_items . len ( ) ) ; scopes . truncate ( new_items . len ( ) ) ; // save a copy of the mapped items for the next update. items = Rc :: clone ( & new_items ) ; debug_assert ! ( [ items len ( ) mapped borrow ( ) len ( ) scopes len ( ) ] iter ( ) all ( l l new_items len ( ) ) ) ; } mapped . borrow ( ) . clone ( ) } ) } }
Function that maps a `Vec` to another `Vec` via a map function.
Function that maps a ` Vec ` to another ` Vec ` via a map function .
list
ReadSignal<Vec<T>>
map_fn
impl Fn(&T) -> U + 'static
params
`list` - The list to be mapped. The list must be a [`ReadSignal`] (obtained from a [`Signal`])
and therefore reactive.
`map_fn` - A closure that maps from the input type to the output type.
` list ` - The list to be mapped . The list must be a [ ` ReadSignal ` ] ( obtained from a [ ` Signal ` ] ) and therefore reactive . ` map_fn ` - A closure that maps from the input type to the output type .