Data Structures

Arrays

Arrays in BanglaCode are ordered collections that can hold elements of any type. They are zero-indexed and dynamically sized.

Creating Arrays

1// Empty array
2dhoro empty = [];
3
4// Array with elements
5dhoro numbers = [1, 2, 3, 4, 5];
6dhoro names = ["Rahim", "Karim", "Jamil"];
7
8// Mixed types
9dhoro mixed = [42, "hello", sotti, khali, [1, 2]];
10
11// Nested arrays
12dhoro matrix = [
13 [1, 2, 3],
14 [4, 5, 6],
15 [7, 8, 9]
16];

Accessing Elements

Arrays are zero-indexed (first element is at index 0):

1dhoro fruits = ["Apple", "Banana", "Mango", "Orange"];
2
3// Access by index
4dekho(fruits[0]); // "Apple"
5dekho(fruits[2]); // "Mango"
6
7// Access last element
8dhoro lastIndex = dorghyo(fruits) - 1;
9dekho(fruits[lastIndex]); // "Orange"
10
11// Nested array access
12dhoro matrix = [[1, 2], [3, 4], [5, 6]];
13dekho(matrix[1][0]); // 3
14dekho(matrix[2][1]); // 6

Modifying Arrays

1dhoro arr = [10, 20, 30];
2
3// Modify element
4arr[1] = 25;
5dekho(arr); // [10, 25, 30]
6
7// Add element with index
8arr[3] = 40;
9dekho(arr); // [10, 25, 30, 40]

Array Built-in Functions

dorghyo - Length

1dhoro arr = [1, 2, 3, 4, 5];
2dekho(dorghyo(arr)); // 5
3
4dhoro empty = [];
5dekho(dorghyo(empty)); // 0

dhokao - Push (Add to End)

1dhoro arr = [1, 2, 3];
2
3dhokao(arr, 4);
4dekho(arr); // [1, 2, 3, 4]
5
6dhokao(arr, 5);
7dhokao(arr, 6);
8dekho(arr); // [1, 2, 3, 4, 5, 6]

berKoro - Pop (Remove from End)

1dhoro arr = [1, 2, 3, 4, 5];
2
3dhoro last = berKoro(arr);
4dekho(last); // 5
5dekho(arr); // [1, 2, 3, 4]
6
7dhoro another = berKoro(arr);
8dekho(another); // 4
9dekho(arr); // [1, 2, 3]

kato - Slice

1dhoro arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
2
3// slice(start, end) - excludes end index
4dekho(kato(arr, 2, 5)); // [2, 3, 4]
5dekho(kato(arr, 0, 3)); // [0, 1, 2]
6
7// slice(start) - from start to end
8dekho(kato(arr, 5)); // [5, 6, 7, 8, 9]
9dekho(kato(arr, 7)); // [7, 8, 9]
10
11// Original array unchanged
12dekho(arr); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ulto - Reverse

1dhoro arr = [1, 2, 3, 4, 5];
2dhoro reversed = ulto(arr);
3
4dekho(reversed); // [5, 4, 3, 2, 1]
5dekho(arr); // [1, 2, 3, 4, 5] (original unchanged)

ache - Contains/Includes

1dhoro fruits = ["Apple", "Banana", "Mango"];
2
3dekho(ache(fruits, "Banana")); // sotti
4dekho(ache(fruits, "Orange")); // mittha
5
6// Use in conditions
7jodi (ache(fruits, "Mango")) {
8 dekho("Mango is available!");
9}

saja - Sort

1// Sort numbers
2dhoro numbers = [3, 1, 4, 1, 5, 9, 2, 6];
3dekho(saja(numbers)); // [1, 1, 2, 3, 4, 5, 6, 9]
4
5// Sort strings
6dhoro names = ["Karim", "Rahim", "Abdul", "Zahir"];
7dekho(saja(names)); // ["Abdul", "Karim", "Rahim", "Zahir"]

joro - Join

1dhoro words = ["Hello", "World", "BanglaCode"];
2
3dekho(joro(words, " ")); // "Hello World BanglaCode"
4dekho(joro(words, ", ")); // "Hello, World, BanglaCode"
5dekho(joro(words, "-")); // "Hello-World-BanglaCode"
6dekho(joro(words, "")); // "HelloWorldBanglaCode"

Array Higher-Order Methods

manchitro - Map

Transforms each element in an array using a callback function. Returns a new array with the transformed elements.

