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 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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
use std::{fmt::Debug, ops::Add, iter::Product}; use rayon::{iter::Either, prelude::*}; /// This trait is to implement some extension functions, /// which need a generic return type, for any sized type. pub trait KtStd<R>: Sized { /// Performs operation `f` with `&self`, returns the closure result. /// /// Moves environment variable(s) to closure by default. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// String::from("Hello") /// .let_ref(|s| format!("String is: {}", s)) /// .echo(); /// ``` fn let_ref<'a>(&'a self, f: impl FnOnce(&'a Self) -> R) -> R { f(self) } /// Performs operation `f` with `&mut self`, returns the closure result. /// /// Moves environment variable(s) to closure by default. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// vec!["a", "b", "c"] /// .let_mut(|v| { v.push("d"); format!("Vector is: {:?}", v) }) /// .echo(); /// ``` fn let_mut<'a>(&'a mut self, f: impl FnOnce(&'a mut Self) -> R) -> R { f(self) } /// Consumes `self`, performs operation `f` with it, returns the closure result. /// /// Moves environment variable(s) to closure by default. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// vec![9, 5, 3, 6, 1] /// .let_owned(|v| v.sort()) // parallel sort, return self /// .echo(); /// ``` fn let_owned(self, f: impl FnOnce(Self) -> R) -> R { f(self) } /// Consumes `self`, performs operation `f` with it, returns the receiver. /// /// Moves environment variable(s) to closure by default. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// vec!["Hello", "Rust"] /// .also_ref(|v| println!("Vector is: {:?}", v)) /// .for_each(|s| s.echo()); /// ``` fn also_ref(self, f: impl FnOnce(&Self) -> R) -> Self { f(&self); self } /// Consumes `self`, performs operation `f` on it, returns the updated value. /// /// Moves environment variable(s) to closure by default. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// vec!["Hello", "Kotlin"] /// .also_mut(|v| v[1] = "Rust") /// .echo(); /// ``` fn also_mut(mut self, f: impl FnOnce(&mut Self) -> R) -> Self { f(&mut self); self } /// The Y Combinator /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// fn fact(n: usize) -> usize { /// n.y(|f, n| match n { /// 0 | 1 => 1, /// n => n * f(n - 1), /// }) /// } /// assert_eq!(fact(5), 5 * 4 * 3 * 2 * 1); /// /// fn fibonacci(n: usize) -> usize { /// n.y(|f, n| match n { /// 0 => 0, /// 1 => 1, /// n => f(n - 1) + f(n - 2), /// }) /// } /// assert_eq!(fibonacci(10), 55); /// ``` fn y(self, f: impl Copy + Fn(&dyn Fn(Self) -> R, Self) -> R) -> R { use super::std_fun::y; y(f)(self) } } impl<T, R> KtStd<R> for T {} /// This trait is to implement some extension functions for any sized type. pub trait Ext: Sized { /// Chainable `drop` fn drop(self) {} /// System output -> with `:#?`'s `println!`. /// /// Consumes `self`, `println!` with `:#?`, returns `self`. fn sout(self) -> Self where Self: Debug { self.also_ref(|s| println!("{:#?}", s)) } /// Consumes `self`, `println!` as it is, returns `self`. fn echo(self) -> Self where Self: Debug { self.also_ref(|s| println!("{:?}", s)) } /// Returns the name of a type as a string slice. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!("".type_name(), "&str"); // auto ref, auto deref. /// assert_eq!((&"").type_name(), "&str"); // auto deref. /// assert_eq!((&&"").type_name(), "&&str"); /// ``` fn type_name(&self) -> &str { std::any::type_name::<Self>() } /// Returns the size of a type in bytes. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(().type_size(), 0); // auto ref, auto deref. /// assert_eq!((&()).type_size(), 0); // auto deref. /// assert_eq!((&&()).type_size(), 8); /// ``` fn type_size(&self) -> usize { std::mem::size_of::<Self>() } /// Returns `Some(self)` if it satisfies the given predicate function, /// or `None` if it doesn't. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(Some("Hello"), "Hello".take_if(|s| s.starts_with("Hel"))); /// assert_eq!(None, "Hello".take_if(|s| s.starts_with("Wor"))) /// ``` fn take_if(self, f: impl FnOnce(&Self) -> bool) -> Option<Self> { if f(&self) { Some(self) } else { None } } /// Returns `Some(self)` if it doesn't satisfy the given predicate function, /// or `None` if it does. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(None, "Hello".take_unless(|s| s.starts_with("Hel"))); /// assert_eq!(Some("Hello"), "Hello".take_unless(|s| s.starts_with("Wor"))) /// ``` fn take_unless(self, f: impl FnOnce(&Self) -> bool) -> Option<Self> { if !f(&self) { Some(self) } else { None } } } impl<T> Ext for T {} /// This trait is to implement some extension functions for `bool` type. pub trait BoolExt<R> { fn if_true(self, f: impl FnOnce() -> R) -> Option<R>; fn if_false(self, f: impl FnOnce() -> R) -> Option<R>; } impl<R> BoolExt<R> for bool { /// Chainable `if`, execute when the condition is `true` /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// let s = "Hello World"; /// assert_eq!(Some("lo Wo"), s.starts_with("Hel").if_true(|| &s[3..8])); /// assert_eq!(None, s.starts_with("Wor").if_true(|| &s[3..8])); /// ``` fn if_true(self, f: impl FnOnce() -> R) -> Option<R> { if self { Some(f()) } else { None } } /// Chainable `if`, execute when the condition is `false` /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// let s = "Hello World"; /// assert_eq!(None, s.starts_with("Hel").if_false(|| &s[3..8])); /// assert_eq!(Some("lo Wo"), s.starts_with("Wor").if_false(|| &s[3..8])); /// ``` fn if_false(self, f: impl FnOnce() -> R) -> Option<R> { if !self { Some(f()) } else { None } } } /// This trait is to implement some extension functions for `u128` type. pub trait U128Ext { fn fmt_size_from(self, unit: char) -> String; } impl U128Ext for u128 { /// Human readable storage unit. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(String::from("32.0 G"), 33554432.fmt_size_from('K')); /// ``` fn fmt_size_from(self, unit: char) -> String { let units = ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']; let mut size = self as f64; let mut counter = 0; while size >= 1024.0 { size /= 1024.0; counter += 1; } for (i, &c) in units.iter().enumerate() { if c == unit { counter += i; break; } } format!("{:.1} {}", size, units.get(counter).unwrap_or_else(|| panic!("memory unit out of bounds"))) } } /// This trait is to implement some extension functions whose type is `FnOnce`. pub trait FnOnceExt<P1, P2, R> { fn partial2(self, p2: P2) -> Box<dyn FnOnce(P1) -> R>; } impl<P1, P2: 'static, R, F> FnOnceExt<P1, P2, R> for F where F: FnOnce(P1, P2) -> R + 'static { /// Two parameters, currying from right to left. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// fn foo(a: u8, b: u8) -> u8 { a - b } /// assert_eq!(foo.partial2(2)(3), 1); /// ``` fn partial2(self, p2: P2) -> Box<dyn FnOnce(P1) -> R> { Box::new(move |p1| self(p1, p2)) } } pub trait Sort<T> { fn sort(self) -> Vec<T>; } impl<T: Ord + Send> Sort<T> for Vec<T> { /// Sorts `Vec` in parallel. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// vec![7, 4, 9, 2].sort().echo(); /// ``` fn sort(self) -> Vec<T> { self.also_mut(|v| v.par_sort()) } } /// This trait is to implement some extension functions for `Vec` type, /// which need two generic types and reference. pub trait VecExt2<'a, T, R> where T: 'a { fn map(&'a self, f: impl Fn(&'a T) -> R + Sync + Send) -> Vec<R>; fn filter_map(&'a self, f: impl Fn(&'a T) -> Option<R> + Sync + Send) -> Vec<R>; } impl<'a, T, R> VecExt2<'a, T, R> for Vec<T> where T: 'a + Sync, R: Send { /// Returns a `Vec` containing the results of applying the given `f` function /// to each element in the original `Vec`. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(vec![1, 2, 3].map(|i| i + 1), vec![2, 3, 4]); /// ``` fn map(&'a self, f: impl Fn(&'a T) -> R + Sync + Send) -> Vec<R> { self.par_iter().map(f).collect() } /// A combined map and filter. Filtering is handled via `Option` instead of `Boolean` /// such that the output type `R` can be different than the input type `T`. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// vec!["apple", "1", "banana", "2"] /// .filter_map(|s| s.parse::<u8>().ok()) /// .let_owned(|v| assert_eq!(v, vec![1, 2])); /// ``` fn filter_map(&'a self, f: impl Fn(&'a T) -> Option<R> + Sync + Send) -> Vec<R> { self.par_iter().filter_map(f).collect() } } /// This trait is to implement some extension functions for `Vec` type, /// which need one generic type, and do not need reference. pub trait VecExt1<T> { fn for_each<R>(self, f: impl Fn(T) -> R + Sync + Send); fn on_each<R>(self, f: impl Fn(&mut T) -> R + Sync + Send) -> Self; fn filter(self, f: impl Fn(&T) -> bool + Sync + Send) -> Vec<T>; fn fold(self, init: T, f: impl Fn(T, T) -> T + Sync + Send) -> T where T: Sync + Copy; fn reduce(self, f: impl Fn(T, T) -> T + Sync + Send) -> T where T: Sync + Copy + Default; fn sum(self) -> T where T: Sync + Copy + Default + Add<Output = T>; fn product(self) -> T where T: Product; fn partition(self, f: impl Fn(&T) -> bool + Sync + Send) -> (Vec<T>, Vec<T>); fn partition3(self, predicate1: impl Fn(&T) -> bool + Sync + Send, predicate2: impl Fn(&T) -> bool + Sync + Send) -> (Vec<T>, Vec<T>, Vec<T>) where T: Sync; } impl<T> VecExt1<T> for Vec<T> where T: Send { /// Executes `f` on each item produced by the iterator for `Vec` in parallel. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// vec![String::from("abc"), String::from("xyz")] /// .for_each(|e| e.echo()); /// ``` fn for_each<R>(self, f: impl Fn(T) -> R + Sync + Send) { self.into_par_iter().for_each(|e| { f(e); }); } /// Performs the given `f` on each element in parallel, and returns the `Vec` itself afterwards. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(vec!["hello", "rust"].on_each(|s| *s.echo()), vec!["hello", "rust"]); /// ``` fn on_each<R>(self, f: impl Fn(&mut T) -> R + Sync + Send) -> Self { self.also_mut(|v| v.par_iter_mut().for_each(|e| { f(e); })) } /// Returns a `Vec` containing only elements matching the given `f` predicate in parallel. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(vec![1, 2, 3, 4].filter(|i| i % 2 == 0), vec![2, 4]); /// ``` fn filter(self, f: impl Fn(&T) -> bool + Sync + Send) -> Vec<T> { self.into_par_iter().filter(f).collect() } /// Accumulates value starting with initial value, /// and applying operation `f` from left to right in parallel. /// /// Returns the specified initial value if the `Vec` is empty. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(vec![1, 2, 3, 4].fold(0, |acc, i| acc + i), 10); /// ``` fn fold(self, init: T, f: impl Fn(T, T) -> T + Sync + Send) -> T where T: Sync + Copy { self.into_par_iter().reduce(|| init, f) } /// Accumulates value starting with default value, /// and applying operation `f` from left to right in parallel. /// /// Returns the default value if the `Vec` is empty. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(vec![1, 2, 3, 4].reduce(|acc, i| acc + i), 10); /// ``` fn reduce(self, f: impl Fn(T, T) -> T + Sync + Send) -> T where T: Sync + Copy + Default { self.fold(T::default(), f) } /// Returns the sum of all elements /// which implement the `Add` trait in parallel for `Vec` type. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(vec![1, 2, 3, 4].sum(), 10); /// ``` fn sum(self) -> T where T: Sync + Copy + Default + Add<Output = T> { self.reduce(|a, b| a + b) } /// Returns the product of all elements /// which implement the `Product` trait in parallel for `Vec` type. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(vec![1, 2, 3, 4].product(), 24); /// ``` fn product(self) -> T where T: Product { self.into_par_iter().product() } /// Splits the original `Vec` into a couple of `Vec` according to the condition, /// where first `Vec` contains elements for which predicate yielded true, /// while second `Vec` contains elements for which predicate yielded false. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(vec![1, 2, 3, 4].partition(|i| i % 2 != 0), (vec![1, 3], vec![2, 4])); /// ``` fn partition(self, f: impl Fn(&T) -> bool + Sync + Send) -> (Vec<T>, Vec<T>) { self.into_par_iter().partition(f) } /// Splits the original `Vec` into a triple of `Vec` according to the condition, /// where first `Vec` contains elements for first predicate yielded true, /// where second `Vec` contains elements for second predicate yielded true, /// while third `Vec` contains elements for which two predicates yielded false. /// /// # Examples /// /// ``` /// use aoko::std_ext::*; /// /// assert_eq!(vec![1, 2, 3].partition3(|i| i < &2, |i| i == &2), (vec![1], vec![2], vec![3])); /// ``` fn partition3(self, predicate1: impl Fn(&T) -> bool + Sync + Send, predicate2: impl Fn(&T) -> bool + Sync + Send) -> (Vec<T>, Vec<T>, Vec<T>) where T: Sync { let ((first, second), third) = self.into_par_iter().partition_map(|e| if predicate1(&e) { Either::Left(Either::Left(e)) } else if predicate2(&e) { Either::Left(Either::Right(e)) } else { Either::Right(e) } ); (first, second, third) } }