!752 /* While idle: Take some text Try to compress it, e.g. by repeating elements Save result (if successful) with notes */ static S compressorID = "#1002225"; p { logOutput(); //while true { pcall { doIt(); } sleepSeconds(1); //} } static void doIt() { S text = takeSomeText(); print("Processing input, size " + toK(l(text)) + " k chars, md5: " + md5(text)); S compression = tryToCompress(text); if (empty(compression)) print("No compression found"); else { boolean ok = verifyCompression(text, compression); if (ok) { print("Compression verifies."); print(indent(2, compression)); } else print("Compression does not verify. Compressor apparently unreliable."); int m = l(text), n = l(compression); long percent = n*100L/m; if (n < m) print("Text successfully compressed to " + percent + "%"); else print("Compression is equal size or bigger than original (+" + (percent-100) + "%)"); } } static S takeSomeText() { return fromLines(repeat("Hello", 5)); } static S tryToCompress(S text) { O compressor = hotwireCached(compressorID); ret (S) call(compressor, "compress", text); } static S expand(S compression) { L lines = toLines(compression); new Matches m; assertTrue(match("expand with #*", lines.get(0), m)); S expanderID = m.get(0); O expander = hotwireCached(expanderID); ret (S) call(expander, "expand", compression); } static boolean verifyCompression(S text, S compression) { ret toLines(text).equals(toLines(expand(compression))); }