1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 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
#![warn(missing_docs)] //! Small tools to work with rust reducing //! mutability and programming more functional //! way. //! //! Mutability and sharing is bad. //! //! Viric mutability is bad. //! //! Mutability on large pieces of code is bad. //! //! //! In Rust, we can have no viric, not shared //! mutability. Let's reduce to small scopes... //! //! //! For an introduction and context view, read... //! //! [README.md](https://github.com/jleahred/idata) //! //! A very basic example... //! //! ```rust //! extern crate idata; //! use idata::cont::IVec; //! //! fn main() { //! let v = vec![1, 2]; //! let v = v.ipush(3) //! .ipush(4); //! //! assert!(v == vec![1,2,3,4]); //! } //!``` //! /// Try getting a char from top of a Chars /// returning the (char, remaining_chars) if possible /// /// ```rust /// extern crate idata; /// /// fn main() { /// let chars = "Hello world".chars(); /// let (ch, chars) = idata::consume_char(chars).unwrap(); /// /// assert!(ch == 'H'); /// assert!(chars.as_str() == "ello world"); /// } ///``` pub fn consume_char(mut chars: std::str::Chars) -> Option<(char, std::str::Chars)> { match chars.next() { Some(ch) => Some((ch, chars)), None => None, } } pub mod cont { //! Module to work with containers /// Some operations to work with vectors /// pub trait IVec<T> { /// Push an element to a vector, and return the same vector /// /// ```rust /// extern crate idata; /// use idata::cont::IVec; /// /// fn main() { /// let v = vec![1, 2]; /// let v = v.ipush(3) /// .ipush(4); /// /// assert!(v == vec![1,2,3,4]); /// } ///``` fn ipush(self, T) -> Self; /// Append a vector to another /// /// ```rust /// extern crate idata; /// use idata::cont::IVec; /// /// fn main() { /// let v1 = vec![1, 2]; /// let v2 = vec![3, 4, 5]; /// let v1 = v1.iappend(v2); /// /// assert!(v1 == vec![1,2,3,4, 5]); /// } ///``` fn iappend(self, Vec<T>) -> Self; /// Remove an element from back of a vector /// /// ```rust /// extern crate idata; /// use idata::cont::IVec; /// /// fn main() { /// let v1 = vec![1, 2, 3, 4, 5, 6]; /// let (o, v1) = v1.ipop(); /// /// assert!(v1 == vec![1,2,3,4, 5]); /// assert!(o.unwrap() == 6); /// } ///``` fn ipop(self) -> (Option<T>, Self); } impl<T> IVec<T> for Vec<T> { fn ipush(mut self, v: T) -> Self { self.push(v); self } fn iappend(mut self, mut v: Vec<T>) -> Self { self.append(&mut v); self } fn ipop(mut self) -> (Option<T>, Self) { (self.pop(), self) } } } //----------------------------------------------------------------------- // TailCall //----------------------------------------------------------------------- pub mod tc { //! Recursive simulation with TCO //! //! We cannot use SSA in rust combined with a for loop //! //! It fits fine with recursion, but...alloc //! //! Rust doesn't have TCO (tail call optimization) in recursion. //! //! In some cases it could be expensive and even dangerous //! //! One option, could be to use next "trampolin" //! //! ```rust //! extern crate idata; //! use idata::tc::*; //! //! fn main() { //! //! let (sum, _) = tail_call((0, 0), |(acc, counter)| { //! if counter < 101 { //! TailCall::Call((acc + counter, counter + 1)) //! } else { //! TailCall::Return((acc, counter)) //! } //! }); //! assert!(sum == 5050); //! } //!``` /// Support to call or return from a recursive function pub enum TailCall<T, R> { /// Support to simulate a recursive call Call(T), /// Simultate a recursive return Return(R), } /// Function to simulate TCO. See example /// /// ```rust /// extern crate idata; /// use idata::tc::*; /// /// fn main() { /// /// let (sum, _) = tail_call((0, 0), |(acc, counter)| { /// if counter < 101 { /// TailCall::Call((acc + counter, counter + 1)) /// } else { /// TailCall::Return((acc, counter)) /// } /// }); /// /// assert!(sum == 5050); /// } ///``` pub fn tail_call<T, R, F>(seed: T, recursive_function: F) -> R where F: Fn(T) -> TailCall<T, R>, { let mut state = TailCall::Call(seed); loop { match state { TailCall::Call(arg) => { state = recursive_function(arg); } TailCall::Return(result) => { return result; } } } } }