Skip to main content

VecBrand

Struct VecBrand 

Source
pub struct VecBrand;
Expand description

Brand for Vec.

Implementations§

Source§

impl VecBrand

Source

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]);
Source

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

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>

Combines two contexts, keeping the value from the first context. Read more
Source§

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>

Combines two contexts, keeping the value from the second context. Read more
Source§

impl Clone for VecBrand

Source§

fn clone(&self) -> VecBrand

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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>

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>>

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 Debug for VecBrand

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for VecBrand

Source§

fn default() -> VecBrand

Returns the “default value” for a type. Read more
Source§

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>>
where Func: Fn(A) -> Result<O, E> + 'a,

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>>
where Func: Fn(A) -> bool + '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>
where Func: Fn(A) -> Option<B> + 'a,

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>
where Func: Fn(A) -> bool + '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

Source§

fn fold_right<'a, FnBrand, A: 'a, B: 'a, Func>( func: Func, initial: B, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> B
where 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>, ) -> B
where 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
where M: Monoid + 'a, Func: Fn(A) -> M + 'a, FnBrand: CloneableFn + 'a,

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

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,

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 Hash for VecBrand

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Kind_cdc7cd43dac7585f for VecBrand

Generated implementation of Kind_cdc7cd43dac7585f for VecBrand.

Source§

type Of<'a, A: 'a> = Vec<A>

The applied type.
Source§

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>
where Func: Fn(A, B) -> C + 'a, A: Clone + 'a, B: Clone + 'a, C: 'a,

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

Source§

fn cmp(&self, other: &VecBrand) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

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
where FnBrand: 'a + SendCloneableFn, A: 'a + Clone + Send + Sync, M: Monoid + Send + Sync + 'a,

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
where A: 'a + Clone + Send + Sync, B: Send + Sync + 'a, FnBrand: 'a + SendCloneableFn,

Parallel version of fold_right. Read more
Source§

impl PartialEq for VecBrand

Source§

fn eq(&self, other: &VecBrand) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for VecBrand

Source§

fn partial_cmp(&self, other: &VecBrand) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Pointed for VecBrand

Source§

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

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>

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

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>
where Func: Fn(A) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B> + 'a,

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

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,

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,

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

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,

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,

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 an Option in 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]));
Source§

impl Copy for VecBrand

Source§

impl Eq for VecBrand

Source§

impl StructuralPartialEq for VecBrand

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<Brand> Applicative for Brand

Source§

impl<Brand> Monad for Brand
where Brand: Applicative + Semimonad,