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

Content added Content deleted
(Added 11l)
m (syntax highlighting fixup automation)
Line 15: Line 15:
{{trans|Python}}
{{trans|Python}}


<lang 11l>T UserInput
<syntaxhighlight lang="11l">T UserInput
Int formFeed
Int formFeed
Int lineFeed
Int lineFeed
Line 79: Line 79:


V uiList = getUserInput()
V uiList = getUserInput()
decode(‘theRaven.txt’, uiList)</lang>
decode(‘theRaven.txt’, uiList)</syntaxhighlight>


{{out}}
{{out}}
Line 88: Line 88:
=={{header|C}}==
=={{header|C}}==
{{trans|C++}}
{{trans|C++}}
<lang c>#include <stdbool.h>
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
Line 303: Line 303:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Silence-Dogood.</pre>
<pre>Silence-Dogood.</pre>
Line 309: Line 309:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|D}}
{{trans|D}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 387: Line 387:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Silence-Dogood.</pre>
<pre>Silence-Dogood.</pre>
Line 393: Line 393:
=={{header|C++}}==
=={{header|C++}}==
Text used to encode:[http://paulo-jorente.de/text/theRaven.txt The Raven - by E.A.Poe]
Text used to encode:[http://paulo-jorente.de/text/theRaven.txt The Raven - by E.A.Poe]
<lang cpp>
<syntaxhighlight lang="cpp">
#include <vector>
#include <vector>
#include <iostream>
#include <iostream>
Line 454: Line 454:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 462: Line 462:
=={{header|D}}==
=={{header|D}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang D>import std.algorithm;
<syntaxhighlight lang="d">import std.algorithm;
import std.array;
import std.array;
import std.conv;
import std.conv;
Line 526: Line 526:
auto uiList = getUserInput();
auto uiList = getUserInput();
decode("theRaven.txt", uiList);
decode("theRaven.txt", uiList);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Silence-Dogood.</pre>
<pre>Silence-Dogood.</pre>
Line 532: Line 532:
=={{header|Go}}==
=={{header|Go}}==
{{trans|C++}}
{{trans|C++}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 610: Line 610:
log.Fatal(err)
log.Fatal(err)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 619: Line 619:
=={{header|Java}}==
=={{header|Java}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang java>import java.io.IOException;
<syntaxhighlight lang="java">import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Files;
Line 737: Line 737:
decode("theRaven.txt", uiList);
decode("theRaven.txt", uiList);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Silence-Dogood.</pre>
<pre>Silence-Dogood.</pre>
Line 745: Line 745:
{{works with|jq}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
'''Works with gojq, the Go implementation of jq'''
<lang jq># User input takes the form of quadtuples of integers: [formFeed, lineFeed, tab, space]
<syntaxhighlight lang="jq"># User input takes the form of quadtuples of integers: [formFeed, lineFeed, tab, space]
def getUserInput:
def getUserInput:
def nwise($n):
def nwise($n):
Line 788: Line 788:


# Input: the text
# Input: the text
[emit_until(. == "!"; getUserInput as $ui | decode($ui)) ] | add</lang>
[emit_until(. == "!"; getUserInput as $ui | decode($ui)) ] | add</syntaxhighlight>
{{out}}
{{out}}
Invocation: jq -Rsr -f program.jq theRaven.txt
Invocation: jq -Rsr -f program.jq theRaven.txt
Line 797: Line 797:
=={{header|Julia}}==
=={{header|Julia}}==
Customization is via adding to or deleting from the chars dictionary.
Customization is via adding to or deleting from the chars dictionary.
<lang julia>@enum streamstate GET_FF GET_LF GET_TAB GET_CHAR ABORT
<syntaxhighlight lang="julia">@enum streamstate GET_FF GET_LF GET_TAB GET_CHAR ABORT
chars = Dict(GET_FF => ['\f'], GET_LF => ['\n'], GET_TAB => ['\t'])
chars = Dict(GET_FF => ['\f'], GET_LF => ['\n'], GET_TAB => ['\t'])


Line 836: Line 836:


stream_decode_jit(open("filename.txt", "r"))
stream_decode_jit(open("filename.txt", "r"))
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|C++}}
{{trans|C++}}
<lang scala>// version 1.2.10
<syntaxhighlight lang="scala">// version 1.2.10


import java.io.File
import java.io.File
Line 889: Line 889:
val uiList = getUserInput()
val uiList = getUserInput()
decode("theRaven.txt", uiList)
decode("theRaven.txt", uiList)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 900: Line 900:
With some modifications compared to the model.
With some modifications compared to the model.


<lang Nim>import options, sequtils, strutils
<syntaxhighlight lang="nim">import options, sequtils, strutils


type Position = tuple[ff, lf, tab, sp: int]
type Position = tuple[ff, lf, tab, sp: int]
Line 937: Line 937:
"33 0 50 0 46 0 54 0 76 0 47 0 84 2 28")
"33 0 50 0 46 0 54 0 76 0 47 0 84 2 28")


echo "theRaven.txt".decode(UiList)</lang>
echo "theRaven.txt".decode(UiList)</syntaxhighlight>


{{out}}
{{out}}
Line 945: Line 945:
{{trans|Raku}}
{{trans|Raku}}
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 998: Line 998:


my $enc = jit_encode('The slithey toves did gyre and gimble in the wabe');
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);</lang>
say my $result = "Encoded\n$enc\n\nDecoded\n" . jit_decode($enc);</syntaxhighlight>
{{out}}
{{out}}
<pre>Encoded
<pre>Encoded
Line 1,009: Line 1,009:
{{trans|C}}
{{trans|C}}
{{libheader|Phix/libcurl}}
{{libheader|Phix/libcurl}}
<!--<lang Phix>(notonline)-->
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">-- demo/rosetta/BookCipher.exw</span>
<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>
<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,077: Line 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: #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>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,085: Line 1,085:
=={{header|Python}}==
=={{header|Python}}==
{{trans|D}}
{{trans|D}}
<lang python>import sys
<syntaxhighlight lang="python">import sys


class UserInput:
class UserInput:
Line 1,146: Line 1,146:


uiList = getUserInput()
uiList = getUserInput()
decode("theRaven.txt", uiList)</lang>
decode("theRaven.txt", uiList)</syntaxhighlight>
{{out}}
{{out}}
<pre>Silence-Dogood.</pre>
<pre>Silence-Dogood.</pre>
Line 1,154: Line 1,154:
{{trans|Kotlin}}
{{trans|Kotlin}}


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(define user-input
(define user-input
Line 1,176: Line 1,176:
(define c (decode slice))
(define c (decode slice))
#:break (char=? #\! c)
#:break (char=? #\! c)
(display c))</lang>
(display c))</syntaxhighlight>


{{in}}
{{in}}
Line 1,196: Line 1,196:
Will handle any visible character in the ASCII range as well as space and new-line.
Will handle any visible character in the ASCII range as well as space and new-line.


<lang perl6>#`[
<syntaxhighlight lang="raku" line>#`[
Set srand to set the encode / decode "key".
Set srand to set the encode / decode "key".
Need to use the same "key" and same implementation
Need to use the same "key" and same implementation
Line 1,258: Line 1,258:


say "\n== Decoded: ==";
say "\n== Decoded: ==";
say jit-decode($enc);</lang>
say jit-decode($enc);</syntaxhighlight>


<pre>== Secret: ==
<pre>== Secret: ==
Line 1,289: Line 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),
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>
<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>
<lang rexx>/*REXX program extracts characters by using a book cipher (that is a text file). */
<syntaxhighlight 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).*/
parse arg iFID . /*obtain optional name of file (book).*/
if iFID=='' | iFID=="," then iFID= 'JIT.TXT' /*Not specified? Then use the default.*/
if iFID=='' | iFID=="," then iFID= 'JIT.TXT' /*Not specified? Then use the default.*/
Line 1,323: Line 1,323:
?= ? || x /*append the character to the phrase. */
?= ? || x /*append the character to the phrase. */
end /*j*/ /* [↑] display letters found in book. */
end /*j*/ /* [↑] display letters found in book. */
say '═════►' ? /*stick a fork in it, we're all done. */</lang>
say '═════►' ? /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|input|text=&nbsp; supplied to the console (terminal) by the user in response to the prompts, &nbsp; (the commas are optional):}}
{{out|input|text=&nbsp; supplied to the console (terminal) by the user in response to the prompts, &nbsp; (the commas are optional):}}
<pre>
<pre>
Line 1,355: Line 1,355:
=={{header|Tcl}}==
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
{{works with|Tcl|8.6}}
<lang tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6


