Skip to main content

WriterT

Struct WriterT 

Source
pub struct WriterT<W, MKind: Kind1, A> {
    pub run_writer_t: MKind::Of<(A, W)>,
    /* private fields */
}
Expand description

The WriterT monad transformer for Kind-encoded types.

WriterT<W, MKind, A> wraps an inner value MKind::Of<(A, W)>: the produced value A paired with an accumulated monoidal log W. The value is stored in run_writer_t.

§Type Parameters

  • W: the log type (must be a Monoid for the Applicative/Monad instances).
  • MKind: the Kind marker for the inner monad (must implement Kind1).
  • A: the value produced alongside the log.

Fields§

§run_writer_t: MKind::Of<(A, W)>

The wrapped inner computation: MKind::Of<(A, W)> (value-then-log).

Implementations§

Source§

impl<W, MKind: Kind1, A> WriterT<W, MKind, A>

Source

pub fn new(inner: MKind::Of<(A, W)>) -> Self

Creates a new WriterT from an inner value MKind::Of<(A, W)>.

Source§

impl<W, MKind: Kind1, A> WriterT<W, MKind, A>

Source

pub fn eval_writer_t(self) -> MKind::Of<A>
where W: 'static, A: 'static, MKind: Functor<(A, W), A> + 'static, MKind::Of<(A, W)>: 'static,

Runs the computation and keeps only the produced value, discarding the log: MKind::Of<A> (inner Functor, projecting fst).

Source

pub fn exec_writer_t(self) -> MKind::Of<W>
where W: 'static, A: 'static, MKind: Functor<(A, W), W> + 'static, MKind::Of<(A, W)>: 'static,

Runs the computation and keeps only the accumulated log, discarding the value: MKind::Of<W> (inner Functor, projecting snd).

Source

pub fn listen(self) -> WriterT<W, MKind, (A, W)>
where W: Clone + 'static, A: 'static, MKind: Functor<(A, W), ((A, W), W)> + 'static, MKind::Of<(A, W)>: 'static, MKind::Of<((A, W), W)>: 'static,

Exposes the accumulated log alongside the value, leaving it in place — the chainable method form of <WriterTKind<W, MKind> as MonadWriter<W, A, MKind>>::listen(self).

The produced value becomes (A, W) and the log remains W.

§Example
use monadify::transformers::writer::{Writer, WriterTKind, MonadWriter};
use monadify::identity::{Identity, IdentityKind};

let w: Writer<String, ()> =
    WriterTKind::<String, IdentityKind>::tell("xyz".to_string());
let Identity((((), exposed), log)) = w.listen().run_writer_t;
assert_eq!(exposed, "xyz");
assert_eq!(log, "xyz");
Source

pub fn censor<F>(self, f: F) -> Self
where W: 'static, A: 'static, MKind: Functor<(A, W), (A, W)> + 'static, MKind::Of<(A, W)>: 'static, F: Fn(W) -> W + Clone + 'static,

Rewrites the log with f, leaving the value unchanged — the chainable method form of <WriterTKind<W, MKind> as MonadWriter<W, A, MKind>>::censor(f, self).

The result is (a, f(w)).

§Example
use monadify::transformers::writer::{Writer, WriterTKind, MonadWriter};
use monadify::identity::{Identity, IdentityKind};

let w: Writer<String, ()> =
    WriterTKind::<String, IdentityKind>::tell("quiet".to_string());
let Identity(((), log)) = w.censor(|s: String| s.to_uppercase()).run_writer_t;
assert_eq!(log, "QUIET");

Trait Implementations§

Source§

impl<W, MKind: Kind1, A> Clone for WriterT<W, MKind, A>
where MKind::Of<(A, W)>: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<W, MKind, A> Freeze for WriterT<W, MKind, A>
where <MKind as Kind>::Of<(A, W)>: Freeze,

§

impl<W, MKind, A> RefUnwindSafe for WriterT<W, MKind, A>
where <MKind as Kind>::Of<(A, W)>: RefUnwindSafe, W: RefUnwindSafe, MKind: RefUnwindSafe, A: RefUnwindSafe,

§

impl<W, MKind, A> Send for WriterT<W, MKind, A>
where <MKind as Kind>::Of<(A, W)>: Send, W: Send, MKind: Send, A: Send,

§

impl<W, MKind, A> Sync for WriterT<W, MKind, A>
where <MKind as Kind>::Of<(A, W)>: Sync, W: Sync, MKind: Sync, A: Sync,

§

impl<W, MKind, A> Unpin for WriterT<W, MKind, A>
where <MKind as Kind>::Of<(A, W)>: Unpin, W: Unpin, MKind: Unpin, A: Unpin,

§

impl<W, MKind, A> UnsafeUnpin for WriterT<W, MKind, A>
where <MKind as Kind>::Of<(A, W)>: UnsafeUnpin,

§

impl<W, MKind, A> UnwindSafe for WriterT<W, MKind, A>
where <MKind as Kind>::Of<(A, W)>: UnwindSafe, W: UnwindSafe, MKind: UnwindSafe, A: UnwindSafe,

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