From dcf7edf6a3aac0a623a1a791027ed79a247d35fb Mon Sep 17 00:00:00 2001 From: Andreas Larsen Date: Tue, 6 Sep 2022 19:17:29 +0200 Subject: [PATCH] refactor out modules to files --- src/labelgen.rs | 35 ++++++++++++++++++++++ src/main.rs | 78 ++----------------------------------------------- src/state.rs | 33 +++++++++++++++++++++ 3 files changed, 71 insertions(+), 75 deletions(-) create mode 100644 src/labelgen.rs create mode 100644 src/state.rs diff --git a/src/labelgen.rs b/src/labelgen.rs new file mode 100644 index 0000000..0b9d423 --- /dev/null +++ b/src/labelgen.rs @@ -0,0 +1,35 @@ +use crate::state::WithState; + +// Generate a next label +pub trait States { + type Item; + fn init() -> Self::Item; + fn next<'c, Value>(f: impl FnOnce(Self::Item) -> Value + 'c) -> WithState<'c, Value, Self::Item>; +} + +pub struct IntCounter; + +impl States for IntCounter +{ + type Item = i32; + + fn init() -> i32 { 0 } + + fn next<'c, Value>(f: impl FnOnce(i32) -> Value + 'c) -> WithState<'c, Value, i32> { + WithState::of(move |n| { (f(n), n + 1) }) + } +} + + +pub struct StrCat; + +impl States for StrCat +{ + type Item = String; + + fn init() -> String { "a".to_string() } + + fn next<'c, Value>(f: impl FnOnce(Self::Item) -> Value + 'c) -> WithState<'c, Value, Self::Item> { + WithState::of(move |n: String| { let s = n.clone(); (f(n), format!("{}a", s)) }) + } +} diff --git a/src/main.rs b/src/main.rs index 7bffcae..0e157b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,79 +1,7 @@ -mod state { - // State monad - pub struct WithState<'c, Value, State>(Box (Value, State) + 'c>); +mod state; +mod labelgen; - impl <'c, Value, State> WithState<'c, Value, State> { - pub fn of(f: impl FnOnce(State) -> (Value, State) + 'c) -> WithState<'c, Value, State> { - WithState(Box::new(f)) - } - - pub fn run(self, s: State) -> (Value, State) { - self.0(s) - } - - pub fn bind<'cn, NewValue>(self, f: impl FnOnce(Value) -> WithState<'cn, NewValue, State> + 'cn) -> WithState<'cn, NewValue, State> - where 'c: 'cn, State : 'cn, Value : 'cn { - WithState::<'cn, NewValue, State>::of(move |n| { - let (x,sp) = self.run(n); - f(x).run(sp) - }) - } - - pub fn zip<'cn, NewValue>(self, st: WithState<'cn, NewValue, State>) -> WithState<'cn, (Value,NewValue), State> - where 'c: 'cn, State: 'cn, Value: 'cn, NewValue: 'cn { - self.bind(|aa| st.map(|bb| (aa,bb))) - } - - pub fn map<'cn, NewValue>(self, f: impl FnOnce(Value) -> NewValue + 'cn) -> WithState<'cn, NewValue, State> - where 'c: 'cn, State : 'cn, Value : 'cn { - WithState::<'cn, NewValue, State>::of(move |n| { - let (x, sp) = self.run(n); - ( f(x), sp ) - }) - } - } -} - - -mod labelgenerators { - use super::state::WithState; - - // Generate a next label - pub trait States { - type Item; - fn init() -> Self::Item; - fn next<'c, Value>(f: impl FnOnce(Self::Item) -> Value + 'c) -> WithState<'c, Value, Self::Item>; - } - - pub struct IntCounter; - - impl States for IntCounter - { - type Item = i32; - - fn init() -> i32 { 0 } - - fn next<'c, Value>(f: impl FnOnce(i32) -> Value + 'c) -> WithState<'c, Value, i32> { - WithState::of(move |n| { (f(n), n + 1) }) - } - } - - - pub struct StrCat; - - impl States for StrCat - { - type Item = String; - - fn init() -> String { "a".to_string() } - - fn next<'c, Value>(f: impl FnOnce(Self::Item) -> Value + 'c) -> WithState<'c, Value, Self::Item> { - WithState::of(move |n: String| { let s = n.clone(); (f(n), format!("{}a", s)) }) - } - } -} - -use labelgenerators::*; +use labelgen::{IntCounter, States}; use state::WithState; // Tree diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..58bac65 --- /dev/null +++ b/src/state.rs @@ -0,0 +1,33 @@ +// State monad +pub struct WithState<'c, Value, State>(Box (Value, State) + 'c>); + +impl <'c, Value, State> WithState<'c, Value, State> { + pub fn of(f: impl FnOnce(State) -> (Value, State) + 'c) -> WithState<'c, Value, State> { + WithState(Box::new(f)) + } + + pub fn run(self, s: State) -> (Value, State) { + self.0(s) + } + + pub fn bind<'cn, NewValue>(self, f: impl FnOnce(Value) -> WithState<'cn, NewValue, State> + 'cn) -> WithState<'cn, NewValue, State> + where 'c: 'cn, State : 'cn, Value : 'cn { + WithState::<'cn, NewValue, State>::of(move |n| { + let (x,sp) = self.run(n); + f(x).run(sp) + }) + } + + pub fn zip<'cn, NewValue>(self, st: WithState<'cn, NewValue, State>) -> WithState<'cn, (Value,NewValue), State> + where 'c: 'cn, State: 'cn, Value: 'cn, NewValue: 'cn { + self.bind(|aa| st.map(|bb| (aa,bb))) + } + + pub fn map<'cn, NewValue>(self, f: impl FnOnce(Value) -> NewValue + 'cn) -> WithState<'cn, NewValue, State> + where 'c: 'cn, State : 'cn, Value : 'cn { + WithState::<'cn, NewValue, State>::of(move |n| { + let (x, sp) = self.run(n); + ( f(x), sp ) + }) + } +}