oo::class create JustInTimeStreamExtract {
oo::class create JustInTimeStreamExtract {
Line 1,424: Line 1,424:
tcl::mathop::== $counter $counters(page) $counters(line) $counters(field) $counters(char)
tcl::mathop::== $counter $counters(page) $counters(line) $counters(field) $counters(char)
}
}
}</lang>
}</syntaxhighlight>
Demonstration of use:
Demonstration of use:
<lang tcl>[JustInTimeStreamExtract new] stream [open "sample.txt"]</lang>
<syntaxhighlight lang="tcl">[JustInTimeStreamExtract new] stream [open "sample.txt"]</syntaxhighlight>
<!-- no output; I'll wait for someone else to invent something to decode… -->
<!-- no output; I'll wait for someone else to invent something to decode… -->


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


Structure UserInput
Structure UserInput
Line 1,515: Line 1,515:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>Silence-Dogood.</pre>
<pre>Silence-Dogood.</pre>
Line 1,523: Line 1,523:
{{libheader|Wren-dynamic}}
{{libheader|Wren-dynamic}}
{{libheader|wren-seq}}
{{libheader|wren-seq}}
<lang ecmascript>import "io" for File
<syntaxhighlight lang="ecmascript">import "io" for File
import "/dynamic" for Tuple
import "/dynamic" for Tuple
import "/seq" for Lst
import "/seq" for Lst
Line 1,576: Line 1,576:


var uiList = getUserInput.call()
var uiList = getUserInput.call()
decode.call("theRaven.txt", uiList)</lang>
decode.call("theRaven.txt", uiList)</syntaxhighlight>


{{out}}
{{out}}
Line 1,585: Line 1,585:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|C++}}
{{trans|C++}}
<lang zkl>class FlyBy{
<syntaxhighlight lang="zkl">class FlyBy{
fcn decode(file,tuplets){
fcn decode(file,tuplets){
codePad:=File(file).read().mode(String); // blob of text
codePad:=File(file).read().mode(String); // blob of text
Line 1,609: Line 1,609:
h.split(" ").pump(List,T(Void.Read,3),
h.split(" ").pump(List,T(Void.Read,3),
fcn(ff,lf,t,s){ vm.arglist.apply("toInt") });
fcn(ff,lf,t,s){ vm.arglist.apply("toInt") });
}</lang>
}</syntaxhighlight>
<lang zkl>input:=getUserInput();
<syntaxhighlight lang="zkl">input:=getUserInput();
// our code pad is: http://paulo-jorente.de/text/theRaven.txt
// our code pad is: http://paulo-jorente.de/text/theRaven.txt
FlyBy.decode("theRaven.txt",input);</lang>
FlyBy.decode("theRaven.txt",input);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>