Waxaan qoraynaa oon dib uhabeyn ku wadnaa docs oo af-soomaali ku qoran, ka qeybqaadasho raac lifaaqaan...Contribution guidelines

Built-in Functions in Soplang

Soplang provides a rich set of built-in functions that help you perform common tasks without having to write the code yourself. These functions are always available and can be called directly in your code.

Input and Output Functions

These functions allow you to interact with the user and display information.

qor() - Print

Displays values to the console without a newline at the end.

print_examples.sop
// Basic printing
qor("Hello")      // Displays: Hello
qor(42)          // Displays: 42
qor(true)        // Displays: true

// Multiple values
qor("The answer is", 42)  // Displays: The answer is 42

qorln() - Print Line

Displays values to the console with a newline at the end.

println_examples.sop
// Printing with newline
qorln("Hello")    // Displays: Hello (with newline)
qorln("World")    // Displays: World (on next line)

// Multiple values
qorln("Name:", "Ahmed", "Age:", 25)  // Displays: Name: Ahmed Age: 25 (with newline)

gelin() - Input

Prompts the user for input and returns the entered value as a string.

input_examples.sop
// Basic input
door name = gelin("Enter your name: ")
qorln("Hello,", name)

// Input with conversion
door age_str = gelin("Enter your age: ")
door age = abn(age_str)  // Convert string to number
qorln("In 10 years, you will be", age + 10, "years old")

String Functions

Soplang provides several built-in functions for manipulating strings.

dherer() - Length

Returns the length of a string or collection.

length_examples.sop
door greeting = "Hello, World!"
door length = dherer(greeting)  // 13

door numbers = [1, 2, 3, 4, 5]
door count = dherer(numbers)    // 5

String Manipulation Functions

string_manipulation.sop
door text = "Soplang Programming"

// Convert to uppercase
door upper = kor_dhig(text)  // "SOPLANG PROGRAMMING"

// Convert to lowercase
door lower = yar_dhig(text)  // "soplang programming"

// Trim whitespace
door padded = "  hello  "
door trimmed = trim(padded)  // "hello"

// Replace text
door replaced = bedel(text, "Programming", "Language")  // "Soplang Language"

// Split string
door parts = kala_jar(text, " ")  // ["Soplang", "Programming"]

// Join strings
door words = ["Hello", "from", "Soplang"]
door joined = ku_dar(words, " ")  // "Hello from Soplang"

Math Functions

Soplang includes various mathematical functions for numerical operations.

math_functions.sop
// Basic math functions
door abs_value = abs(-10)           // 10 (absolute value)
door rounded = round(3.7)          // 4 (round to nearest integer)
door floored = floor(3.7)          // 3 (round down)
door ceiled = ceil(3.2)            // 4 (round up)

// Trigonometric functions
door sine = sin(0.5)               // Sine of 0.5 radians
door cosine = cos(0.5)             // Cosine of 0.5 radians
door tangent = tan(0.5)            // Tangent of 0.5 radians

// Other math functions
door square_root = sqrt(16)        // 4 (square root)
door power_result = pow(2, 3)      // 8 (2 raised to power 3)
door log_result = log(100)         // Natural logarithm of 100
door log10_result = log10(100)     // 2 (base-10 logarithm of 100)

// Min and max
door minimum = min(5, 3, 8, 1)     // 1
door maximum = max(5, 3, 8, 1)     // 8

// Random number
door random_val = random()         // Random number between 0 and 1
door random_range = random_int(1, 10)  // Random integer between 1 and 10

Type Conversion Functions

These functions allow you to convert between different data types.

type_conversion.sop
// String to number conversions
door num_str = "42"
door num = abn(num_str)        // 42 (integer)
door float_str = "3.14"
door float_num = toban_dhig(float_str)  // 3.14 (float)

// Number to string conversion
door x = 42
door x_str = qoraal_dhig(x)         // "42"

// Boolean conversions
door bool_val = run_dhig("true")     // true
door bool_str = qoraal_dhig(true)    // "true"

// List conversions
door str_chars = liis_dhig("hello")  // ["h", "e", "l", "l", "o"]
door num_list = [1, 2, 3, 4]
door num_str = qoraal_dhig(num_list)  // "[1, 2, 3, 4]"

List Functions

Soplang provides several functions for working with lists and collections.

list_functions.sop
door numbers = [3, 1, 4, 1, 5, 9]

// Add element to end of list
ku_dar(numbers, 2)  // numbers is now [3, 1, 4, 1, 5, 9, 2]

// Remove and return last element
door last = ka_jar(numbers)  // last = 2, numbers is now [3, 1, 4, 1, 5, 9]

// Insert at specific position
ku_dar_meel(numbers, 0, 0)  // numbers is now [0, 3, 1, 4, 1, 5, 9]

// Remove at specific position
ka_jar_meel(numbers, 2)  // numbers is now [0, 3, 4, 1, 5, 9]

// Sort list
kala_sooc(numbers)  // numbers is now [0, 1, 3, 4, 5, 9]

// Reverse list
kala_rogrog(numbers)  // numbers is now [9, 5, 4, 3, 1, 0]

// Find index of element
door index = ka_raadi(numbers, 5)  // index = 1

// Count occurrences
door fruits = ["apple", "banana", "apple", "orange"]
door count = ku_tiri(fruits, "apple")  // count = 2

Utility Functions

These functions provide various utility operations in Soplang.

utility_functions.sop
// Type checking
door x = 42
door is_num = nooca(x) == "tiro"  // true

door text = "hello"
door is_str = nooca(text) == "qoraal"  // true

// Time functions
door current_time = waqtiga()  // Returns current timestamp
door formatted_time = waqti_format(current_time, "%Y-%m-%d")  // e.g., "2025-05-26"

// System functions
sugna("Processing data...")  // Sleep for 1 second

// File existence check
door exists = file_jira("/path/to/file.txt")  // Returns true if file exists

// Get environment variable
door home_dir = deegaan_hel("HOME")  // Returns value of HOME environment variable

Best Practices

  • Error Handling

    Always check for potential errors when using functions that might fail, such as type conversions or file operations.

    error_handling.sop
    // Good practice: Check for conversion errors
    haday {
      door age_str = gelin("Enter your age: ")
      door age = abn(age_str)
      qorln("In 10 years, you will be", age + 10, "years old")
    } hadii_qalad {
      qorln("Please enter a valid number for age")
    }
  • Function Composition

    Combine built-in functions to create more powerful operations.

    function_composition.sop
    // Combining functions for more complex operations
    door text = "  Hello, World!  "
    
    // Chain operations: trim, convert to lowercase, and split
    door words = kala_jar(yar_dhig(trim(text)), " ")
    
    // Result: ["hello,", "world!"]
  • Performance Considerations

    Be mindful of performance when using functions in loops or with large data sets.

    performance_tips.sop
    // Less efficient: Computing length inside loop
    door text = "Hello, World!"
    ku_celi i = 0; i < dherer(text); i++ {
      // dherer() is called on each iteration
      qorln(text[i])
    }
    
    // More efficient: Compute length once
    door text = "Hello, World!"
    door len = dherer(text)
    ku_celi i = 0; i < len; i++ {
      qorln(text[i])
    }