Categories
Tags
algorithms APIT Arc arm assembly asynchronous base64 BitHacks Blogging box c clang-format client cmake compiler concat concurrency const_fn contravariant cos covariant cpp Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP dtruss Dynamic Example FFI flat_map format FP fsanitize Functional functions futures Fuwari GATs gccrs generics gitignore glibc GUI hacking hashmap haskell heap interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS Markdown math ML mmap nc OnceLock optimization OS panic parallels perf physics pin postgresql radare2 release reverse RPIT rust sanitizer science Science serialization server shift sin SmallProjects socket std strace String StringView strip strlen surrealdb SWAR swisstable synchronous tan toml traits triangulation UnsafeRust utf16 utf8 Video 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/