Just in time processing on a character stream: Difference between revisions

m
→‎{{header|11l}}: new way of specifying file open mode
(→‎{{header|jq}}: .out = $c)
m (→‎{{header|11l}}: new way of specifying file open mode)
 
(4 intermediate revisions by 4 users not shown)
Line 11:
 
This task was inspired by the movie "[[wp:National_Treasure_%28film%29|National Treasure]]", which refers to a "[[wp:Book cipher|book cipher]]".
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">T UserInput
Int formFeed
Int lineFeed
Int tab
Int space
 
F (chunk)
.formFeed = chunk[0]
.lineFeed = chunk[1]
.tab = chunk[2]
.space = chunk[3]
 
F String()
R ‘(ff=#.; lf=#.; tb=#.; sp#.)’.format(.formFeed, .lineFeed, .tab, .space)
 
F chunks(l, n)
[[Int]] r
L(i) (0 .< l.len).step(n)
r.append(l[i .+ n])
R r
 
F getUserInput()
V h = ‘0 18 0 0 0 68 0 1 0 100 0 32 0 114 0 45 0 38 0 26 0 16 0 21 0 17 0 59 0 11 ’""
‘0 29 0 102 0 0 0 10 0 50 0 39 0 42 0 33 0 50 0 46 0 54 0 76 0 47 0 84 2 28’
V ha = h.split(‘ ’).map(Int)
R chunks(ha, 4).map(chunk -> UserInput(chunk))
 
F decode(filename, uiList)
V f = File(filename)
V text = f.read()
 
F decode2(ui)
V endf = 0
V l = 0
V t = 0
V s = 0
L(c) @text
| ifI .f == ui.formFeed and& .l == ui.lineFeed and& .t == ui.tab and& .s == ui.space
I else .outc == $c‘!’
|R .return = true0B
print(c, end' ‘’)
R 1B
I c.code == 0'C
f = f + 1
l = 0
t = 0
s = 0
E I c == "\n"
l = l + 1
t = 0
s = 0
E I c == "\t"
t = t + 1
s = 0
E
s = s + 1
R 0B
 
L(ui) uiList
I !decode2(ui)
L.break
print()
 
V uiList = getUserInput()
decode(‘theRaven.txt’, uiList)</syntaxhighlight>
 
{{out}}
<pre>
Silence-Dogood.
</langpre>
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 229 ⟶ 303:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Silence-Dogood.</pre>
Line 235 ⟶ 309:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 313 ⟶ 387:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Silence-Dogood.</pre>
Line 319 ⟶ 393:
=={{header|C++}}==
Text used to encode:[http://paulo-jorente.de/text/theRaven.txt The Raven - by E.A.Poe]
<langsyntaxhighlight lang="cpp">
#include <vector>
#include <iostream>
Line 380 ⟶ 454:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 388 ⟶ 462:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.algorithm;
import std.array;
import std.conv;
Line 452 ⟶ 526:
auto uiList = getUserInput();
decode("theRaven.txt", uiList);
}</langsyntaxhighlight>
{{out}}
<pre>Silence-Dogood.</pre>
Line 458 ⟶ 532:
=={{header|Go}}==
{{trans|C++}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 536 ⟶ 610:
log.Fatal(err)
}
}</langsyntaxhighlight>
 
{{out}}
Line 545 ⟶ 619:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Line 663 ⟶ 737:
decode("theRaven.txt", uiList);
}
}</langsyntaxhighlight>
{{out}}
<pre>Silence-Dogood.</pre>
Line 671 ⟶ 745:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq"># User input takes the form of quadtuples of integers: [formFeed, lineFeed, tab, space]
def getUserInput:
def nwise($n):
Line 681 ⟶ 755:
| split(" ") | map(tonumber) | nwise(4)
| {formFeed: .[0], lineFeed: .[1], tab: .[2], space: .[3]} ;
 
def emit_until(cond; stream):
label $out | stream | if cond then break $out else . end;
 
# Input should be the text as a (long) string
Line 689 ⟶ 766:
| label $out
| foreach stream as $c (
{ f: 0, l: 0, t: 0, s: 0 };
if .f == ui.formFeed and .l == ui.lineFeed and .t == ui.tab and .s == ui.space
 
then .returnout = null$c
| if .f == ui.formFeed and .l == ui.lineFeed and .t == ui.tab and .s == ui.space
then if $c == "!" then .return = false
else .out = $c
| .return = true
end
elif $c == "\f"
then .f += 1
Line 711 ⟶ 783:
else .s += 1
end;
if .return != nullout then .out, break $out else empty end )
// .out"" ;
decode2($uiList) ;
 
# Input: the text
[emit_until(. == "!"; getUserInput as $ui | decode($ui)) ] | add</syntaxhighlight>
</lang>
{{out}}
Invocation: jq -Rsr -f program.jq theRaven.txt
Line 726 ⟶ 797:
=={{header|Julia}}==
Customization is via adding to or deleting from the chars dictionary.
<langsyntaxhighlight lang="julia">@enum streamstate GET_FF GET_LF GET_TAB GET_CHAR ABORT
chars = Dict(GET_FF => ['\f'], GET_LF => ['\n'], GET_TAB => ['\t'])
 
