iter_python/
lib.rs

1#![cfg_attr(feature = "better-docs",
2    cfg_attr(all(), doc = include_str!("../README.md")),
3    feature(doc_cfg),
4)]
5#![cfg_attr(not(feature = "better-docs"),
6    doc = "See [crates.io](https://crates.io/crates/iter-python)"
7)]
8#![cfg_attr(not(feature = "better-docs"),
9    doc = "for more info about this crate."
10)]
11
12#![cfg_attr(not(doc),
13    no_std,
14)]
15
16#![forbid(unsafe_code)]
17
18pub mod prelude {
19    #[doc(no_inline)]
20    pub use crate::{
21        all, any,
22        macros::{
23            lazy_format as f,
24            iter as i,
25        },
26        extension_traits::{
27            Join as _,
28        },
29    };
30
31    #[doc(no_inline)]
32    #[cfg(feature = "std")]
33    #[cfg_attr(feature = "better-docs",
34        doc(cfg(feature = "std")),
35    )]
36    pub use crate::{
37        extension_traits::IteratorExt as _,
38        macros::vec as v,
39    };
40}
41
42pub
43mod extension_traits;
44
45pub
46mod macros;
47
48/// Python's `all(iterable)` function.
49///
50/// # Example
51///
52/// ```rust,edition2018
53/// use ::iter_python::*;
54///
55/// fn is_square (n: u32) -> bool
56/// {
57///     ((n as f64).sqrt().trunc() as u32).pow(2) == n
58/// }
59///
60/// let odds = || iter!(2 * n + 1 for n in 0 ..);
61///
62/// let sums_of_odds = iter!(odds().take(n).sum() for n in 1 .. 20);
63///
64/// assert!(all(iter!(is_square(sum_of_odds) for sum_of_odds in sums_of_odds)));
65/// ```
66#[inline]
67pub
68fn all (
69    iterable: impl IntoIterator<Item = bool>,
70) -> bool
71{
72    iterable
73        .into_iter()
74        .all(::core::convert::identity)
75}
76
77/// Python's `any(iterable)` function.
78///
79/// # Example
80///
81/// ```rust,edition2018
82/// use ::iter_python::*;
83///
84/// fn is_not_prime (n: usize) -> bool
85/// {
86///     any(iter!(n % k == 0 for k in (2 ..).take_while(|&k| k * k <= n)))
87/// }
88///
89/// assert!(is_not_prime(91));
90/// ```
91#[inline]
92pub
93fn any (
94    iterable: impl IntoIterator<Item = bool>,
95) -> bool
96{
97    iterable
98        .into_iter()
99        .any(::core::convert::identity)
100}
101
102#[doc(hidden)] /** Not part of the public API */ pub
103mod __ {
104    pub use core;
105
106    #[cfg(feature = "std")] pub
107    extern crate std;
108}