5. Extract a function
Move the counting logic into src/main/kalm/stats/Counting.kalm:
space stats
pub fun countWords(text: Text) -> Int:
var words = 0
for word in text.split("\\s+"):
if not word.isBlank():
words += 1
return wordsspace stats
pub fun countWords(text: Text) -> Int {
var words = 0
for word in text.split("\\s+") {
if not word.isBlank() {
words += 1
}
}
return words
}package stats;
public final class Counting {
public static int countWords(String text) {
int words = 0;
for (final String word : text.split("\\s+"))
if (!word.isBlank()) words += 1;
return words;
}
}Kalm Script unavailable — showing KalmThis source file belongs to the project path; the script path uses one self-contained file.
Replace App.kalm with its caller:
space stats
pub fun main() -> Unit:
sayLine(@"${countWords("a quiet morning")} words")space stats
pub fun main() -> Unit {
sayLine(@"${countWords("a quiet morning")} words")
}package stats;
public final class App {
public static void main(String[] args) {
System.out.println(
Counting.countWords("a quiet morning") + " words"
);
}
}Kalm Script unavailable — showing KalmThis source file belongs to the project path; the script path uses one self-contained file.
The function has an explicit parameter and result type. Both files share the
same package, so the function is available without an import. ${...} embeds
an expression in interpolated text.
kalm.bat runkalm.bat runkalm runkalm runkalm runkalm runkalm runkalm run3 wordsTry calling countWords with two different strings. It has no mutable shared state, which makes it easy to test.
Save the complete program below as App.kalms in a folder of your choice. Each lesson replaces this file; the shell does not load neighboring project files.
fun countWords(text: Text) -> Int:
var words = 0
for word in text.split("\\s+"):
if not word.isBlank():
words += 1
return words
pub fun main() -> Unit:
sayLine(@"${countWords("a quiet morning")} words")fun countWords(text: Text) -> Int {
var words = 0
for word in text.split("\\s+") {
if not word.isBlank() {
words += 1
}
}
return words
}
pub fun main() -> Unit {
sayLine(@"${countWords("a quiet morning")} words")
}fun countWords(text: Text) -> Int:
var words = 0
for word in text.split("\\s+"):
if not word.isBlank():
words += 1
return words
sayLine(@"${countWords("a quiet morning")} words")fun countWords(text: Text) -> Int {
var words = 0
for word in text.split("\\s+") {
if not word.isBlank() {
words += 1
}
}
return words
}
sayLine(@"${countWords("a quiet morning")} words")final class Counting {
public static int countWords(String text) {
int words = 0;
for (final String word : text.split("\\s+"))
if (!word.isBlank()) words += 1;
return words;
}
}
public final class App {
public static void main(String[] args) {
System.out.println(
Counting.countWords("a quiet morning") + " words"
);
}
}kalm.bat shell -f App.kalmskalm.bat shell -f App.kalmskalm shell -f App.kalmskalm shell -f App.kalmskalm shell -f App.kalmskalm shell -f App.kalmskalm shell -f App.kalmskalm shell -f App.kalms3 words