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

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 i in "$@"; do
        if (( i % 2 != 0 )); then
            (( sum+=i ))
        fi
    done
    echo $sum
}

array=(1 2 3 4 5)
echo $(sum_odd_int_array "${array[@]}")

Lua – The First Mate’s Chant:

function sum_odd_int_array(array)
    local sum = 0
    for _, v in ipairs(array) do
        if v % 2 ~= 0 then
            sum = sum + v
        end
    end
    return sum
end

local array = {1, 2, 3, 4, 5}
print(sum_odd_int_array(array))

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))

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))

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));
}

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.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
unleash-the-power-of-nextgencss-

Unleash the power of NextGenCSS 🔥

Next Post
global-developers-use-google-tools-to-build-solutions-in-recruiting,-mentorship-and-more

Global developers use Google tools to build solutions in recruiting, mentorship and more

Related Posts