Categories
Tags
3d algorithms alignment analyze APIT Arc Architecture arm ascii assembly asynchronous base64 BitHacks Blogging box c c23 clang clang-format client clippy cmake compiler Computer concat concurrency const_fn constexpr contravariant cos covariant cpp cpu crate CS Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP drawio dtruss Dynamic emulator example Example FFI flamegraph flat_map fold format FP fsanitize Functional FunctionalProgramming functions futures Fuwari game GATs gcc gccrs generics gitignore glibc GUI hacking hashmap haskell heap hyperfine Imperative interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS map Markdown math ML mmap mod nc OnceLock optimization OS ownership panic parallels perf physics pin postgresql product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift sin size SmallProjects socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous tan thread time toml tracing traits triangulation uint32_t UnsafeRust utf16 utf8 Video vulkan wsl x86_64 xilem zig
200 words
1 minutes
240701_for_all_vs_for_some
link
1. The core idea (logic symbols)|🔝|
| English phrase | Logical quantifier | Symbol |
|---|---|---|
| for some | existential | ∃ |
| for all | universal | ∀ |
2. Formal mathematical meaning|🔝|
- 🔹 “For some”
There exists at least one element that satisfies the condition.
- 🔹 “For all”
Every element must satisfy the condition.
3. Programming intuition (Rust-style)|🔝|
For some → any()
let exists = xs.iter().any(|x| x % 2 == 0);Stop early on FIRST trueFor all → all()
let all = xs.iter().all(|x| x % 2 == 0);Stop early on FIRST false
4. Rust 코드로 실습|🔝|
use std::ops::Rem;
fn for_some<T>(x: &[T]) -> bool
where
T: Copy + Rem<T, Output = T> + PartialEq<T> + From<u8>,
{
if x.iter().any(|xi| *xi % T::from(2) == T::from(0)) {
true
} else {
false
}
}
fn for_all<T>(x: &[T]) -> bool
where
T: Copy + Rem<T, Output = T> + PartialEq<T> + From<u8>,
{
if x.iter().all(|xi| *xi % T::from(2) == T::from(0)) {
true
} else {
false
}
}
fn main() {
let a = vec![1, 2, 3, 4, 5];
let b = vec![1, 2, 3, 4, 5];
let is_some = for_some(&a);
println!("a is some : {is_some}");
let is_all = for_all(&b);
println!("b is all : {is_all}");
}- result
a is some : true
b is all : false240701_for_all_vs_for_some
https://younghakim7.github.io/blog/posts/240701_for_all_vs_for_some/