Just in time processing on a character stream: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 15: | Line 15: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<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)</ |
decode(‘theRaven.txt’, uiList)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 88: | Line 88: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<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; |
||
}</ |
}</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}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 387: | Line 387: | ||
} |
} |
||
} |
} |
||
}</ |
}</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] |
||
< |
<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}} |
||
< |
<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); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Silence-Dogood.</pre> |
<pre>Silence-Dogood.</pre> |
||
Line 532: | Line 532: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 610: | Line 610: | ||
log.Fatal(err) |
log.Fatal(err) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 619: | Line 619: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<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); |
||
} |
} |
||
}</ |
}</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''' |
||
< |
<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</ |
[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. |
||
< |
<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++}} |
||
< |
<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) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 900: | Line 900: | ||
With some modifications compared to the model. |
With some modifications compared to the model. |
||
< |
<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)</ |
echo "theRaven.txt".decode(UiList)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 945: | Line 945: | ||
{{trans|Raku}} |
{{trans|Raku}} |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<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);</ |
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}} |
||
<!--< |
<!--<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,085: | Line 1,085: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="python">import sys |
||
class UserInput: |
class UserInput: |
||
Line 1,146: | Line 1,146: | ||
uiList = getUserInput() |
uiList = getUserInput() |
||
decode("theRaven.txt", uiList)</ |
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}} |
||
< |
<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))</ |
(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 |
<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);</ |
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; it has no ''FF'' (''formfeed'') characters in it), |
The input file used by this REXX program only contains one page; it has no ''FF'' (''formfeed'') characters in it), |
||
<br>and the injection of ''FF'' characters into the file would be like putting pencil marks into a holy book. <big><big><big> ☺ </big></big></big> |
<br>and the injection of ''FF'' characters into the file would be like putting pencil marks into a holy book. <big><big><big> ☺ </big></big></big> |
||
< |
<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. */</ |
say '═════►' ? /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|input|text= supplied to the console (terminal) by the user in response to the prompts, (the commas are optional):}} |
{{out|input|text= supplied to the console (terminal) by the user in response to the prompts, (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}} |
||
< |
<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) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Demonstration of use: |
Demonstration of use: |
||
< |
<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#}} |
||
< |
<syntaxhighlight lang="vbnet">Module Module1 |
||
Structure UserInput |
Structure UserInput |
||
Line 1,515: | Line 1,515: | ||
End Sub |
End Sub |
||
End Module</ |
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}} |
||
< |
<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)</ |
decode.call("theRaven.txt", uiList)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,585: | Line 1,585: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<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") }); |
||
}</ |
}</syntaxhighlight> |
||
< |
<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);</ |
FlyBy.decode("theRaven.txt",input);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |