TryAPL: Difference between revisions

Jump to navigation Jump to search
3,458 bytes added ,  10:33, 11 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
m (Text replacement - "<source" to "<syntaxhighlight")
 
(7 intermediate revisions by 2 users not shown)
Line 14: Line 14:
=== TryAPL Mini ===
=== TryAPL Mini ===
[https://janiczek.github.io/tryapl-elm/ TryAPL Mini] is an alternative web interface written in [[wikipedia:Elm (programming language)|Elm]], focusing on exploration of [[primitive]]s. Half of the screen is used to display information about whichever [[glyph]] the user last hovered their mouse over on the built-in [[Typing_glyphs#Web|language bar]].
[https://janiczek.github.io/tryapl-elm/ TryAPL Mini] is an alternative web interface written in [[wikipedia:Elm (programming language)|Elm]], focusing on exploration of [[primitive]]s. Half of the screen is used to display information about whichever [[glyph]] the user last hovered their mouse over on the built-in [[Typing_glyphs#Web|language bar]].
The source code is available [https://github.com/janiczek/tryapl-elm/ on GitHub].
The source code is available [https://github.com/janiczek/tryapl-elm/ on GitHub].


Line 24: Line 25:


Creating a [[wikipedia:bookmarklet|bookmarklet]] with the following URL, will enable clicking on the bookmark to pop up a box wherein one can enter an APL expression, which will then be executed, and the result shown in another pop-up:
Creating a [[wikipedia:bookmarklet|bookmarklet]] with the following URL, will enable clicking on the bookmark to pop up a box wherein one can enter an APL expression, which will then be executed, and the result shown in another pop-up:
<source lang=js>
<syntaxhighlight lang=js>
javascript:((()=>{with(new XMLHttpRequest){open(`POST`,`tryapl.org/Exec`);setRequestHeader(`Content-Type`,`application/json;charset=utf-8`);send(JSON.stringify([0,0,0,prompt()]));onload=(_=>alert(eval(responseText)[3].join`\n`+`\n`))}})())</source>
javascript:((()=>{with(new XMLHttpRequest){open(`POST`,`https://tryapl.org/Exec`);setRequestHeader(`Content-Type`,`application/json;charset=utf-8`);send(JSON.stringify([0,0,0,prompt()]));onload=(_=>alert(eval(responseText)[3].join`\n`+`\n`))}})())</syntaxhighlight>


=== Chat box exec ===
=== Chat box exec ===
Line 31: Line 32:


=== Chatbot ===
=== Chatbot ===
[[wikipedia:Stack Exchange|Stack Exchange]] moderator "hyper-neutrino" hosts a [[wikipedia:chatbot|chatbot]] using TryAPL's name and icon, active in two Stack Exchange chat rooms; the [[APL Orchard]] and the Stack Exchange's [[sandbox (software_development)|sandbox]] chat room. To use it, write [[APL Orchard#inline|inline code]] or a [[APL Orchard#Multi-line messages|multi-line code block]], and prepend <source lang=apl inline>⎕←</source> or <source lang=apl inline>⋄</source> to lines you wish to run, in any of the two chat rooms:<ref>For details, see [https://codegolf.stackexchange.com/users/75949 the chat bot's profile].</ref>
[[wikipedia:Stack Exchange|Stack Exchange]] moderator "hyper-neutrino" hosts a [[wikipedia:chatbot|chatbot]] using TryAPL's name and icon, active in two Stack Exchange chat rooms; the [[APL Orchard]] and the Stack Exchange's [[sandbox (software_development)|sandbox]] chat room. To use it, write [[APL Orchard#inline|inline code]] or a [[APL Orchard#Multi-line messages|multi-line code block]], and prepend <syntaxhighlight lang=apl inline>⎕←</syntaxhighlight> or <syntaxhighlight lang=apl inline>⋄</syntaxhighlight> to lines you wish to run, in any of the two chat rooms:<ref>For details, see [https://codegolf.stackexchange.com/users/75949 the chat bot's profile].</ref>


* [https://apl.chat APL Orchard]
* [https://apl.chat APL Orchard]
Line 45: Line 46:
=== API ===
=== API ===


Requests to TryAPL's backend consist of submitting a [[wikipedia:POST (HTTP)|POST]] request to https://tryapl.org/Exec containing a 4-element list <source lang=js inline>["state", size, "hash", "input"]</source> where the first three elements can use the placeholder values <source lang=js inline>""</source> or <source lang=js inline>0</source>. The server responds with a similar 4-element list <source lang=js inline>["state", size, "hash", ["lines", "of", "output"]]</source>. The first three elements are kept on the front-end and sent back with the next request, or cleared to restart with default state.
Requests to TryAPL's backend consist of submitting a [[wikipedia:POST (HTTP)|POST]] request to https://tryapl.org/Exec containing a 4-element list <syntaxhighlight lang=js inline>["state", size, "hash", "input"]</syntaxhighlight> where the first three elements can use the placeholder values <syntaxhighlight lang=js inline>""</syntaxhighlight> or <syntaxhighlight lang=js inline>0</syntaxhighlight>. The server responds with a similar 4-element list <syntaxhighlight lang=js inline>["state", size, "hash", ["lines", "of", "output"]]</syntaxhighlight>. The first three elements are kept on the front-end and sent back with the next request, or cleared to restart with default state.


If the output begins with a backspace character (U+08) then the actual output only begins after the second backspace character, and the text between the two backspace characters describes the role of the text. As 27 Jun 2021, only one tag has been implemented, <source lang=js inline>"\bhelp\b"</source> for which the text is the URL of help page requested by <source lang=apl inline>]Help</source>
If the output begins with a backspace character (U+08) then the actual output only begins after the second backspace character, and the text between the two backspace characters describes the role of the text. As 27 Jun 2021, only one tag has been implemented, <syntaxhighlight lang=js inline>"\bhelp\b"</syntaxhighlight> for which the text is the URL of help page requested by <syntaxhighlight lang=apl inline>]Help</syntaxhighlight>


A minimal (no-state) TryAPL front-end can be implemented as follows:
==== XMLHttpRequest ====
<source lang=js>
A minimal (no-state) TryAPL front-end can be implemented as follows using the [[wikipedia:XMLHttpRequest|XMLHttpRequest]] API:
<syntaxhighlight lang=js>
with(new XMLHttpRequest) {
with(new XMLHttpRequest) {
open("POST", "tryapl.org/Exec");
open("POST", "https://tryapl.org/Exec");
setRequestHeader("Content-Type", "application/json;charset=utf-8");
setRequestHeader("Content-Type", "application/json;charset=utf-8");
send(JSON.stringify([0, 0, 0, prompt()]));
send(JSON.stringify([0, 0, 0, prompt()]));
onload = (_ => alert(eval(responseText)[3].join("\n") + "\n"))
onload = (_ => alert(eval(responseText)[3].join("\n") + "\n"))
}
}
</source>
</syntaxhighlight>
==== Fetch ====
This is a function that uses the [[wikipedia:XMLHttpRequest#Fetch_alternative|Fetch]] API to send a request given as input a string code:
<syntaxhighlight lang=js>
async function executeAPL(code) {
  const res = await fetch("https://tryapl.org/Exec", {
    method: 'POST',
    headers: { 'Content-Type': 'application/json; charset=utf-8' },
    body: JSON.stringify(['', 0, '', code]),
  });
  const data = await res.json(); // data contains the server response
  return data[3]; // return the output of the function
}
 
// To call the function you must use an async/await statement.
(async () => {
  let resultSum = await executeAPL(`2+2`);
  let reshape = (await executeAPL(`2 2⍴⍳${resultSum}`))
    .map(row => row.split` `.map(x => +x));
  let sumReduce = (await executeAPL(`+/ (↑⍣≡0∘⎕JSON) '${JSON.stringify(reshape)}'`))
    .map(row => row.split` `.map(x => +x));
  console.log(sumReduce);
})();
</syntaxhighlight>
 
If you want to load a namespace containing some functions, you have to change state, size and hash variables. To have these parameters just go on https://tryapl.org/ [Developer Tools > Network]. Here, after typing on TryAPL <syntaxhighlight lang="js" inline>increment ← 1∘+</syntaxhighlight>, will appear a file named Exec: in the Response tab there will be a JSON containing the three values.
 
<syntaxhighlight lang="js">
let state = 'c-ocK2UHbD6oBCo<$(l+5DX+5l%hycQBgoav5N>QHY|XESU^xIb|oSjO|?X0LNtOHO=4n-(R6#oE_My}-g`Iy<?ZtvR#CI(eCO`od3$GPXJ%hfzNIR8l+&V_owJ?9ysT%MiVq7d?&(<aep5a3uIjnR6YWUVt}B}AM3%f?-)R}8CL8ZJUK>n#e`9{2N>TADRVAooq^NXV9n<fpXmNIAVVFavQvRm#>iV;BdD^dfm2ul@(Lw9TT3@e{G3I#XPRhwP1nK)xFX{hj-u}gTt5WKyQhr}rzU)iJYpxrwzD`y5S9@;;T^-XrWSi>Pt7j&{QX6LlRMXzILF=P3%A=vHQb`o6&bd6wD6TUQ!+9*?5Rbk{KtJ@yfa0~JR3efv2+2snU<|=f$ofr#tg{RZ!*GniNa;^|WEsyA)RtQG??bGMdZ-UyG?3?opE9n;0PX$fF?S1yZkR9EGEQ>Mh|SRgEzt_C(FSc1f_4Z+dxRkz9U!m26C%(VT_9Vv8@fZbVGs1w=2GVQ5tbuU+8Dzs;%cnH25iJ;6krRsVjH$&2X<jM3T6Ir00(ghNAMUYa1mE=9k(zVi<#iDGJeMGN8gJ$Ux~F?hxIaN<Tnv_VlVdNFp6*#$8a1caSEq#24`^&=Wzj-a2Z!{4ZTnYO{w3T7)%V5HN&$6$+{snA@(8Ggga_+?S(toC-=%)C7;f{<2aw@tkevoab3XT^py3?xfLw<oukwumqnZa4|vKtKt9IM))?Nh4!I{45eP*Hf-sBU#SjDFEb~hYm;Hc5#L^yz8?@cTZQO-?Dri<tsDY($<QZ*Y4SNL2hC&s%!57{zLoiMEa32p4jfe7nq%Osm5gW-qp_3KFT$xAmQ+degh$8PnE=u-2=3yabl8;8bt8p(Be-~VI-vux4M!k9)EXtHOGcRj-;C{I+`}PM}AFS~E-KF2GUR3)_pStzx`!;Ck=O1u&)x5m?#b=r|Z_%<<>o#pe+J&|c3-8deQ$*)3UAuLU?9nr4?!61PmtMDf!@km+4vufVUAf9TPF1TpySTblui;+P!*le?u}fke+-TY)@Zr0)4qb`9K0Y=sK5IgD--Jp1`VSbGm^A3U<dnffCVw|&XlmN~=^uRf(Z|C+nL2Iyj8A9I8vfbr&qsVQa(CgVJ>Tv<d+z+jsEpUf-1_nMPr=K-|LyX^umAdc!OmT|^Z)pBQ{!<f{<-<<GONsjjdQ-*uzuURt$pr1-uz_Zj_q~Uto`{1n`;#x9XtK+k)p%LPn<e=^cU0j*v9F3|67(m`tSQhGxfiY{{TCxArt';
let size = 2090;
let hash = 'b#I;?EZD=8s=YfFKk=g-u;6Uc`dwiu3Val5Gt`%rAhCWd4~6Z_WwqIp<R`FEoA*lr*Z0=uC*HY#_2JbY';
 
async function executeAPL(code) {
  const res = await fetch("https://tryapl.org/Exec", {
    method: 'POST',
    headers: { 'Content-Type': 'application/json; charset=utf-8' },
    body: JSON.stringify([state, size, hash, code]),
  });
  const data = await res.json();
  return data[3];
}
 
(async () => {
  let result = await executeAPL(`increment 27`);
  console.log(result);
})();
</syntaxhighlight>


== In media ==
== In media ==

Navigation menu