To Code and Beyond: A Neverland Adventure in Bash, Lua, Python, and Rust
Tags: rust, python, lua, bash
Published: 2023-12-23 14:06:16


![ship](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/epnd8ccp1dbf02z63h9f.png)

### 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:**
```bash
#!/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:**
```lua
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))
```

___

![peter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/944bnwt2e2bxylxbtjgo.jpeg)
### 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:**
```python
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:**
```python
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdhcahcmq4qfkh3uiu7z.png)
### 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:**
```rust
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:**
```rust
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.