6. Keep results together
Keep Counting.kalm from the previous lesson. Add a result record in
src/main/kalm/stats/Statistics.kalm:
space stats
pub data Statistics(lines: Int, words: Int)space stats
pub data Statistics(lines: Int, words: Int)package stats;
public record Statistics(int lines, int words) {}Kalm Script unavailable — showing KalmThis source file belongs to the project path; the script path uses one self-contained file.
Add src/main/kalm/stats/Summary.kalm:
space stats
pub fun summarize(lines: List<Text>) -> Statistics:
var words = 0
for line in lines:
words += countWords(line)
return Statistics(len(lines), words)space stats
pub fun summarize(lines: List<Text>) -> Statistics {
var words = 0
for line in lines {
words += countWords(line)
}
return Statistics(len(lines), words)
}package stats;
import java.util.List;
public final class Summary {
public static Statistics summarize(List<String> lines) {
int words = 0;
for (final String line : lines) words += Counting.countWords(line);
return new Statistics(lines.size(), words);
}
}Kalm Script unavailable — showing KalmThis source file belongs to the project path; the script path uses one self-contained file.
Replace App.kalm:
space stats
pub fun main() -> Unit:
fix lines: List<Text> = ["a quiet", "morning"]
fix result = summarize(lines)
sayLine(@"${result.lines()} lines / ${result.words()} words")space stats
pub fun main() -> Unit {
fix lines: List<Text> = ["a quiet", "morning"]
fix result = summarize(lines)
sayLine(@"${result.lines()} lines / ${result.words()} words")
}package stats;
import java.util.ArrayList;
import java.util.List;
public final class App {
public static void main(String[] args) {
final var lines = new ArrayList<>(List.of("a quiet", "morning"));
final Statistics result = Summary.summarize(lines);
System.out.println(
result.lines() + " lines / " + result.words() + " words"
);
}
}Kalm Script unavailable — showing KalmThis source file belongs to the project path; the script path uses one self-contained file.
A data declaration becomes an immutable Java record. Components are available
through accessor methods. An empty list yields zero lines and zero words.
kalm.bat runkalm.bat runkalm runkalm runkalm runkalm runkalm runkalm run2 lines / 3 wordsRead types and contracts for other ways to model data.
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
data Statistics(lines: Int, words: Int)
fun summarize(lines: List<Text>) -> Statistics:
var words = 0
for line in lines:
words += countWords(line)
return Statistics(len(lines), words)
pub fun main() -> Unit:
fix lines: List<Text> = ["a quiet", "morning"]
fix result = summarize(lines)
sayLine(@"${result.lines()} lines / ${result.words()} words")fun countWords(text: Text) -> Int {
var words = 0
for word in text.split("\\s+") {
if not word.isBlank() {
words += 1
}
}
return words
}
data Statistics(lines: Int, words: Int)
fun summarize(lines: List<Text>) -> Statistics {
var words = 0
for line in lines {
words += countWords(line)
}
return Statistics(len(lines), words)
}
pub fun main() -> Unit {
fix lines: List<Text> = ["a quiet", "morning"]
fix result = summarize(lines)
sayLine(@"${result.lines()} lines / ${result.words()} words")
}fun countWords(text: Text) -> Int:
var words = 0
for word in text.split("\\s+"):
if not word.isBlank():
words += 1
return words
data Statistics(lines: Int, words: Int)
fun summarize(lines: List<Text>) -> Statistics:
var words = 0
for line in lines:
words += countWords(line)
return Statistics(len(lines), words)
fix lines: List<Text> = ["a quiet", "morning"]
fix result = summarize(lines)
sayLine(@"${result.lines()} lines / ${result.words()} words")fun countWords(text: Text) -> Int {
var words = 0
for word in text.split("\\s+") {
if not word.isBlank() {
words += 1
}
}
return words
}
data Statistics(lines: Int, words: Int)
fun summarize(lines: List<Text>) -> Statistics {
var words = 0
for line in lines {
words += countWords(line)
}
return Statistics(len(lines), words)
}
fix lines: List<Text> = ["a quiet", "morning"]
fix result = summarize(lines)
sayLine(@"${result.lines()} lines / ${result.words()} words")import java.util.ArrayList;
import java.util.List;
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;
}
}
record Statistics(int lines, int words) {}
final class Summary {
public static Statistics summarize(List<String> lines) {
int words = 0;
for (final String line : lines) words += Counting.countWords(line);
return new Statistics(lines.size(), words);
}
}
public final class App {
public static void main(String[] args) {
final var lines = new ArrayList<>(List.of("a quiet", "morning"));
final Statistics result = Summary.summarize(lines);
System.out.println(
result.lines() + " lines / " + result.words() + " 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.kalms2 lines / 3 words