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 |
| 2 | dhoro empty = []; |
| 3 | |
| 4 | // Array with elements |
| 5 | dhoro numbers = [1, 2, 3, 4, 5]; |
| 6 | dhoro names = ["Rahim", "Karim", "Jamil"]; |
| 7 | |
| 8 | // Mixed types |
| 9 | dhoro mixed = [42, "hello", sotti, khali, [1, 2]]; |
| 10 | |
| 11 | // Nested arrays |
| 12 | dhoro 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):
| 1 | dhoro fruits = ["Apple", "Banana", "Mango", "Orange"]; |
| 2 | |
| 3 | // Access by index |
| 4 | dekho(fruits[0]); // "Apple" |
| 5 | dekho(fruits[2]); // "Mango" |
| 6 | |
| 7 | // Access last element |
| 8 | dhoro lastIndex = dorghyo(fruits) - 1; |
| 9 | dekho(fruits[lastIndex]); // "Orange" |
| 10 | |
| 11 | // Nested array access |
| 12 | dhoro matrix = [[1, 2], [3, 4], [5, 6]]; |
| 13 | dekho(matrix[1][0]); // 3 |
| 14 | dekho(matrix[2][1]); // 6 |
Modifying Arrays
| 1 | dhoro arr = [10, 20, 30]; |
| 2 | |
| 3 | // Modify element |
| 4 | arr[1] = 25; |
| 5 | dekho(arr); // [10, 25, 30] |
| 6 | |
| 7 | // Add element with index |
| 8 | arr[3] = 40; |
| 9 | dekho(arr); // [10, 25, 30, 40] |
Array Built-in Functions
dorghyo - Length
| 1 | dhoro arr = [1, 2, 3, 4, 5]; |
| 2 | dekho(dorghyo(arr)); // 5 |
| 3 | |
| 4 | dhoro empty = []; |
| 5 | dekho(dorghyo(empty)); // 0 |
dhokao - Push (Add to End)
| 1 | dhoro arr = [1, 2, 3]; |
| 2 | |
| 3 | dhokao(arr, 4); |
| 4 | dekho(arr); // [1, 2, 3, 4] |
| 5 | |
| 6 | dhokao(arr, 5); |
| 7 | dhokao(arr, 6); |
| 8 | dekho(arr); // [1, 2, 3, 4, 5, 6] |
berKoro - Pop (Remove from End)
| 1 | dhoro arr = [1, 2, 3, 4, 5]; |
| 2 | |
| 3 | dhoro last = berKoro(arr); |
| 4 | dekho(last); // 5 |
| 5 | dekho(arr); // [1, 2, 3, 4] |
| 6 | |
| 7 | dhoro another = berKoro(arr); |
| 8 | dekho(another); // 4 |
| 9 | dekho(arr); // [1, 2, 3] |
kato - Slice
| 1 | dhoro arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 2 | |
| 3 | // slice(start, end) - excludes end index |
| 4 | dekho(kato(arr, 2, 5)); // [2, 3, 4] |
| 5 | dekho(kato(arr, 0, 3)); // [0, 1, 2] |
| 6 | |
| 7 | // slice(start) - from start to end |
| 8 | dekho(kato(arr, 5)); // [5, 6, 7, 8, 9] |
| 9 | dekho(kato(arr, 7)); // [7, 8, 9] |
| 10 | |
| 11 | // Original array unchanged |
| 12 | dekho(arr); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
ulto - Reverse
| 1 | dhoro arr = [1, 2, 3, 4, 5]; |
| 2 | dhoro reversed = ulto(arr); |
| 3 | |
| 4 | dekho(reversed); // [5, 4, 3, 2, 1] |
| 5 | dekho(arr); // [1, 2, 3, 4, 5] (original unchanged) |
ache - Contains/Includes
| 1 | dhoro fruits = ["Apple", "Banana", "Mango"]; |
| 2 | |
| 3 | dekho(ache(fruits, "Banana")); // sotti |
| 4 | dekho(ache(fruits, "Orange")); // mittha |
| 5 | |
| 6 | // Use in conditions |
| 7 | jodi (ache(fruits, "Mango")) { |
| 8 | dekho("Mango is available!"); |
| 9 | } |
saja - Sort
| 1 | // Sort numbers |
| 2 | dhoro numbers = [3, 1, 4, 1, 5, 9, 2, 6]; |
| 3 | dekho(saja(numbers)); // [1, 1, 2, 3, 4, 5, 6, 9] |
| 4 | |
| 5 | // Sort strings |
| 6 | dhoro names = ["Karim", "Rahim", "Abdul", "Zahir"]; |
| 7 | dekho(saja(names)); // ["Abdul", "Karim", "Rahim", "Zahir"] |
joro - Join
| 1 | dhoro words = ["Hello", "World", "BanglaCode"]; |
| 2 | |
| 3 | dekho(joro(words, " ")); // "Hello World BanglaCode" |
| 4 | dekho(joro(words, ", ")); // "Hello, World, BanglaCode" |
| 5 | dekho(joro(words, "-")); // "Hello-World-BanglaCode" |
| 6 | dekho(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 |
| 2 | dhoro numbers = [1, 2, 3, 4, 5]; |
| 3 | dhoro doubled = manchitro(numbers, kaj(x) { |
| 4 | ferao x * 2; |
| 5 | }); |
| 6 | dekho(doubled); // [2, 4, 6, 8, 10] |
| 7 | |
| 8 | // Extract property from objects |
| 9 | dhoro people = [ |
| 10 | {"naam": "Rahim", "age": 25}, |
| 11 | {"naam": "Karim", "age": 30} |
| 12 | ]; |
| 13 | dhoro names = manchitro(people, kaj(person) { |
| 14 | ferao person["naam"]; |
| 15 | }); |
| 16 | dekho(names); // ["Rahim", "Karim"] |
| 17 | |
| 18 | // Using index parameter |
| 19 | dhoro squared = manchitro(numbers, kaj(x, i) { |
| 20 | ferao x * x; |
| 21 | }); |
| 22 | dekho(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 |
| 2 | dhoro numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 3 | dhoro evens = chhanno(numbers, kaj(x) { |
| 4 | ferao x % 2 == 0; |
| 5 | }); |
| 6 | dekho(evens); // [2, 4, 6, 8, 10] |
| 7 | |
| 8 | // Filter by condition |
| 9 | dhoro scores = [45, 78, 92, 35, 88, 55]; |
| 10 | dhoro passing = chhanno(scores, kaj(score) { |
| 11 | ferao score >= 50; |
| 12 | }); |
| 13 | dekho(passing); // [78, 92, 88, 55] |
| 14 | |
| 15 | // Filter with index |
| 16 | dhoro colors = ["Red", "Green", "Blue", "Yellow"]; |
| 17 | dhoro result = chhanno(colors, kaj(color, index) { |
| 18 | ferao index % 2 == 0; // Keep items at even indices |
| 19 | }); |
| 20 | dekho(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 |
| 2 | dhoro numbers = [1, 2, 3, 4, 5]; |
| 3 | dhoro sum = sonkuchito(numbers, kaj(acc, x) { |
| 4 | ferao acc + x; |
| 5 | }, 0); |
| 6 | dekho(sum); // 15 |
| 7 | |
| 8 | // Calculate product |
| 9 | dhoro product = sonkuchito(numbers, kaj(acc, x) { |
| 10 | ferao acc * x; |
| 11 | }, 1); |
| 12 | dekho(product); // 120 |
| 13 | |
| 14 | // Build a string |
| 15 | dhoro words = ["Hello", "World", "BanglaCode"]; |
| 16 | dhoro sentence = sonkuchito(words, kaj(acc, word) { |
| 17 | jodi (acc == "") { |
| 18 | ferao word; |
| 19 | } |
| 20 | ferao acc + " " + word; |
| 21 | }, ""); |
| 22 | dekho(sentence); // "Hello World BanglaCode" |
| 23 | |
| 24 | // Count occurrences |
| 25 | dhoro arr = [1, 2, 2, 3, 3, 3, 4]; |
| 26 | dhoro counts = sonkuchito(arr, kaj(acc, x) { |
| 27 | acc[lipi(x)] = (acc[lipi(x)] || 0) + 1; |
| 28 | ferao acc; |
| 29 | }, {}); |
| 30 | dekho(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 |
| 2 | dhoro fruits = ["Apple", "Banana", "Mango"]; |
| 3 | proti(fruits, kaj(fruit) { |
| 4 | dekho("- " + fruit); |
| 5 | }); |
| 6 | // Output: |
| 7 | // - Apple |
| 8 | // - Banana |
| 9 | // - Mango |
| 10 | |
| 11 | // Enumerate with index |
| 12 | dhoro items = ["First", "Second", "Third"]; |
| 13 | proti(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) |
| 22 | dhoro counter = 0; |
| 23 | proti([10, 20, 30], kaj(x) { |
| 24 | counter = counter + 1; |
| 25 | }); |
| 26 | dekho("Processed:", counter); // Processed: 3 |
Chaining Higher-Order Methods
| 1 | // Chain multiple operations |
| 2 | dhoro numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 3 | |
| 4 | // Filter > Map > Reduce: Get sum of squares of even numbers |
| 5 | dhoro evens = chhanno(numbers, kaj(x) { ferao x % 2 == 0; }); |
| 6 | dhoro squares = manchitro(evens, kaj(x) { ferao x * x; }); |
| 7 | dhoro sumOfSquares = sonkuchito(squares, kaj(acc, x) { ferao acc + x; }, 0); |
| 8 | dekho(sumOfSquares); // 220 (4 + 16 + 36 + 64 + 100) |
| 9 | |
| 10 | // Practical: Process user data |
| 11 | dhoro 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 |
| 18 | dhoro activeAdults = chhanno(users, kaj(user) { |
| 19 | ferao user["active"] ebong user["age"] >= 18; |
| 20 | }); |
| 21 | dhoro activeNames = manchitro(activeAdults, kaj(user) { |
| 22 | ferao user["naam"]; |
| 23 | }); |
| 24 | dekho(activeNames); // ["Rahim", "Jamil"] |
Iterating Over Arrays
| 1 | dhoro colors = ["Red", "Green", "Blue", "Yellow"]; |
| 2 | |
| 3 | // Using for loop |
| 4 | ghuriye (dhoro i = 0; i < dorghyo(colors); i = i + 1) { |
| 5 | dekho(i + 1, "-", colors[i]); |
| 6 | } |
| 7 | |
| 8 | // Process each element |
| 9 | dhoro numbers = [1, 2, 3, 4, 5]; |
| 10 | dhoro sum = 0; |
| 11 | |
| 12 | ghuriye (dhoro i = 0; i < dorghyo(numbers); i = i + 1) { |
| 13 | sum = sum + numbers[i]; |
| 14 | } |
| 15 | |
| 16 | dekho("Sum:", sum); // Sum: 15 |
Common Array Patterns
Finding Maximum/Minimum
| 1 | dhoro numbers = [34, 12, 89, 45, 23, 67]; |
| 2 | |
| 3 | // Find maximum |
| 4 | dhoro max = numbers[0]; |
| 5 | ghuriye (dhoro i = 1; i < dorghyo(numbers); i = i + 1) { |
| 6 | jodi (numbers[i] > max) { |
| 7 | max = numbers[i]; |
| 8 | } |
| 9 | } |
| 10 | dekho("Max:", max); // 89 |
| 11 | |
| 12 | // Find minimum |
| 13 | dhoro min = numbers[0]; |
| 14 | ghuriye (dhoro i = 1; i < dorghyo(numbers); i = i + 1) { |
| 15 | jodi (numbers[i] < min) { |
| 16 | min = numbers[i]; |
| 17 | } |
| 18 | } |
| 19 | dekho("Min:", min); // 12 |
| 20 | |
| 21 | // Using built-ins |
| 22 | dekho("Max:", boro(34, 12, 89, 45, 23, 67)); // 89 |
| 23 | dekho("Min:", choto(34, 12, 89, 45, 23, 67)); // 12 |
Filter Array
| 1 | dhoro numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 2 | |
| 3 | // Filter even numbers |
| 4 | dhoro evens = []; |
| 5 | ghuriye (dhoro i = 0; i < dorghyo(numbers); i = i + 1) { |
| 6 | jodi (numbers[i] % 2 == 0) { |
| 7 | dhokao(evens, numbers[i]); |
| 8 | } |
| 9 | } |
| 10 | dekho(evens); // [2, 4, 6, 8, 10] |
Map/Transform Array
| 1 | dhoro numbers = [1, 2, 3, 4, 5]; |
| 2 | |
| 3 | // Double each element |
| 4 | dhoro doubled = []; |
| 5 | ghuriye (dhoro i = 0; i < dorghyo(numbers); i = i + 1) { |
| 6 | dhokao(doubled, numbers[i] * 2); |
| 7 | } |
| 8 | dekho(doubled); // [2, 4, 6, 8, 10] |
Remove Duplicates
| 1 | dhoro arr = [1, 2, 2, 3, 3, 3, 4, 5, 5]; |
| 2 | |
| 3 | dhoro unique = []; |
| 4 | ghuriye (dhoro i = 0; i < dorghyo(arr); i = i + 1) { |
| 5 | jodi (na ache(unique, arr[i])) { |
| 6 | dhokao(unique, arr[i]); |
| 7 | } |
| 8 | } |
| 9 | dekho(unique); // [1, 2, 3, 4, 5] |
Multi-dimensional Arrays
| 1 | // 2D array (matrix) |
| 2 | dhoro matrix = [ |
| 3 | [1, 2, 3], |
| 4 | [4, 5, 6], |
| 5 | [7, 8, 9] |
| 6 | ]; |
| 7 | |
| 8 | // Access elements |
| 9 | dekho(matrix[0][0]); // 1 |
| 10 | dekho(matrix[1][1]); // 5 |
| 11 | dekho(matrix[2][2]); // 9 |
| 12 | |
| 13 | // Iterate over 2D array |
| 14 | ghuriye (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 |
| 23 | dhoro total = 0; |
| 24 | ghuriye (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 | } |
| 29 | dekho("Sum:", total); // 45 |
Practical Examples
Stack Implementation
| 1 | dhoro stack = []; |
| 2 | |
| 3 | // Push |
| 4 | dhokao(stack, "first"); |
| 5 | dhokao(stack, "second"); |
| 6 | dhokao(stack, "third"); |
| 7 | |
| 8 | // Pop |
| 9 | dekho(berKoro(stack)); // "third" |
| 10 | dekho(berKoro(stack)); // "second" |
| 11 | |
| 12 | // Peek (without removing) |
| 13 | dekho(stack[dorghyo(stack) - 1]); // "first" |
Queue Implementation
| 1 | dhoro queue = []; |
| 2 | |
| 3 | // Enqueue (add to end) |
| 4 | dhokao(queue, "first"); |
| 5 | dhokao(queue, "second"); |
| 6 | dhokao(queue, "third"); |
| 7 | |
| 8 | // Dequeue (remove from front using slice) |
| 9 | dhoro front = queue[0]; |
| 10 | queue = kato(queue, 1); |
| 11 | dekho(front); // "first" |
| 12 | dekho(queue); // ["second", "third"] |