DEV Community

Cover image for To Code and Beyond: A Neverland Adventure in Bash, Lua, Python, and Rust
Prayson Wilfred Daniel
Prayson Wilfred Daniel

Posted on • Edited on

To Code and Beyond: A Neverland Adventure in Bash, Lua, Python, and Rust

ship

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[@]}
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

peter

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))
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

rust

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));
}
Enter fullscreen mode Exit fullscreen mode

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));
}
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
voyeg3r profile image
SΓ©rgio AraΓΊjo

For me what makes the shell something unique are pipes:

seq -s+ 1 2 5 | bc
Enter fullscreen mode Exit fullscreen mode

A killer one-linner proves that

Collapse
 
proteusiq profile image
Prayson Wilfred Daniel

🀯Nice. Is there similar that takes a sequence of even and odd numbers and only sum odds?

Collapse
 
voyeg3r profile image
SΓ©rgio AraΓΊjo

yes:

seq 9 | awk '{($1 % 2 ? sum+=$1 : 0)} END { print sum}'

Thread Thread
 
proteusiq profile image
Prayson Wilfred Daniel • Edited

Now that blows my mind 🀯. 😍🎊 Is awk not programming language by itself?

Collapse
 
antonov_mike profile image
Antonov Mike

Sir, your writing style is brilliant

Collapse
 
proteusiq profile image
Prayson Wilfred Daniel

Thank you, Antonov. I am attempting a story like telling of concepts 🫒 I love telling stories.

 
proteusiq profile image
Prayson Wilfred Daniel
Collapse
 
proteusiq profile image
Prayson Wilfred Daniel • Edited

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?

Collapse
 
tharakamts profile image
Tharaka Sandaruwan

Great Story, Like it πŸ”₯

Collapse
 
proteusiq profile image
Prayson Wilfred Daniel

I am glad you did πŸͺπŸ™πŸΏ

Collapse
 
proteusiq profile image
Prayson Wilfred Daniel • Edited

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 🀭

 
proteusiq profile image
Prayson Wilfred Daniel

I am cheering πŸ“£ for your success. You are awesome πŸ™ŒπŸΏ

 
proteusiq profile image
Prayson Wilfred Daniel • Edited

That is another level πŸ™ŒπŸΏπŸ₯³ and pure genius.

 
proteusiq profile image
Prayson Wilfred Daniel • Edited

Nice! Are you using LLMs under the hood?

 
proteusiq profile image
Prayson Wilfred Daniel

Oh no! I am not suggesting for better-ness. I just thought I missed something in Bash 😊