Writer

Struct Writer 

Source
pub struct Writer<A, W: Monoid> { /* private fields */ }
Expand description

Writer helps capture the pattern of writing to a pure log or accumulated value, handling the book-keeping for you. This is often used for loggers, but could be anything as long as the hidden value is a Monoid.

There are many applications of Writers, but as an illustrative point, one could use it for logging across processes and time, since the log is carried around with the result in a pure fashion. The monadic DSL helps make using these feel more natural.

Implementations§

Source§

impl<A, W: Monoid> Writer<A, W>

Source

pub fn new(value: A, log: W) -> Self

Construct a Writer struct from a starting value and log.

Examples found in repository?
examples/turtle/model/helpers.rs (lines 14-17)
8pub fn lift_op<'a, A: 'a>(
9    op_fun: impl Fn(Turtle, A) -> Turtle + 'static,
10) -> impl FnOnce(A) -> StateT<'a, Turtle, ResultT<Writer<Result<((), Turtle), TurtleError>, String>>>
11{
12    move |p: A| {
13        StateT::new(move |s| {
14            ResultT::lift(Writer::new(
15                ((), op_fun(s, p)),
16                <String as Monoid>::mempty(),
17            ))
18        })
19    }
20}
21
22pub fn lift_validation<'a, A: 'a>(
23    validate_fun: impl Fn(String) -> Result<A, TurtleError>,
24) -> impl Fn(&str) -> StateT<'a, Turtle, ResultT<Writer<Result<(A, Turtle), TurtleError>, String>>>
25{
26    move |p: &str| {
27        StateT::<'a, Turtle, ResultT<Writer<Result<(A, Turtle), TurtleError>, String>>>::lift(
28            ResultT::new(Writer::new(
29                validate_fun(p.to_string()),
30                <String as Monoid>::mempty(),
31            )),
32        )
33    }
34}
Source

pub fn tell(log: W) -> Writer<(), W>

Construct a Writer struct from a log.

Examples found in repository?
examples/turtle/model/helpers.rs (line 40)
36pub fn log_info(
37    l: String,
38) -> StateT<'static, Turtle, ResultT<Writer<Result<((), Turtle), TurtleError>, String>>> {
39    StateT::<Turtle, ResultT<Writer<Result<(), TurtleError>, String>>>::lift(ResultT::lift(
40        Writer::<Turtle, _>::tell(String::from(l)),
41    ))
42}
Source

pub fn execute(self) -> (A, W)

Extract the enclosed value and log from an Writer.

Examples found in repository?
examples/turtle/model/helpers.rs (line 87)
83pub fn run(
84    computation: StateT<Turtle, ResultT<Writer<Result<((), Turtle), TurtleError>, String>>>,
85    t: Turtle,
86) -> (Result<((), Turtle), TurtleError>, String) {
87    Writer::execute(ResultT::execute(StateT::execute(computation, t)))
88}

Trait Implementations§

Source§

impl<'a, A: 'a, W: Monoid> Applicative<'a> for Writer<A, W>

Source§

fn of(value: Self::Unwrapped) -> Self::Wrapped<Self::Unwrapped>

Lift a value into a context
Source§

impl<'a, A, W: Monoid> Apply<'a> for Writer<A, W>

Source§

fn ap<F, B: 'a>(self, f: Self::Wrapped<F>) -> Self::Wrapped<B>
where F: FnOnce(Self::Unwrapped) -> B + 'a,

Apply a function wrapped in a context to to a value wrapped in the same type of context
Source§

fn lift_a2<F, B: 'a, C: 'a>(self, b: Self::Wrapped<B>, f: F) -> Self::Wrapped<C>
where F: FnOnce(Self::Unwrapped, B) -> C,

Lift an (unwrapped) binary function and apply to two wrapped values
Source§

impl<'a, A, W: Monoid> Functor<'a> for Writer<A, W>

Source§

type Unwrapped = A

The inner type which will be mapped over
Source§

type Wrapped<B: 'a> = Writer<B, W>

Target of the fmap operation. Like Self, but has a different wrapped up value underneath.
Source§

fn fmap<F, B: 'a>(self, f: F) -> Self::Wrapped<B>
where F: FnOnce(Self::Unwrapped) -> B,

fmap is used to apply a function of type (a -> b) to a value of type f a, where f is a functor, to produce a value of type f b.
Source§

impl<'a, A: 'a, W: Monoid> Monad<'a> for Writer<A, W>

Source§

type Unwrapped = A

Source§

type Wrapped<B: 'a> = Writer<B, W>

Source§

fn bind<F, B: 'a>(self, f: F) -> Self::Wrapped<B>
where F: FnOnce(Self::Unwrapped) -> Self::Wrapped<B> + 'a,

Sequentially compose actions, piping values through successive function chains. Read more
Source§

fn of<T: 'a>(value: T) -> Self::Wrapped<T>

Lift a value into a context

Auto Trait Implementations§

§

impl<A, W> Freeze for Writer<A, W>
where A: Freeze, W: Freeze,

§

impl<A, W> RefUnwindSafe for Writer<A, W>

§

impl<A, W> Send for Writer<A, W>
where A: Send, W: Send,

§

impl<A, W> Sync for Writer<A, W>
where A: Sync, W: Sync,

§

impl<A, W> Unpin for Writer<A, W>
where A: Unpin, W: Unpin,

§

impl<A, W> UnwindSafe for Writer<A, W>
where A: UnwindSafe, W: 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> 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, 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.