1// Double each number
2dhoro numbers = [1, 2, 3, 4, 5];
3dhoro doubled = manchitro(numbers, kaj(x) {
4 ferao x * 2;
5});
6dekho(doubled); // [2, 4, 6, 8, 10]
7
8// Extract property from objects
9dhoro people = [
10 {"naam": "Rahim", "age": 25},
11 {"naam": "Karim", "age": 30}
12];
13dhoro names = manchitro(people, kaj(person) {
14 ferao person["naam"];
15});
16dekho(names); // ["Rahim", "Karim"]
17
18// Using index parameter
19dhoro squared = manchitro(numbers, kaj(x, i) {
20 ferao x * x;
21});
22dekho(squared); // [1, 4, 9, 16, 25]

chhanno - Filter

Filters array elements based on a condition. Returns a new array containing only elements where the callback returns true.

1// Filter even numbers
2dhoro numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3dhoro evens = chhanno(numbers, kaj(x) {
4 ferao x % 2 == 0;
5});
6dekho(evens); // [2, 4, 6, 8, 10]
7
8// Filter by condition
9dhoro scores = [45, 78, 92, 35, 88, 55];
10dhoro passing = chhanno(scores, kaj(score) {
11 ferao score >= 50;
12});
13dekho(passing); // [78, 92, 88, 55]
14
15// Filter with index
16dhoro colors = ["Red", "Green", "Blue", "Yellow"];
17dhoro result = chhanno(colors, kaj(color, index) {
18 ferao index % 2 == 0; // Keep items at even indices
19});
20dekho(result); // ["Red", "Blue"]

sonkuchito - Reduce

Reduces array to a single value by applying a function to accumulate the result. Takes an optional initial value.

1// Sum all numbers
2dhoro numbers = [1, 2, 3, 4, 5];
3dhoro sum = sonkuchito(numbers, kaj(acc, x) {
4 ferao acc + x;
5}, 0);
6dekho(sum); // 15
7
8// Calculate product
9dhoro product = sonkuchito(numbers, kaj(acc, x) {
10 ferao acc * x;
11}, 1);
12dekho(product); // 120
13
14// Build a string
15dhoro words = ["Hello", "World", "BanglaCode"];
16dhoro sentence = sonkuchito(words, kaj(acc, word) {
17 jodi (acc == "") {
18 ferao word;
19 }
20 ferao acc + " " + word;
21}, "");
22dekho(sentence); // "Hello World BanglaCode"
23
24// Count occurrences
25dhoro arr = [1, 2, 2, 3, 3, 3, 4];
26dhoro counts = sonkuchito(arr, kaj(acc, x) {
27 acc[lipi(x)] = (acc[lipi(x)] || 0) + 1;
28 ferao acc;
29}, {});
30dekho(counts); // {"1": 1, "2": 2, "3": 3, "4": 1}

proti - ForEach

Executes a callback function for each element. Returns null. Useful for side effects like logging.

1// Print each element
2dhoro fruits = ["Apple", "Banana", "Mango"];
3proti(fruits, kaj(fruit) {
4 dekho("- " + fruit);
5});
6// Output:
7// - Apple
8// - Banana
9// - Mango
10
11// Enumerate with index
12dhoro items = ["First", "Second", "Third"];
13proti(items, kaj(item, index) {
14 dekho((index + 1) + ". " + item);
15});
16// Output:
17// 1. First
18// 2. Second
19// 3. Third
20
21// Side effects (mutations)
22dhoro counter = 0;
23proti([10, 20, 30], kaj(x) {
24 counter = counter + 1;
25});
26dekho("Processed:", counter); // Processed: 3

Chaining Higher-Order Methods

1// Chain multiple operations
2dhoro numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3
4// Filter > Map > Reduce: Get sum of squares of even numbers
5dhoro evens = chhanno(numbers, kaj(x) { ferao x % 2 == 0; });
6dhoro squares = manchitro(evens, kaj(x) { ferao x * x; });
7dhoro sumOfSquares = sonkuchito(squares, kaj(acc, x) { ferao acc + x; }, 0);
8dekho(sumOfSquares); // 220 (4 + 16 + 36 + 64 + 100)
9
10// Practical: Process user data
11dhoro users = [
12 {"naam": "Rahim", "age": 25, "active": sotti},
13 {"naam": "Karim", "age": 17, "active": mittha},
14 {"naam": "Jamil", "age": 30, "active": sotti}
15];
16
17// Get names of active adults
18dhoro activeAdults = chhanno(users, kaj(user) {
19 ferao user["active"] ebong user["age"] >= 18;
20});
21dhoro activeNames = manchitro(activeAdults, kaj(user) {
22 ferao user["naam"];
23});
24dekho(activeNames); // ["Rahim", "Jamil"]

