Skip to main content

extend

Function extend 

Source
pub fn extend<'a, Brand: Extend, A: 'a + Clone, B: 'a>(
    f: impl Fn(<Brand as Kind_cdc7cd43dac7585f>::Of<'a, A>) -> B + 'a,
    wa: <Brand as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Brand as Kind_cdc7cd43dac7585f>::Of<'a, B>
Expand description

Extends a local context-dependent computation to a global computation.

Free function version that dispatches to the type class’ associated function.

§Type Signature

forall Brand A B. Extend Brand => (Brand A -> B, Brand A) -> Brand B

§Type Parameters

  • 'a: The lifetime of the values.
  • Brand: The brand of the comonadic context.
  • A: The type of the value(s) inside the comonadic context.
  • B: The result type of the extension function.

§Parameters

  • f: The function that consumes a whole context and produces a value.
  • wa: The comonadic context to extend over.

§Returns

A new comonadic context containing the results of applying the function.

§Examples

use fp_library::{
	brands::*,
	functions::*,
	types::*,
};

// extend on Identity: apply f to the whole container
let w = Identity(10);
let result = extend::<IdentityBrand, _, _>(|id| id.0 * 2, w);
assert_eq!(result, Identity(20));

// extend on Vec: apply f to each suffix
let v = vec![1, 2, 3];
let sums = extend::<VecBrand, _, _>(|s: Vec<i32>| s.iter().sum::<i32>(), v);
assert_eq!(sums, vec![6, 5, 3]);