Line 765 ⟶ 836:
 
stream_decode_jit(open("filename.txt", "r"))
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">// version 1.2.10
 
import java.io.File
Line 818 ⟶ 889:
val uiList = getUserInput()
decode("theRaven.txt", uiList)
}</langsyntaxhighlight>
 
{{out}}
Line 829 ⟶ 900:
With some modifications compared to the model.
 
<langsyntaxhighlight Nimlang="nim">import options, sequtils, strutils
 
type Position = tuple[ff, lf, tab, sp: int]
Line 866 ⟶ 937:
"33 0 50 0 46 0 54 0 76 0 47 0 84 2 28")
 
echo "theRaven.txt".decode(UiList)</langsyntaxhighlight>
 
{{out}}
Line 874 ⟶ 945:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 927 ⟶ 998:
 
my $enc = jit_encode('The slithey toves did gyre and gimble in the wabe');
say my $result = "Encoded\n$enc\n\nDecoded\n" . jit_decode($enc);</langsyntaxhighlight>
{{out}}
<pre>Encoded
Line 938 ⟶ 1,009:
{{trans|C}}
{{libheader|Phix/libcurl}}
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">-- demo/rosetta/BookCipher.exw</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">decode</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">ui</span><span style="color: #0000FF;">)</span>
Line 1,006 ⟶ 1,077:
<span style="color: #000000;">decodeFile</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"theRaven.txt"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"http://paulo-jorente.de/text/theRaven.txt"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">code</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,014 ⟶ 1,085:
=={{header|Python}}==
{{trans|D}}
<langsyntaxhighlight lang="python">import sys
 
class UserInput:
Line 1,075 ⟶ 1,146:
 
uiList = getUserInput()
decode("theRaven.txt", uiList)</langsyntaxhighlight>
{{out}}
<pre>Silence-Dogood.</pre>
Line 1,083 ⟶ 1,154:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define user-input
Line 1,105 ⟶ 1,176:
(define c (decode slice))
#:break (char=? #\! c)
(display c))</langsyntaxhighlight>
 
{{in}}
Line 1,125 ⟶ 1,196:
Will handle any visible character in the ASCII range as well as space and new-line.
 
<syntaxhighlight lang="raku" perl6line>#`[
Set srand to set the encode / decode "key".
Need to use the same "key" and same implementation
Line 1,187 ⟶ 1,258:
 
say "\n== Decoded: ==";
say jit-decode($enc);</langsyntaxhighlight>
 
<pre>== Secret: ==
Line 1,218 ⟶ 1,289:
The input file used by this REXX program only contains one page; &nbsp; it has no &nbsp; ''FF'' &nbsp; (''formfeed'') &nbsp; characters in it),
<br>and the injection of &nbsp; ''FF'' &nbsp; characters into the file would be like putting pencil marks into a holy book. &nbsp; <big><big><big> ☺ </big></big></big>
<langsyntaxhighlight lang="rexx">/*REXX program extracts characters by using a book cipher (that is a text file). */
parse arg iFID . /*obtain optional name of file (book).*/
if iFID=='' | iFID=="," then iFID= 'JIT.TXT' /*Not specified? Then use the default.*/
Line 1,252 ⟶ 1,323:
?= ? || x /*append the character to the phrase. */
end /*j*/ /* [↑] display letters found in book. */
say '═════►' ? /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|input|text=&nbsp; supplied to the console (terminal) by the user in response to the prompts, &nbsp; (the commas are optional):}}
<pre>
Line 1,284 ⟶ 1,355:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create JustInTimeStreamExtract {
Line 1,353 ⟶ 1,424:
tcl::mathop::== $counter $counters(page) $counters(line) $counters(field) $counters(char)
}
}</langsyntaxhighlight>
Demonstration of use:
<langsyntaxhighlight lang="tcl">[JustInTimeStreamExtract new] stream [open "sample.txt"]</langsyntaxhighlight>
<!-- no output; I'll wait for someone else to invent something to decode… -->
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Structure UserInput
Line 1,444 ⟶ 1,515:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Silence-Dogood.</pre>
Line 1,452 ⟶ 1,523:
{{libheader|Wren-dynamic}}
{{libheader|wren-seq}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for File
import "./dynamic" for Tuple
import "./seq" for Lst
 
var UserInput = Tuple.create("UserINput", ["formFeed", "lineFeed", "tab", "space"])
Line 1,505 ⟶ 1,576:
 
var uiList = getUserInput.call()
decode.call("theRaven.txt", uiList)</langsyntaxhighlight>
 
{{out}}
Line 1,514 ⟶ 1,585:
=={{header|zkl}}==
{{trans|C++}}
<langsyntaxhighlight lang="zkl">class FlyBy{
fcn decode(file,tuplets){
codePad:=File(file).read().mode(String); // blob of text
Line 1,538 ⟶ 1,609:
h.split(" ").pump(List,T(Void.Read,3),
fcn(ff,lf,t,s){ vm.arglist.apply("toInt") });
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">input:=getUserInput();
// our code pad is: http://paulo-jorente.de/text/theRaven.txt
FlyBy.decode("theRaven.txt",input);</langsyntaxhighlight>
{{out}}
<pre>
1,480

edits