Skip to content

Instantly share code, notes, and snippets.

@MrDwarf7
Created November 30, 2023 22:32
Show Gist options
  • Save MrDwarf7/2e8b9e22f11d278315460fc7fd2b92c0 to your computer and use it in GitHub Desktop.
Save MrDwarf7/2e8b9e22f11d278315460fc7fd2b92c0 to your computer and use it in GitHub Desktop.
Overly convoluted Language demonstrations
class PrimeCheck:
def __init__(self, number):
self.number = number
def __bool__(self):
is_prime = lambda x: x > 1 and all(x % i for i in range(2, int(x**0.5) + 1))
return is_prime(self.number)
def __repr__(self):
return f"PrimeCheck({self.number}) is {'Prime' if self else 'Not Prime'}"
def prime_checker(*args):
return [eval('__import__("functools").reduce(lambda x, y: x(y), args, PrimeCheck)')]
# Usage example
print(prime_checker(29, lambda x: PrimeCheck(x.number ** 2), lambda x: PrimeCheck(x.number // 28)))
#![allow(unused)]
fn main() {
struct Factorial<T>(T);
impl<T> Factorial<T>
where
T: IntoIterator,
T::Item: std::ops::Mul<Output = T::Item> + Copy,
{
fn calc(self) -> T::Item {
self.0.into_iter().fold(1 as T::Item, |acc, x| acc * x)
}
}
let factorial = |n: u32| -> Factorial<_> {
Factorial((1..=n).map(|x| {
std::iter::repeat(x).take(x as usize).fold(1, |acc, y| acc * y)
}))
};
println!("{}", factorial(5).calc());
}
type MaybePromise<T> = T | Promise<T>;
type ConfusedFunction = <T>(arg: MaybePromise<T>) => Promise<MaybePromise<T>>;
const fetchData: ConfusedFunction = async (data) => {
const resolveData = await new Promise(resolve => setTimeout(() => resolve(data), 1000));
return typeof resolveData === 'number' ? Promise.resolve(resolveData) : resolveData;
};
(async () => {
const confusingArray = [fetchData(10), Promise.resolve(20), 30];
for (const element of confusingArray) {
console.log(await (await fetchData(element) as Promise<number>));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment