Prologue: Departure to Neverland
Once upon a time, in the mystical world of terminals, we find ourselves tagging along on a Peter Pan-themed odyssey, soaring across the skies of Neverland. Our quest? To discover the magic similarities and differences of the unique spirits of Bash, Lua, Python, and Rust.
Chapter 1: Aboard the Jolly Roger with Bash and Lua
Under the moonlit sky, we join Captain Hook on the Jolly Roger, navigating the traditional seas of Bash and Lua. Like seasoned sailors chanting an age-old shanty, these languages use for loops as their trusted compass.
Bash - The Captain's Command:
#!/bin/bash
sum_odd_int_array() {
local sum=0
for x in "$@"; do
if (( x % 2 != 0 )); then
(( sum+=x ))
fi
done
echo $sum
}
array=(1 2 3 4 5)
sum_odd_int_array ${array[@]}
Lua - The First Mate's Chant:
function sum_odd_int_array(array)
local sum = 0
for _, x in ipairs(array) do
if x % 2 ~= 0 then
sum = sum + x
end
end
return sum
end
local array = {1, 2, 3, 4, 5}
print(sum_odd_int_array(array))
Chapter 2: Tinker Bell's Python Whispers
In the heart of Neverland, Tinker Bell whisks us away, revealing the wonders of Python. She shows us two paths: one trodden by many, and the other, a secret trail lit by her magical glow.
The Beaten Path - Traditional For Loop:
def sum_odd_int_array_for_loop(array: list[int]) -> int:
sum = 0
for x in array:
if x % 2 != 0:
sum += x
return sum
array = [1, 2, 3, 4, 5]
print(sum_odd_int_array_for_loop(array))
Tinker Bell's Enchanted Trail - Comprehension:
def sum_odd_int_array(array: list[int]) -> int:
return sum(x for x in array if x % 2 != 0)
array = [1, 2, 3, 4, 5]
print(sum_odd_int_array(array))
Chapter 3: The Lost Boys' Rusty Innovations
Deep in Neverland's forests, the Lost Boys unveil their secret: a marvel of Rust. First, a familiar structure, echoing the old ways. Then, a creation so ingenious, it seemed woven from the threads of the future.
The Olden Design - Traditional For Loop:
fn sum_odd_int_array_for_loop(array: &[i32]) -> i32 {
let mut sum = 0;
for &x in array {
if x % 2 != 0 {
sum += x;
}
}
sum
}
fn main() {
let array = [1, 2, 3, 4, 5];
println!("{}", sum_odd_int_array_for_loop(&array));
}
The Future Woven - Iterator Method:
fn sum_odd_int_array(array: &[i32]) -> i32 {
array.iter().filter(|&&x| x % 2 != 0).sum()
}
fn main() {
let array = [1, 2, 3, 4, 5];
println!("{}", sum_odd_int_array(&array));
}
Epilogue: Magic in the Code
From the steady chants of Bash and Lua to the whimsical whispers of Python and the ingenious creations of Rust, each language brings its own spellbinding qualities. We're reminded of the magic and wonder that each language holds.
As ageless programmers on a Neverland odyssey, we discover the art of transcending traditional loops, delving into the allure of modern programming languages and their captivating syntactic sugar.
In this Neverland of code, the adventure never ends, and with each line written, we continue to weave our own magical tales.
Until then, keep on coding with πͺ andπͺs.



Top comments (24)
For me what makes the shell something unique are pipes:
A killer one-linner proves that
π€―Nice. Is there similar that takes a sequence of even and odd numbers and only sum odds?
yes:
seq 9 | awk '{($1 % 2 ? sum+=$1 : 0)} END { print sum}'Now that blows my mind π€―. ππ Is
awknot programming language by itself?Sir, your writing style is brilliant
Thank you, Antonov. I am attempting a story like telling of concepts π«’ I love telling stories.
I use mostly openjourney, bingβs Dalle-E, and Adobeβs Firefly.
WoW! That is awesome. Perhaps another article. I primary use Python π and Rust π¦, Bash and Lua π are secondary.
My main goal was not to show the power or effectiveness or to compare the languages. My aim is to show that even though we can write some of code very similar, some languages have their syntactic sugar π€.
With the goal of filtering numbers and adding, is it possible to use Bash modern way of βhiding" loops? Did I miss Bash syntactic sugar?
Great Story, Like it π₯
I am glad you did πͺππΏ
Nice catch! ππΎ The function already echo instead of
returnπ«£. And were we to return then we are limited to 0-255 πThank you ππΎ for reading and catching my blunder π€
I am cheering π£ for your success. You are awesome ππΏ
That is another level ππΏπ₯³ and pure genius.
Nice! Are you using LLMs under the hood?
Oh no! I am not suggesting for better-ness. I just thought I missed something in Bash π