To Code and Beyond: A Neverland Adventure in Bash, Lua, Python, and Rust
source link: https://dev.to/proteusiq/to-code-and-beyond-a-neverland-adventure-in-bash-lua-python-and-rust-1jon
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK