Struct prop_check_rs::state::State
source · pub struct State<S, A> { /* private fields */ }Implementations§
source§impl<S, A> State<S, A>where
S: 'static,
A: Clone + 'static,
impl<S, A> State<S, A>where
S: 'static,
A: Clone + 'static,
sourcepub fn new<T, B, F>(f: F) -> State<T, B>where
F: Fn(T) -> (B, T) + 'static,
pub fn new<T, B, F>(f: F) -> State<T, B>where
F: Fn(T) -> (B, T) + 'static,
Examples found in repository?
src/state.rs (line 32)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
pub fn value(a: A) -> State<S, A> {
Self::new(move |s| (a.clone(), s))
}
pub fn new<T, B, F>(f: F) -> State<T, B>
where
F: Fn(T) -> (B, T) + 'static, {
State { run_f: Rc::new(f) }
}
pub fn pure<B>(b: B) -> State<S, B>
where
B: Clone + 'static, {
Self::new(move |s| (b.clone(), s))
}
pub fn run(self, s: S) -> (A, S) {
(self.run_f)(s)
}
pub fn map<B, F>(self, f: F) -> State<S, B>
where
F: Fn(A) -> B + 'static,
B: Clone + 'static, {
self.flat_map(move |a| Self::pure(f(a)))
}
pub fn and_then<B>(self, sb: State<S, B>) -> State<S, (A, B)>
where
A: Clone,
B: Clone + 'static, {
self.flat_map(move |a| sb.clone().flat_map(move |b| Self::pure((a.clone(), b))))
}
pub fn flat_map<B, F>(self, f: F) -> State<S, B>
where
F: Fn(A) -> State<S, B> + 'static,
B: Clone + 'static, {
State::<S, B>::new(move |s| {
let (a, s1) = self.clone().run(s);
f(a).run(s1)
})
}
pub fn modify<T, F>(f: F) -> State<T, ()>
where
F: Fn(T) -> T + 'static,
T: Clone + 'static, {
let s = Self::get();
s.flat_map(move |t: T| Self::set(f(t)))
}
pub fn get<T>() -> State<T, T>
where
T: Clone, {
Self::new(move |t: T| (t.clone(), t))
}
pub fn set<T>(t: T) -> State<T, ()>
where
T: Clone + 'static, {
Self::new(move |_| ((), t.clone()))
}
pub fn sequence(sas: Vec<State<S, A>>) -> State<S, Vec<A>> {
Self::new(move |s| {
let mut s_ = s;
let actions = sas.clone();
let mut acc: Vec<A> = vec![];
for x in actions.into_iter() {
let (a, s2) = x.run(s_);
s_ = s2;
acc.push(a);
}
(acc, s_)
})
}
}
impl<S, A> Default for State<S, A>
where
S: Default + 'static,
A: Default + Clone + 'static,
{
fn default() -> Self {
Self::new(|_| (A::default(), S::default()))
}More examples
src/gen.rs (line 113)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
pub fn one_i64() -> Gen<i64> {
Gen {
sample: State::<RNG, i64>::new(move |rng: RNG| rng.next_i64()),
}
}
/// Generates a Gen that returns a single value of type u64.<br/>
/// u64型の値を一つ返すGenを生成します。
pub fn one_u64() -> Gen<u64> {
Gen {
sample: State::<RNG, u64>::new(move |rng: RNG| rng.next_u64()),
}
}
/// Generates a Gen that returns a single value of type i32.<br/>
/// i32型の値を一つ返すGenを生成します。
pub fn one_i32() -> Gen<i32> {
Gen {
sample: State::<RNG, i32>::new(move |rng: RNG| rng.next_i32()),
}
}
/// Generates a Gen that returns a single value of type u32.<br/>
/// u32型の値を一つ返すGenを生成します。
pub fn one_u32() -> Gen<u32> {
Gen {
sample: State::<RNG, u16>::new(move |rng: RNG| rng.next_u32()),
}
}
/// Generates a Gen that returns a single value of type i16.<br/>
/// i16型の値を一つ返すGenを生成します。
pub fn one_i16() -> Gen<i16> {
Gen {
sample: State::<RNG, i16>::new(move |rng: RNG| rng.next_i16()),
}
}
/// Generates a Gen that returns a single value of type u16.<br/>
/// u16型の値を一つ返すGenを生成します。
pub fn one_u16() -> Gen<u16> {
Gen {
sample: State::<RNG, u32>::new(move |rng: RNG| rng.next_u16()),
}
}
/// Generates a Gen that returns a single value of type i8.<br/>
/// i8型の値を一つ返すGenを生成します。
pub fn one_i8() -> Gen<i8> {
Gen {
sample: State::<RNG, i8>::new(move |rng: RNG| rng.next_i8()),
}
}
/// Generates a Gen that returns a single value of type u8.<br/>
/// u8型の値を一つ返すGenを生成します。
pub fn one_u8() -> Gen<u8> {
Gen {
sample: State::<RNG, u8>::new(move |rng: RNG| rng.next_u8()),
}
}
/// Generates a Gen that returns a single value of type char.<br/>
/// char型の値を一つ返すGenを生成します。
pub fn one_char() -> Gen<char> {
Self::one_u8().map(|v| v as char)
}
/// Generates a Gen that returns a single value of type bool.<br/>
/// bool型の値を一つ返すGenを生成します。
pub fn one_bool() -> Gen<bool> {
Gen {
sample: State::<RNG, bool>::new(|rng: RNG| rng.next_bool()),
}
}
/// Generates a Gen that returns a single value of type f64.<br/>
/// f64型の値を一つ返すGenを生成します。
pub fn one_f64() -> Gen<f64> {
Gen {
sample: State::<RNG, f64>::new(move |rng: RNG| rng.next_f64()),
}
}
/// Generates a Gen that returns a single value of type f32.<br/>
/// f32型の値を一つ返すGenを生成します。
pub fn one_f32() -> Gen<f32> {
Gen {
sample: State::<RNG, f32>::new(move |rng: RNG| rng.next_f32()),
}
}
/// Generates a Gen that returns a value selected at random from a specified set of Gen.<br/>
/// 指定されたGenの集合からランダムに一つ選択した値を返すGenを生成します。
pub fn one_of<T: Choose + Clone + 'static>(values: impl IntoIterator<Item = Gen<T>>) -> Gen<T> {
let mut vec = vec![];
vec.extend(values.into_iter());
Self::choose(0usize, vec.len() - 1).flat_map(move |idx| vec[idx as usize].clone())
}
/// Generates a Gen that returns one randomly selected value from the specified set of values.<br/>
/// 指定された値の集合からランダムに一つ選択した値を返すGenを生成します。
pub fn one_of_values<T: Choose + Clone + 'static>(values: impl IntoIterator<Item = T>) -> Gen<T> {
Self::one_of(values.into_iter().map(Gens::pure))
}
/// Generates a Gen that returns one randomly selected value from the specified maximum and minimum ranges of generic type.<br/>
/// 指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose<T: Choose>(min: T, max: T) -> Gen<T> {
Choose::choose(min, max)
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type char.<br/>
/// char型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_char(min: char, max: char) -> Gen<char> {
let chars = (min..=max).into_iter().map(|e| Self::pure(e)).collect::<Vec<_>>();
Self::one_of(chars)
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i64.<br/>
/// i64型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i64(min: i64, max: i64) -> Gen<i64> {
Gen {
sample: State::<RNG, i64>::new(move |rng: RNG| rng.next_i64()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u64.<br/>
/// u64型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u64(min: u64, max: u64) -> Gen<u64> {
Gen {
sample: State::<RNG, u64>::new(move |rng: RNG| rng.next_u64()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i32.<br/>
/// i32型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i32(min: i32, max: i32) -> Gen<i32> {
Gen {
sample: State::<RNG, i32>::new(move |rng: RNG| rng.next_i32()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u32.<br/>
/// u32型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u32(min: u32, max: u32) -> Gen<u32> {
Gen {
sample: State::<RNG, u32>::new(move |rng: RNG| rng.next_u32()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i16.<br/>
/// i16型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i16(min: i16, max: i16) -> Gen<i16> {
Gen {
sample: State::<RNG, i16>::new(move |rng: RNG| rng.next_i16()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u16.<br/>
/// u16型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u16(min: u16, max: u16) -> Gen<u16> {
Gen {
sample: State::<RNG, u16>::new(move |rng: RNG| rng.next_u16()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i8.<br/>
/// i8型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i8(min: i8, max: i8) -> Gen<i8> {
Gen {
sample: State::<RNG, i8>::new(move |rng: RNG| rng.next_i8()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u8.<br/>
/// u8型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u8(min: u8, max: u8) -> Gen<u8> {
Gen {
sample: State::<RNG, u8>::new(move |rng: RNG| rng.next_u8()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type f64.<br/>
/// f64型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_f64(min: f64, max: f64) -> Gen<f64> {
Gen {
sample: State::<RNG, f64>::new(move |rng: RNG| rng.next_f64()),
}
.map(move |d| min + d * (max - min))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type f32.<br/>
/// f32型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_f32(min: f32, max: f32) -> Gen<f32> {
Gen {
sample: State::<RNG, f32>::new(move |rng: RNG| rng.next_f32()),
}
.map(move |d| min + d * (max - min))
}sourcepub fn pure<B>(b: B) -> State<S, B>where
B: Clone + 'static,
pub fn pure<B>(b: B) -> State<S, B>where
B: Clone + 'static,
Examples found in repository?
src/state.rs (line 55)
51 52 53 54 55 56 57 58 59 60 61 62 63
pub fn map<B, F>(self, f: F) -> State<S, B>
where
F: Fn(A) -> B + 'static,
B: Clone + 'static, {
self.flat_map(move |a| Self::pure(f(a)))
}
pub fn and_then<B>(self, sb: State<S, B>) -> State<S, (A, B)>
where
A: Clone,
B: Clone + 'static, {
self.flat_map(move |a| sb.clone().flat_map(move |b| Self::pure((a.clone(), b))))
}sourcepub fn run(self, s: S) -> (A, S)
pub fn run(self, s: S) -> (A, S)
Examples found in repository?
More examples
src/state.rs (line 70)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
pub fn flat_map<B, F>(self, f: F) -> State<S, B>
where
F: Fn(A) -> State<S, B> + 'static,
B: Clone + 'static, {
State::<S, B>::new(move |s| {
let (a, s1) = self.clone().run(s);
f(a).run(s1)
})
}
pub fn modify<T, F>(f: F) -> State<T, ()>
where
F: Fn(T) -> T + 'static,
T: Clone + 'static, {
let s = Self::get();
s.flat_map(move |t: T| Self::set(f(t)))
}
pub fn get<T>() -> State<T, T>
where
T: Clone, {
Self::new(move |t: T| (t.clone(), t))
}
pub fn set<T>(t: T) -> State<T, ()>
where
T: Clone + 'static, {
Self::new(move |_| ((), t.clone()))
}
pub fn sequence(sas: Vec<State<S, A>>) -> State<S, Vec<A>> {
Self::new(move |s| {
let mut s_ = s;
let actions = sas.clone();
let mut acc: Vec<A> = vec![];
for x in actions.into_iter() {
let (a, s2) = x.run(s_);
s_ = s2;
acc.push(a);
}
(acc, s_)
})
}sourcepub fn map<B, F>(self, f: F) -> State<S, B>where
F: Fn(A) -> B + 'static,
B: Clone + 'static,
pub fn map<B, F>(self, f: F) -> State<S, B>where
F: Fn(A) -> B + 'static,
B: Clone + 'static,
Examples found in repository?
src/gen.rs (line 385)
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
pub fn map<B, F>(self, f: F) -> Gen<B>
where
F: Fn(A) -> B + 'static,
B: Clone + 'static, {
Self::new(self.sample.map(f))
}
/// Applies a function that takes the result of two Gen's as arguments.<br/>
/// 二つのGenの結果を引数に取る関数を適用します。
pub fn and_then<B, C, F>(self, g: Gen<B>, f: F) -> Gen<C>
where
F: Fn(A, B) -> C + 'static,
A: Clone,
B: Clone + 'static,
C: Clone + 'static, {
Self::new(self.sample.and_then(g.sample).map(move |(a, b)| f(a, b)))
}More examples
src/machine.rs (line 28)
17 18 19 20 21 22 23 24 25 26 27 28 29
fn simulate_machine(inputs: Vec<Input>) -> State<Machine, (i32, i32)> {
let xs = inputs
.into_iter()
.map(move |i| {
let uf: Box<dyn Fn(Input) -> Box<dyn Fn(Machine) -> Machine>> = Self::update();
let r: Box<dyn Fn(Machine) -> Machine> = uf(i);
State::<Machine, ()>::modify(move |m: Machine| r(m))
})
.collect::<Vec<_>>();
let result = State::sequence(xs);
result.flat_map(|_| State::<Machine, Machine>::get().map(|s: Machine| (s.coins, s.candies)))
}sourcepub fn and_then<B>(self, sb: State<S, B>) -> State<S, (A, B)>where
A: Clone,
B: Clone + 'static,
pub fn and_then<B>(self, sb: State<S, B>) -> State<S, (A, B)>where
A: Clone,
B: Clone + 'static,
sourcepub fn flat_map<B, F>(self, f: F) -> State<S, B>where
F: Fn(A) -> State<S, B> + 'static,
B: Clone + 'static,
pub fn flat_map<B, F>(self, f: F) -> State<S, B>where
F: Fn(A) -> State<S, B> + 'static,
B: Clone + 'static,
Examples found in repository?
src/state.rs (line 55)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
pub fn map<B, F>(self, f: F) -> State<S, B>
where
F: Fn(A) -> B + 'static,
B: Clone + 'static, {
self.flat_map(move |a| Self::pure(f(a)))
}
pub fn and_then<B>(self, sb: State<S, B>) -> State<S, (A, B)>
where
A: Clone,
B: Clone + 'static, {
self.flat_map(move |a| sb.clone().flat_map(move |b| Self::pure((a.clone(), b))))
}
pub fn flat_map<B, F>(self, f: F) -> State<S, B>
where
F: Fn(A) -> State<S, B> + 'static,
B: Clone + 'static, {
State::<S, B>::new(move |s| {
let (a, s1) = self.clone().run(s);
f(a).run(s1)
})
}
pub fn modify<T, F>(f: F) -> State<T, ()>
where
F: Fn(T) -> T + 'static,
T: Clone + 'static, {
let s = Self::get();
s.flat_map(move |t: T| Self::set(f(t)))
}More examples
src/machine.rs (line 28)
17 18 19 20 21 22 23 24 25 26 27 28 29
fn simulate_machine(inputs: Vec<Input>) -> State<Machine, (i32, i32)> {
let xs = inputs
.into_iter()
.map(move |i| {
let uf: Box<dyn Fn(Input) -> Box<dyn Fn(Machine) -> Machine>> = Self::update();
let r: Box<dyn Fn(Machine) -> Machine> = uf(i);
State::<Machine, ()>::modify(move |m: Machine| r(m))
})
.collect::<Vec<_>>();
let result = State::sequence(xs);
result.flat_map(|_| State::<Machine, Machine>::get().map(|s: Machine| (s.coins, s.candies)))
}sourcepub fn modify<T, F>(f: F) -> State<T, ()>where
F: Fn(T) -> T + 'static,
T: Clone + 'static,
pub fn modify<T, F>(f: F) -> State<T, ()>where
F: Fn(T) -> T + 'static,
T: Clone + 'static,
Examples found in repository?
src/machine.rs (line 23)
17 18 19 20 21 22 23 24 25 26 27 28 29
fn simulate_machine(inputs: Vec<Input>) -> State<Machine, (i32, i32)> {
let xs = inputs
.into_iter()
.map(move |i| {
let uf: Box<dyn Fn(Input) -> Box<dyn Fn(Machine) -> Machine>> = Self::update();
let r: Box<dyn Fn(Machine) -> Machine> = uf(i);
State::<Machine, ()>::modify(move |m: Machine| r(m))
})
.collect::<Vec<_>>();
let result = State::sequence(xs);
result.flat_map(|_| State::<Machine, Machine>::get().map(|s: Machine| (s.coins, s.candies)))
}sourcepub fn get<T>() -> State<T, T>where
T: Clone,
pub fn get<T>() -> State<T, T>where
T: Clone,
Examples found in repository?
More examples
src/machine.rs (line 28)
17 18 19 20 21 22 23 24 25 26 27 28 29
fn simulate_machine(inputs: Vec<Input>) -> State<Machine, (i32, i32)> {
let xs = inputs
.into_iter()
.map(move |i| {
let uf: Box<dyn Fn(Input) -> Box<dyn Fn(Machine) -> Machine>> = Self::update();
let r: Box<dyn Fn(Machine) -> Machine> = uf(i);
State::<Machine, ()>::modify(move |m: Machine| r(m))
})
.collect::<Vec<_>>();
let result = State::sequence(xs);
result.flat_map(|_| State::<Machine, Machine>::get().map(|s: Machine| (s.coins, s.candies)))
}sourcepub fn sequence(sas: Vec<State<S, A>>) -> State<S, Vec<A>>
pub fn sequence(sas: Vec<State<S, A>>) -> State<S, Vec<A>>
Examples found in repository?
More examples
src/machine.rs (line 27)
17 18 19 20 21 22 23 24 25 26 27 28 29
fn simulate_machine(inputs: Vec<Input>) -> State<Machine, (i32, i32)> {
let xs = inputs
.into_iter()
.map(move |i| {
let uf: Box<dyn Fn(Input) -> Box<dyn Fn(Machine) -> Machine>> = Self::update();
let r: Box<dyn Fn(Machine) -> Machine> = uf(i);
State::<Machine, ()>::modify(move |m: Machine| r(m))
})
.collect::<Vec<_>>();
let result = State::sequence(xs);
result.flat_map(|_| State::<Machine, Machine>::get().map(|s: Machine| (s.coins, s.candies)))
}