Real-world text processing and transformation.
// slug.ifa - Generate URL-friendly slugs from titles
ise slugify(title) {
// Convert to lowercase
ayanmo slug = Ika.lowercase(title);
// Replace spaces with hyphens
ayanmo slug = Ika.replace(slug, " ", "-");
// Remove special characters
ayanmo allowed = "abcdefghijklmnopqrstuvwxyz0123456789-";
ayanmo result = "";
ayanmo i = 0;
nigba (i < Ika.len(slug)) {
ayanmo char = Ika.char_at(slug, i);
ti (Ika.contains(allowed, char)) {
ayanmo result = result + char;
}
ayanmo i = i + 1;
}
// Remove multiple consecutive hyphens
nigba (Ika.contains(result, "--")) {
ayanmo result = Ika.replace(result, "--", "-");
}
// Trim hyphens from ends
ayanmo result = Ika.trim_char(result, "-");
padap? result;
}
// Usage
ayanmo titles = [
"Hello World! This is a Test",
"10 Tips for Better Coding",
"Ifá-Lang: The Future of Programming!!!"
];
fun title ninu titles {
Irosu.fo(title);
Irosu.fo(" ? " + slugify(title) + "\n");
}
// template.ifa - Simple template variable substitution
ise render_template(template, vars) {
ayanmo result = template;
fun key ninu Ogbe.keys(vars) {
ayanmo placeholder = "{{" + key + "}}";
ayanmo value = vars[key];
ayanmo result = Ika.replace(result, placeholder, value);
}
padap? result;
}
ise render_list(template, items, item_template) {
ayanmo list_content = "";
fun item ninu items {
ayanmo rendered = render_template(item_template, item);
ayanmo list_content = list_content + rendered;
}
padap? Ika.replace(template, "{{items}}", list_content);
}
// Usage
ayanmo email_template = "
Dear {{name}},
Thank you for your order #{{order_id}}.
Total: ${{total}}
{{items}}
Best regards,
{{company}}
";
ayanmo item_template = "- {{product}}: ${{price}}\n";
ayanmo vars = {
"name": "John",
"order_id": "12345",
"total": "150.00",
"company": "Ifá Shop"
};
ayanmo items = [
{"product": "Widget A", "price": "50.00"},
{"product": "Widget B", "price": "100.00"}
];
ayanmo email = render_template(email_template, vars);
ayanmo email = render_list(email, items, item_template);
Irosu.fo(email);
// csv_parser.ifa - Parse CSV files into structured data
ise parse_csv(content) {
ayanmo lines = Ika.split(content, "\n");
ayanmo headers = [];
ayanmo rows = [];
ayanmo first = otito;
fun line ninu lines {
ti (Ika.len(Ika.trim(line)) == 0) {
tesiwaju;
}
ayanmo cells = parse_csv_line(line);
ti (first) {
ayanmo headers = cells;
ayanmo first = iro;
} bib?k? {
ayanmo row = {};
ayanmo i = 0;
fun cell ninu cells {
ayanmo key = Ogunda.get(headers, i);
row[key] = cell;
ayanmo i = i + 1;
}
ayanmo rows = Ogunda.push(rows, row);
}
}
padap? rows;
}
ise parse_csv_line(line) {
ayanmo cells = [];
ayanmo current = "";
ayanmo in_quotes = iro;
ayanmo i = 0;
nigba (i < Ika.len(line)) {
ayanmo char = Ika.char_at(line, i);
ti (char == "\"") {
ayanmo in_quotes = !in_quotes;
} bib?k? ti (char == "," && !in_quotes) {
ayanmo cells = Ogunda.push(cells, Ika.trim(current));
ayanmo current = "";
} bib?k? {
ayanmo current = current + char;
}
ayanmo i = i + 1;
}
ayanmo cells = Ogunda.push(cells, Ika.trim(current));
padap? cells;
}
// Usage
ayanmo csv = "name,email,age
John Doe,john@example.com,30
Jane Smith,jane@example.com,25
\"Bob, Jr.\",bob@example.com,35";
ayanmo data = parse_csv(csv);
fun row ninu data {
Irosu.fo(row["name"] + " (" + row["email"] + ") - Age: " + row["age"]);
}
// word_count.ifa - Text analysis utilities
ise count_words(text) {
ayanmo words = Ika.split(text, " ");
ayanmo filtered = [];
fun word ninu words {
ti (Ika.len(Ika.trim(word)) > 0) {
ayanmo filtered = Ogunda.push(filtered, word);
}
}
padap? Ogunda.len(filtered);
}
ise count_sentences(text) {
ayanmo count = 0;
fun char ninu [".", "!", "?"] {
ayanmo parts = Ika.split(text, char);
ayanmo count = count + Ogunda.len(parts) - 1;
}
padap? count;
}
ise word_frequency(text) {
ayanmo words = Ika.split(Ika.lowercase(text), " ");
ayanmo freq = {};
fun word ninu words {
ayanmo clean = Ika.replace(word, ".", "");
ayanmo clean = Ika.replace(clean, ",", "");
ayanmo clean = Ika.trim(clean);
ti (Ika.len(clean) > 0) {
ti (freq[clean] == nil) {
freq[clean] = 0;
}
freq[clean] = freq[clean] + 1;
}
}
padap? freq;
}
// Usage
ayanmo text = "The quick brown fox jumps over the lazy dog. The dog was very lazy. The fox was very quick.";
Irosu.fo("Words: " + count_words(text));
Irosu.fo("Sentences: " + count_sentences(text));
ayanmo freq = word_frequency(text);
Irosu.fo("\nWord Frequency:");
fun word key(freq) {
Irosu.fo(" " + word + ": " + freq[word]);
}