Iterating Over Arrays

1dhoro colors = ["Red", "Green", "Blue", "Yellow"];
2
3// Using for loop
4ghuriye (dhoro i = 0; i < dorghyo(colors); i = i + 1) {
5 dekho(i + 1, "-", colors[i]);
6}
7
8// Process each element
9dhoro numbers = [1, 2, 3, 4, 5];
10dhoro sum = 0;
11
12ghuriye (dhoro i = 0; i < dorghyo(numbers); i = i + 1) {
13 sum = sum + numbers[i];
14}
15
16dekho("Sum:", sum); // Sum: 15

Common Array Patterns

Finding Maximum/Minimum

1dhoro numbers = [34, 12, 89, 45, 23, 67];
2
3// Find maximum
4dhoro max = numbers[0];
5ghuriye (dhoro i = 1; i < dorghyo(numbers); i = i + 1) {
6 jodi (numbers[i] > max) {
7 max = numbers[i];
8 }
9}
10dekho("Max:", max); // 89
11
12// Find minimum
13dhoro min = numbers[0];
14ghuriye (dhoro i = 1; i < dorghyo(numbers); i = i + 1) {
15 jodi (numbers[i] < min) {
16 min = numbers[i];
17 }
18}
19dekho("Min:", min); // 12
20
21// Using built-ins
22dekho("Max:", boro(34, 12, 89, 45, 23, 67)); // 89
23dekho("Min:", choto(34, 12, 89, 45, 23, 67)); // 12

Filter Array

1dhoro numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
2
3// Filter even numbers
4dhoro evens = [];
5ghuriye (dhoro i = 0; i < dorghyo(numbers); i = i + 1) {
6 jodi (numbers[i] % 2 == 0) {
7 dhokao(evens, numbers[i]);
8 }
9}
10dekho(evens); // [2, 4, 6, 8, 10]

Map/Transform Array

1dhoro numbers = [1, 2, 3, 4, 5];
2
3// Double each element
4dhoro doubled = [];
5ghuriye (dhoro i = 0; i < dorghyo(numbers); i = i + 1) {
6 dhokao(doubled, numbers[i] * 2);
7}
8dekho(doubled); // [2, 4, 6, 8, 10]

Remove Duplicates

1dhoro arr = [1, 2, 2, 3, 3, 3, 4, 5, 5];
2
3dhoro unique = [];
4ghuriye (dhoro i = 0; i < dorghyo(arr); i = i + 1) {
5 jodi (na ache(unique, arr[i])) {
6 dhokao(unique, arr[i]);
7 }
8}
9dekho(unique); // [1, 2, 3, 4, 5]

Multi-dimensional Arrays

1// 2D array (matrix)
2dhoro matrix = [
3 [1, 2, 3],
4 [4, 5, 6],
5 [7, 8, 9]
6];
7
8// Access elements
9dekho(matrix[0][0]); // 1
10dekho(matrix[1][1]); // 5
11dekho(matrix[2][2]); // 9
12
13// Iterate over 2D array
14ghuriye (dhoro i = 0; i < dorghyo(matrix); i = i + 1) {
15 dhoro row = "";
16 ghuriye (dhoro j = 0; j < dorghyo(matrix[i]); j = j + 1) {
17 row = row + lipi(matrix[i][j]) + " ";
18 }
19 dekho(row);
20}
21
22// Sum all elements
23dhoro total = 0;
24ghuriye (dhoro i = 0; i < dorghyo(matrix); i = i + 1) {
25 ghuriye (dhoro j = 0; j < dorghyo(matrix[i]); j = j + 1) {
26 total = total + matrix[i][j];
27 }
28}
29dekho("Sum:", total); // 45

Practical Examples

Stack Implementation

1dhoro stack = [];
2
3// Push
4dhokao(stack, "first");
5dhokao(stack, "second");
6dhokao(stack, "third");
7
8// Pop
9dekho(berKoro(stack)); // "third"
10dekho(berKoro(stack)); // "second"
11
12// Peek (without removing)
13dekho(stack[dorghyo(stack) - 1]); // "first"

Queue Implementation

1dhoro queue = [];
2
3// Enqueue (add to end)
4dhokao(queue, "first");
5dhokao(queue, "second");
6dhokao(queue, "third");
7
8// Dequeue (remove from front using slice)
9dhoro front = queue[0];
10queue = kato(queue, 1);
11dekho(front); // "first"
12dekho(queue); // ["second", "third"]