pub struct VecBrand;Expand description
Brand for Vec.
Implementations§
Source§impl VecBrand
impl VecBrand
Sourcepub fn construct<A>(head: A, tail: Vec<A>) -> Vec<A>where
A: Clone,
pub fn construct<A>(head: A, tail: Vec<A>) -> Vec<A>where
A: Clone,
Constructs a new vector by prepending a value to an existing vector.
This method creates a new vector with the given head element followed by the elements of the tail vector.
§Type Signature
forall a. (a, Vec a) -> Vec a
§Type Parameters
A: The type of the elements in the vector.
§Parameters
head: A value to prepend to the vector.tail: A vector to prepend the value to.
§Returns
A new vector consisting of the head element prepended to the tail vector.
§Examples
use fp_library::brands::VecBrand;
let head = 1;
let tail = vec![2, 3];
let new_vec = VecBrand::construct(head, tail);
assert_eq!(new_vec, vec![1, 2, 3]);
let empty_tail = vec![];
let single_element = VecBrand::construct(42, empty_tail);
assert_eq!(single_element, vec![42]);Sourcepub fn deconstruct<A>(slice: &[A]) -> Option<(A, Vec<A>)>where
A: Clone,
pub fn deconstruct<A>(slice: &[A]) -> Option<(A, Vec<A>)>where
A: Clone,
Deconstructs a slice into its head element and tail vector.
This method splits a slice into its first element and the rest of the elements as a new vector.
§Type Signature
forall a. [a] -> Option (a, Vec a)
§Type Parameters
A: The type of the elements in the vector.
§Parameters
slice: The vector slice to deconstruct.
§Returns
An Option containing a tuple of the head element and the remaining tail vector,
or None if the slice is empty.
§Examples
use fp_library::brands::VecBrand;
let vec = vec![1, 2, 3];
let deconstructed = VecBrand::deconstruct(&vec);
assert_eq!(deconstructed, Some((1, vec![2, 3])));
let empty: Vec<i32> = vec![];
assert_eq!(VecBrand::deconstruct(&empty), None);Trait Implementations§
Source§impl ApplyFirst for VecBrand
impl ApplyFirst for VecBrand
Source§fn apply_first<'a, A: 'a + Clone, B: 'a + Clone>(
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
fb: <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
fn apply_first<'a, A: 'a + Clone, B: 'a + Clone>( fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, fb: <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
Source§impl ApplySecond for VecBrand
impl ApplySecond for VecBrand
Source§fn apply_second<'a, A: 'a + Clone, B: 'a + Clone>(
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
fb: <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
fn apply_second<'a, A: 'a + Clone, B: 'a + Clone>( fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, fb: <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
Source§impl Compactable for VecBrand
impl Compactable for VecBrand
Source§fn compact<'a, A: 'a>(
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <OptionBrand as Kind_cdc7cd43dac7585f>::Of<'a, A>>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
fn compact<'a, A: 'a>( fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <OptionBrand as Kind_cdc7cd43dac7585f>::Of<'a, A>>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
Compacts a vector of options.
This method flattens a vector of options, discarding None values.
§Type Signature
forall self a. Compactable self => self (Option a) -> self a
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements.
§Parameters
fa: The vector of options.
§Returns
The flattened vector.
§Examples
use fp_library::functions::*;
use fp_library::brands::VecBrand;
let x = vec![Some(1), None, Some(2)];
let y = compact::<VecBrand, _>(x);
assert_eq!(y, vec![1, 2]);Source§fn separate<'a, O: 'a, E: 'a>(
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>,
) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>
fn separate<'a, O: 'a, E: 'a>( fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>, ) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>
Separates a vector of results.
This method separates a vector of results into a pair of vectors.
§Type Signature
forall self o e. Compactable self => self (Result o e) -> Pair (self o) (self e)
§Type Parameters
'a: The lifetime of the elements.O: The type of the success value.E: The type of the error value.
§Parameters
fa: The vector of results.
§Returns
A pair of vectors.
§Examples
use fp_library::{brands::*, functions::*, types::*};
let x = vec![Ok(1), Err("error"), Ok(2)];
let Pair(oks, errs) = separate::<VecBrand, _, _>(x);
assert_eq!(oks, vec![1, 2]);
assert_eq!(errs, vec!["error"]);Source§impl Filterable for VecBrand
impl Filterable for VecBrand
Source§fn partition_map<'a, A: 'a, O: 'a, E: 'a, Func>(
func: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>
fn partition_map<'a, A: 'a, O: 'a, E: 'a, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>
Partitions a vector based on a function that returns a result.
This method partitions a vector based on a function that returns a result.
§Type Signature
forall self a o e. Filterable self => (a -> Result o e, self a) -> Pair (self o) (self e)
§Type Parameters
'a: The lifetime of the elements.A: The type of the input value.O: The type of the success value.E: The type of the error value.Func: The type of the function to apply.
§Parameters
func: The function to apply.fa: The vector to partition.
§Returns
A pair of vectors.
§Examples
use fp_library::{brands::*, functions::*, types::*};
let x = vec![1, 2, 3, 4];
let Pair(oks, errs) = partition_map::<VecBrand, _, _, _, _>(|a| if a % 2 == 0 { Ok(a) } else { Err(a) }, x);
assert_eq!(oks, vec![2, 4]);
assert_eq!(errs, vec![1, 3]);Source§fn partition<'a, A: 'a + Clone, Func>(
func: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>>
fn partition<'a, A: 'a + Clone, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>>
Partitions a vector based on a predicate.
This method partitions a vector based on a predicate.
§Type Signature
forall self a. Filterable self => (a -> bool, self a) -> Pair (self a) (self a)
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements.Func: The type of the predicate.
§Parameters
func: The predicate.fa: The vector to partition.
§Returns
A pair of vectors.
§Examples
use fp_library::{brands::*, functions::*, types::*};
let x = vec![1, 2, 3, 4];
let Pair(satisfied, not_satisfied) = partition::<VecBrand, _, _>(|a| a % 2 == 0, x);
assert_eq!(satisfied, vec![2, 4]);
assert_eq!(not_satisfied, vec![1, 3]);Source§fn filter_map<'a, A: 'a, B: 'a, Func>(
func: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
fn filter_map<'a, A: 'a, B: 'a, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
Maps a function over a vector and filters out None results.
This method maps a function over a vector and filters out None results.
§Type Signature
forall self a b. Filterable self => (a -> Option b, self a) -> self b
§Type Parameters
'a: The lifetime of the elements.A: The type of the input value.B: The type of the result of applying the function.Func: The type of the function to apply.
§Parameters
func: The function to apply.fa: The vector to filter and map.
§Returns
The filtered and mapped vector.
§Examples
use fp_library::functions::*;
use fp_library::brands::VecBrand;
let x = vec![1, 2, 3, 4];
let y = filter_map::<VecBrand, _, _, _>(|a| if a % 2 == 0 { Some(a * 2) } else { None }, x);
assert_eq!(y, vec![4, 8]);Source§fn filter<'a, A: 'a + Clone, Func>(
func: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
fn filter<'a, A: 'a + Clone, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
Filters a vector based on a predicate.
This method filters a vector based on a predicate.
§Type Signature
forall self a. Filterable self => (a -> bool, self a) -> self a
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements.Func: The type of the predicate.
§Parameters
func: The predicate.fa: The vector to filter.
§Returns
The filtered vector.
§Examples
use fp_library::functions::*;
use fp_library::brands::VecBrand;
let x = vec![1, 2, 3, 4];
let y = filter::<VecBrand, _, _>(|a| a % 2 == 0, x);
assert_eq!(y, vec![2, 4]);Source§impl Foldable for VecBrand
impl Foldable for VecBrand
Source§fn fold_right<'a, FnBrand, A: 'a, B: 'a, Func>(
func: Func,
initial: B,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> Bwhere
Func: Fn(A, B) -> B + 'a,
FnBrand: CloneableFn + 'a,
fn fold_right<'a, FnBrand, A: 'a, B: 'a, Func>(
func: Func,
initial: B,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> Bwhere
Func: Fn(A, B) -> B + 'a,
FnBrand: CloneableFn + 'a,
Folds the vector from the right.
This method performs a right-associative fold of the vector.
§Type Signature
forall self a b. Foldable self => ((a, b) -> b, b, self a) -> b
§Type Parameters
'a: The lifetime of the elements.FnBrand: The brand of the cloneable function to use.A: The type of the elements in the vector.B: The type of the accumulator.Func: The type of the folding function.
§Parameters
func: The folding function.initial: The initial value.fa: The vector to fold.
§Returns
The final accumulator value.
§Examples
use fp_library::{brands::*, functions::*};
assert_eq!(fold_right::<RcFnBrand, VecBrand, _, _, _>(|x: i32, acc| x + acc, 0, vec![1, 2, 3]), 6);Source§fn fold_left<'a, FnBrand, A: 'a, B: 'a, Func>(
func: Func,
initial: B,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> Bwhere
Func: Fn(B, A) -> B + 'a,
FnBrand: CloneableFn + 'a,
fn fold_left<'a, FnBrand, A: 'a, B: 'a, Func>(
func: Func,
initial: B,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> Bwhere
Func: Fn(B, A) -> B + 'a,
FnBrand: CloneableFn + 'a,
Folds the vector from the left.
This method performs a left-associative fold of the vector.
§Type Signature
forall self a b. Foldable self => ((b, a) -> b, b, self a) -> b
§Type Parameters
'a: The lifetime of the elements.FnBrand: The brand of the cloneable function to use.A: The type of the elements in the vector.B: The type of the accumulator.Func: The type of the folding function.
§Parameters
func: The function to apply to the accumulator and each element.initial: The initial value of the accumulator.fa: The vector to fold.
§Returns
The final accumulator value.
§Examples
use fp_library::{brands::*, functions::*};
assert_eq!(fold_left::<RcFnBrand, VecBrand, _, _, _>(|acc, x: i32| acc + x, 0, vec![1, 2, 3]), 6);Source§fn fold_map<'a, FnBrand, A: 'a, M, Func>(
func: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> M
fn fold_map<'a, FnBrand, A: 'a, M, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> M
Maps the values to a monoid and combines them.
This method maps each element of the vector to a monoid and then combines the results using the monoid’s append operation.
§Type Signature
forall self a m. (Foldable self, Monoid m) => (a -> m, self a) -> m
§Type Parameters
'a: The lifetime of the elements.FnBrand: The brand of the cloneable function to use.A: The type of the elements in the vector.M: The type of the monoid.Func: The type of the mapping function.
§Parameters
func: The mapping function.fa: The vector to fold.
§Returns
The combined monoid value.
§Examples
use fp_library::{brands::*, functions::*};
assert_eq!(
fold_map::<RcFnBrand, VecBrand, _, _, _>(|x: i32| x.to_string(), vec![1, 2, 3]),
"123".to_string()
);Source§impl Functor for VecBrand
impl Functor for VecBrand
Source§fn map<'a, A: 'a, B: 'a, Func>(
func: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>where
Func: Fn(A) -> B + 'a,
fn map<'a, A: 'a, B: 'a, Func>(
func: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>where
Func: Fn(A) -> B + 'a,
Maps a function over the vector.
This method applies a function to each element of the vector, producing a new vector with the transformed values.
§Type Signature
forall self a b. Functor self => (a -> b, self a) -> self b
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements in the vector.B: The type of the elements in the resulting vector.Func: The type of the function to apply.
§Parameters
func: The function to apply to each element.fa: The vector to map over.
§Returns
A new vector containing the results of applying the function.
§Examples
use fp_library::{brands::*, functions::*};
assert_eq!(map::<VecBrand, _, _, _>(|x: i32| x * 2, vec![1, 2, 3]), vec![2, 4, 6]);Source§impl Kind_cdc7cd43dac7585f for VecBrand
Generated implementation of Kind_cdc7cd43dac7585f for VecBrand.
impl Kind_cdc7cd43dac7585f for VecBrand
Generated implementation of Kind_cdc7cd43dac7585f for VecBrand.
Source§impl Lift for VecBrand
impl Lift for VecBrand
Source§fn lift2<'a, A, B, C, Func>(
func: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
fb: <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, C>
fn lift2<'a, A, B, C, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, fb: <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, C>
Lifts a binary function into the vector context (Cartesian product).
This method applies a binary function to all pairs of elements from two vectors, producing a new vector containing the results (Cartesian product).
§Type Signature
forall self a b c. Lift self => ((a, b) -> c, self a, self b) -> self c
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements in the first vector.B: The type of the elements in the second vector.C: The type of the elements in the resulting vector.Func: The type of the binary function.
§Parameters
func: The binary function to apply.fa: The first vector.fb: The second vector.
§Returns
A new vector containing the results of applying the function to all pairs of elements.
§Examples
use fp_library::{brands::*, functions::*};
assert_eq!(
lift2::<VecBrand, _, _, _, _>(|x, y| x + y, vec![1, 2], vec![10, 20]),
vec![11, 21, 12, 22]
);Source§impl Ord for VecBrand
impl Ord for VecBrand
Source§impl ParFoldable for VecBrand
impl ParFoldable for VecBrand
Source§fn par_fold_map<'a, FnBrand, A, M>(
func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> M
fn par_fold_map<'a, FnBrand, A, M>( func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> M
Maps values to a monoid and combines them in parallel.
This method maps each element of the vector to a monoid and then combines the results using the monoid’s append operation. The mapping and combination operations may be executed in parallel.
Note: The rayon feature must be enabled to use parallel iteration.
§Type Signature
forall self a m. (ParFoldable self, Monoid m) => (a -> m, self a) -> m
§Type Parameters
'a: The lifetime of the elements.FnBrand: The brand of the cloneable function wrapper.A: The element type.M: The monoid type.
§Parameters
func: The thread-safe function to map each element to a monoid.fa: The vector to fold.
§Returns
The combined monoid value.
§Examples
use fp_library::{brands::*, functions::*};
let v = vec![1, 2, 3];
let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
assert_eq!(par_fold_map::<ArcFnBrand, VecBrand, _, _>(f, v), "123".to_string());Source§fn par_fold_right<'a, FnBrand, A, B>(
func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
init: B,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> B
fn par_fold_right<'a, FnBrand, A, B>( func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>, init: B, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> B
Source§impl PartialOrd for VecBrand
impl PartialOrd for VecBrand
Source§impl Pointed for VecBrand
impl Pointed for VecBrand
Source§fn pure<'a, A: 'a>(a: A) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
fn pure<'a, A: 'a>(a: A) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
Wraps a value in a vector.
This method creates a new vector containing the single given value.
§Type Signature
forall self a. Pointed self => a -> self a
§Type Parameters
'a: The lifetime of the value.A: The type of the value to wrap.
§Parameters
a: The value to wrap.
§Returns
A vector containing the single value.
§Examples
use fp_library::functions::*;
use fp_library::brands::VecBrand;
assert_eq!(pure::<VecBrand, _>(5), vec![5]);Source§impl Semiapplicative for VecBrand
impl Semiapplicative for VecBrand
Source§fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
ff: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>( ff: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
Applies wrapped functions to wrapped values (Cartesian product).
This method applies each function in the first vector to each value in the second vector, producing a new vector containing all the results.
§Type Signature
forall self a b. Semiapplicative self => (self (a -> b), self a) -> self b
§Type Parameters
'a: The lifetime of the values.FnBrand: The brand of the cloneable function wrapper.A: The type of the input values.B: The type of the output values.
§Parameters
ff: The vector containing the functions.fa: The vector containing the values.
§Returns
A new vector containing the results of applying each function to each value.
§Examples
use fp_library::{brands::*, classes::*, functions::*};
let funcs = vec![
cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x + 1),
cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2),
];
assert_eq!(apply::<RcFnBrand, VecBrand, _, _>(funcs, vec![1, 2]), vec![2, 3, 2, 4]);Source§impl Semimonad for VecBrand
impl Semimonad for VecBrand
Source§fn bind<'a, A: 'a, B: 'a, Func>(
ma: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
func: Func,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
fn bind<'a, A: 'a, B: 'a, Func>( ma: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, func: Func, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
Chains vector computations (flat_map).
This method applies a function that returns a vector to each element of the input vector, and then flattens the result.
§Type Signature
forall self a b. Semimonad self => (self a, a -> self b) -> self b
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements in the input vector.B: The type of the elements in the output vector.Func: The type of the function to apply.
§Parameters
ma: The first vector.func: The function to apply to each element, returning a vector.
§Returns
A new vector containing the flattened results.
§Examples
use fp_library::functions::*;
use fp_library::brands::VecBrand;
assert_eq!(
bind::<VecBrand, _, _, _>(vec![1, 2], |x| vec![x, x * 2]),
vec![1, 2, 2, 4]
);Source§impl Traversable for VecBrand
impl Traversable for VecBrand
Source§fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
func: Func,
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>>where
Func: Fn(A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, B> + 'a,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone,
<F as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone,
fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
func: Func,
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>>where
Func: Fn(A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, B> + 'a,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone,
<F as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone,
Traverses the vector with an applicative function.
This method maps each element of the vector to a computation, evaluates them, and combines the results into an applicative context.
§Type Signature
forall self a b f. (Traversable self, Applicative f) => (a -> f b, self a) -> f (self b)
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements in the traversable structure.B: The type of the elements in the resulting traversable structure.F: The applicative context.Func: The type of the function to apply.
§Parameters
func: The function to apply to each element, returning a value in an applicative context.ta: The vector to traverse.
§Returns
The vector wrapped in the applicative context.
§Examples
use fp_library::functions::*;
use fp_library::brands::{OptionBrand, VecBrand};
assert_eq!(
traverse::<VecBrand, _, _, OptionBrand, _>(|x| Some(x * 2), vec![1, 2, 3]),
Some(vec![2, 4, 6])
);Source§fn sequence<'a, A: 'a + Clone, F: Applicative>(
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <F as Kind_cdc7cd43dac7585f>::Of<'a, A>>,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>>where
<F as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone,
fn sequence<'a, A: 'a + Clone, F: Applicative>(
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <F as Kind_cdc7cd43dac7585f>::Of<'a, A>>,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>>where
<F as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone,
Sequences a vector of applicative.
This method evaluates the computations inside the vector and accumulates the results into an applicative context.
§Type Signature
forall self a f. (Traversable self, Applicative f) => self (f a) -> f (self a)
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements in the traversable structure.F: The applicative context.
§Parameters
ta: The vector containing the applicative values.
§Returns
The vector wrapped in the applicative context.
§Examples
use fp_library::functions::*;
use fp_library::brands::{OptionBrand, VecBrand};
assert_eq!(
sequence::<VecBrand, _, OptionBrand>(vec![Some(1), Some(2)]),
Some(vec![1, 2])
);Source§impl Witherable for VecBrand
impl Witherable for VecBrand
Source§fn wilt<'a, M: Applicative, A: 'a + Clone, O: 'a + Clone, E: 'a + Clone, Func>(
func: Func,
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>>where
Func: Fn(A) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>> + 'a,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>: Clone,
<M as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>: Clone,
fn wilt<'a, M: Applicative, A: 'a + Clone, O: 'a + Clone, E: 'a + Clone, Func>(
func: Func,
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>>where
Func: Fn(A) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>> + 'a,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>: Clone,
<M as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>: Clone,
Partitions a vector based on a function that returns a result in an applicative context.
This method partitions a vector based on a function that returns a result in an applicative context.
§Type Signature
forall self m a o e. (Witherable self, Applicative m) => (a -> m (Result o e), self a) -> m (Pair (self o) (self e))
§Type Parameters
'a: The lifetime of the elements.M: The applicative context.A: The type of the input value.O: The type of the success value.E: The type of the error value.Func: The type of the function to apply.
§Parameters
func: The function to apply.ta: The vector to partition.
§Returns
The partitioned vector wrapped in the applicative context.
§Examples
use fp_library::{brands::*, functions::*, types::*};
let x = vec![1, 2, 3, 4];
let y = wilt::<VecBrand, OptionBrand, _, _, _, _>(|a| Some(if a % 2 == 0 { Ok(a) } else { Err(a) }), x);
assert_eq!(y, Some(Pair(vec![2, 4], vec![1, 3])));Source§fn wither<'a, M: Applicative, A: 'a + Clone, B: 'a + Clone, Func>(
func: Func,
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>>where
Func: Fn(A) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>> + 'a,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>>: Clone,
<M as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>>: Clone,
fn wither<'a, M: Applicative, A: 'a + Clone, B: 'a + Clone, Func>(
func: Func,
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>>where
Func: Fn(A) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>> + 'a,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>>: Clone,
<M as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>>: Clone,
Maps a function over a vector and filters out None results in an applicative context.
This method maps a function over a vector and filters out None results in an applicative context.
§Type Signature
forall self m a b. (Witherable self, Applicative m) => (a -> m (Option b), self a) -> m (self b)
§Type Parameters
'a: The lifetime of the values.M: The applicative context.A: The type of the elements in the input structure.B: The type of the result of applying the function.Func: The type of the function to apply.
§Parameters
func: The function to apply to each element, returning anOptionin an applicative context.ta: The vector to filter and map.
§Returns
The filtered and mapped vector wrapped in the applicative context.
§Examples
use fp_library::functions::*;
use fp_library::brands::{VecBrand, OptionBrand};
let x = vec![1, 2, 3, 4];
let y = wither::<VecBrand, OptionBrand, _, _, _>(|a| Some(if a % 2 == 0 { Some(a * 2) } else { None }), x);
assert_eq!(y, Some(vec![4, 8]));impl Copy for VecBrand
impl Eq for VecBrand
impl StructuralPartialEq for VecBrand
Auto Trait Implementations§
impl Freeze for VecBrand
impl RefUnwindSafe for VecBrand
impl Send for VecBrand
impl Sync for VecBrand
impl Unpin for VecBrand
impl UnwindSafe for VecBrand
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more