Conversation started Jan 10, 2018 at 18:30.
Jan 10, 2018 18:30
Welcome to the APL learning session!
We're going through the system functions. Next up are some easy constants.
⎕A is the uppercase English alphabet:
⍞←⎕A
@Adám ABCDEFGHIJKLMNOPQRSTUVWXYZ
There's no Dyalog built-in for the lowercase alphabet, but you can get it with:
⍞←819⌶⎕A
@Adám abcdefghijklmnopqrstuvwxyz
⎕D has the digits:
(temporary solution; likely to stop working at a later version :D I'd rather 'abcdefghijklmnopqrstuvwxyz' >.>)
Jan 10, 2018 18:31
⍞←⎕D
@Adám 0123456789
@EriktheOutgolfer Well, 819⌶ is likely to be replaced with a proper system function at some point.
> An event is starting 2 mins ago in APL - "APl learning session"
@Adám Why specifically 819?
Jan 10, 2018 18:33
@Pavel 819 looks like BIg :-)
@Adám Um, really?
@Pavel Yes.
...huh
Anyway, ⎕NULL is a scalar null value. It isn't really used much in APL itself, but you can meet it e.g. when importing spreadsheets where it represents empty cells.
Jan 10, 2018 18:35
What does monadic I-beam do?
@Pavel I-beam is a monadic operator.
I-beam is an operator, there's no "monadic/dyadic" ...duh Adám is so fast
@Pavel So N⌶ derives a monadic, dyadic, or ambivalent function which you can then apply. The N selects which function.
⍞←⌶
@Pavel ⌶
Jan 10, 2018 18:37
@Pavel See Lesson 4.
⍞←819⌶
@Pavel 819 ⌶
¯\_(ツ)_/¯
@Pavel That's completely analogous to:
⍞←,¨
@Adám ,¨
Jan 10, 2018 18:39
It should be noted that ⎕NULL equals itself.
These three (⎕A ⎕D ⎕NULL) are system constants; you can't assign to them.
@Adám hooray, finally a language which isn't a weirdo!
Is ⎕NULL also the .NET null?
@Pavel Yes.
@EriktheOutgolfer Other than JS, when does null ever not equal null?
@Pavel But it is not JSON null, which is represented as ⊂'null' to match true and false being ⊂'true' and ⊂'false'.
Let's go on with more advanced features.
⎕CMD and ⎕SH are exactly identical, but obviously the first feels more natural to Windows users while the second feels more natural to UNIX users. Pressing on them will give you the help appropriate for that OS.
As you of course can guess, they are used to call the OS command processor.
⍞←⎕SH'ls /'
Jan 10, 2018 18:44
@Adám  bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
@DyalogAPL TIO root directory contents.
⍞←⎕SH'ls' , '/'
@Pavel ERROR: Use ⎕← for a full error report
@Pavel You need a space after ls, no?
@Adám I forgot that the comma makes it a single character vector, I meant to have an array of two strings there
Jan 10, 2018 18:47
@Pavel Why? A UNIX command is a single string, no?
@Adám In e.g. python's equivalent of ⎕SH, you can either pass a full command, or a list containing the executable to run and then the arguments to pass in, which wouldn't be processed by the shell.
@Pavel Dyadic ⎕SH/⎕CMD starts an auxiliary processor where the processor name is the left argument and the right argument is a list of arguments.
@Adám Ah, I see. So I would use 'ls'⎕SH'/'
Um, how do I define a list with one element?
So I have a list containing '/'
I think it's
@Pavel You can just use a simple scalar or vector if you only need one.
@Pavel ,'a' is a 1-element list.
Anyway, see the full documentation for details.
⎕CSV will import and export Character Separated Values.
⎕←⎕CSV '"abc","def",3' 'S'
Jan 10, 2018 18:55
@Adám
┌───┬───┬─┐
│abc│def│3│
└───┴───┴─┘
It has a ton of options for almost anything you could want. Again, the full documentation is available by pressing F1 with the cursor on it, and also online. Now you know it exists.
Btw, it can import and export directly to and from text files.
⎕DR is Data Representation. Monadically, it will tell you how an array is represented internally, and dyadically, it allows you to convert between data types:
⍞←⎕DR 42
@Adám 83
⍞←⎕DR 1 'a' 2 3 'beth'
@EriktheOutgolfer 326
Dyalog APL data type codes have two parts, the 1's place and the rest.
The 1's place tells you which kind of data it is, the rest tells you how many bits are used to store it (with one exception).
@EriktheOutgolfer 6 means a memory pointer, and 32 means 32-bit. However, TIO really runs a 64-bit system. This is the exception; pointers are always 326 even on 64 bit systems.
The number 42 gave us 83, where 3 means integer and 8 means 8-bit.
hello
@osmelg Hi, welcome to.
Dyalog APL has single-bit Boolean arrays, so they are type 11 where the rightmost 1 means Boolean, and the leftmost 1 means 1-bit.
⍞←⎕DR 1 0 1 1 1 0
@Adám 11
Dyadic ⎕DR lets you convert between types:
⍞←11 ⎕DR 42
Jan 10, 2018 19:04
@Adám 0 0 1 0 1 0 1 0
what kinds of problems can we solve here ?
This takes the memory which was used to represent 42 and interprets it as if it was a Boolean array.
@osmelg The problem of not knowing all of Dyalog APL.
⍞←11 ⎕dr ○1
@dzaima 0 0 0 1 1 0 0 0 0 0 1 0 1 1 0 1 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 1 1 1 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0
excellent, because i need the basics
any basic examples?
Jan 10, 2018 19:07
@osmelg I'd suggest looking back at the first lessons
thats it :D
ty erik
@osmelg alternatively, you can also use this to get a very, very basic understanding (beware hurts eyes)
jejeje let me check it
You can also combine two steps of ⎕DR into one. A two-element left argument will interpret the right argument as that type, then convert it to the type given by the second element of the left argument.
⎕FMT is ForMaT. It is like a beefed up version of .
returns a vector for single line arguments, and a matrix otherwise. ⎕FMT always returns a matrix.
Also, treats control characters as normal characters, while ⎕FMT will resolve them:
⍞←'abc',(⎕UCS 8),'def' ⍝ 8 is backspace
@Adám abcdef
Jan 10, 2018 19:13
⍞←⍕'abc',(⎕UCS 8),'def' ⍝ 8 is backspace
@DyalogAPL Hello?
⍞←⍕'abc',(⎕UCS 8),'def' ⍝ 8 is backspace
Oh well.
@DyalogAPL WAKE UP before I slap you harshly on your face!
⍞←⎕FMT 'abc',(⎕UCS 8),'def' ⍝ 8 is backspace
@Adám abdef
see? you need to discipline
⍞←⍴⎕FMT'abc',(⎕UCS 8),'def' ⍝ 8 is backspace
Jan 10, 2018 19:15
@Adám 1 5
So you see that the c really was erased by the backspace.
Dyadic ⎕FMT gives you access to a whole new language, namely a formatting specification language. I won't go though all the details here (see docs!), but here's a taste:
⎕←'I3,F5.2' ⎕FMT 2 4⍴⍳8
@Adám
  1 2.00  3 4.00
  5 6.00  7 8.00
The formatting string I3,F5.2 means that each row should first have an integer, then a float which uses five characters in width and has 2 decimals, then this formatting is cycled as much as needed for all the columns (here twice).
@all Anyone up for guessing what ⎕JSON does?
Does dyalog have dictionaries?
@Pavel Dyalog has namespaces which work pretty much the same.
Jan 10, 2018 19:23
⍞←⎕SH¨'ls' '..' 'ls'
@RosLuP ERROR: Use ⎕← for a full error report
is .. a valid command
@RosLuP .. isn't a valid UNIX command, is it?
ninja'd.
⍞←⎕SH¨'ls' 'ls'
@RosLuP
Jan 10, 2018 19:25
@RosLuP Try with ⎕←
you need ⎕← not ⍞←
ninja'd
So ⎕JSON imports/exports JSON, of course. It works for both arrays and objects:
⎕←⎕JSON'[[42,null],"hello"]'
@Adám
┌───────────┬─────┐
│┌──┬──────┐│hello│
││42│┌────┐││     │
││  ││null│││     │
││  │└────┘││     │
│└──┴──────┘│     │
└───────────┴─────┘
⎕←⎕SH¨'ls' 'ls'
@RosLuP
┌┬┐
│││
│││
│││
└┴┘
Jan 10, 2018 19:27
⎕←ns←⎕JSON'{"abc":42,"de":null,"f":"hello"}' ⋄ ⎕←ns.(abc f)
@Adám
#.[JSON object]
┌──┬─────┐
│42│hello│
└──┴─────┘
@RosLuP I think the current directory is empty. Try giving a path to ls.
And export:
⍞←⎕JSON ('abc' 1 2 3)4 5
@Adám [["abc",1,2,3],4,5]
Just be aware that if you want to convert and APL string to JSON, you need use the left argument to specify whether you want import (0) or export (1).
⎕←⎕SH 'ls ../'
Jan 10, 2018 19:30
@RosLuP

Rebuilding user command cache... done
ls: cannot open directory '../': Permission denied
⍎DOMAIN ERROR: Command interpreter returned failure code 2
 __field_initialize_result_←(⎕NS ⍬).⍎'⎕CY''salt''⋄⎕SE.UCMD''←box on -fns=on -trains=tree''⊣enableSALT⋄''''' ⋄ ⎕←⎕SH'ls ../'
                                                                                                              ∧

Real time: 1.260 s
User time: 1.120 s
Sys. time: 0.069 s
CPU share: 94.31 %
Exit code: 0
You can also tell ⎕JSON that you want your JSON fully white-spaced:
⎕←⎕JSON⍠'Compact'0⊢('abc' 1 2 3)4 5
@Adám
[
  [
    "abc",
    1,
    2,
    3
  ],
  4,
  5
]
@DyalogAPL eugh
Finally, while you can import any JSON object, not every APL namespace can be exported. E.g. a namespace with APL functions cannot be converted to JSON. (Would be cool though if they were converted to JavaScript objects with the equivalent JavaScript functions…)
@EriktheOutgolfer Well, you don't have to.
Again, ⎕JSON has some more advanced options — see the docs.
What is "JSON" and if it is a library, what is the language it first it was implemented? Thanks
Jan 10, 2018 19:36
I think it means JavaScript Object Notation or something?
@RosLuP JSON is a widely used (especially online) notation for objects and arrays.
@EriktheOutgolfer Yes, but it is not identical to JavaScript's object notation.
@Adám well of course no why would APL rely on something so JS-y
(There is valid JSON which is not valid JavaScript, and JavaScript object notation which is not valid JSON.)
@EriktheOutgolfer ⎕JSON is fully compliant with JSON, though, but we do allow some leniency which allows you to create some JavaScript objects which are not valid JSON.
E.g.:
⍞←⎕JSON 'hello' (⊂'world')
@Adám ["hello",world]
⎕←⎕JSON 'hello',0,'world'
Jan 10, 2018 19:42
@RosLuP
["h","e","l","l","o",0,"w","o","r","l","d"]
@RosLuP JSON doesn't have the concept of a scalar character, so APL does its best to give you something useful.
⎕←⎕JSON ('hello' 0 'world')
@Pavel
["hello",0,"world"]
⎕←⎕JSON ⎕NULL
@Pavel

Rebuilding user command cache... done
⍎DOMAIN ERROR: The right argument cannot be converted to JSON (⎕IO=1)
 __field_initialize_result_←(⎕NS ⍬).⍎'⎕CY''salt''⋄⎕SE.UCMD''←box on -fns=on -trains=tree''⊣enableSALT⋄''''' ⋄ ⎕←⎕JSON ⎕NULL
                                                                                                              ∧

Real time: 1.046 s
User time: 0.944 s
Sys. time: 0.047 s
CPU share: 94.70 %
Exit code: 0
Jan 10, 2018 19:44
Aww
@Pavel As I mentioned above, you need to use ⊂'null' for that.
@Adám I remember, but I still was curious if using ⎕NULL did anything.
@Pavel We opted for a generalised system for strings without quotes, rather than special casing null. The i-beam that preceded ⎕JSON did in fact use ⎕NULL. By using enclosed strings, we can losslessly roundtrip.
⎕MAP is a function I'll only mention and not demonstrate (again, see docs) because TIO makes this hard. It basically allows you to use a file as an array instead of keeping the array in memory. Very useful.
⎕←⎕JSON ('hello' , 'world')
@RosLuP

Rebuilding user command cache... done
⍎DOMAIN ERROR: Invalid character at offset 1 (⎕IO=1)
 __field_initialize_result_←(⎕NS ⍬).⍎'⎕CY''salt''⋄⎕SE.UCMD''←box on -fns=on -trains=tree''⊣enableSALT⋄''''' ⋄ ⎕←⎕JSON('hello','world')
                                                                                                              ∧

Real time: 1.041 s
User time: 0.947 s
Sys. time: 0.052 s
CPU share: 96.07 %
Exit code: 0
Jan 10, 2018 19:48
@RosLuP That's a simple character vector, so ⎕JSON defaults to converting from JSON, but it isn't valid JSON, hence the error.
@RosLuP you need left argument 1
⍞←1⎕JSON'hello','world'
@EriktheOutgolfer "helloworld"
Great for ensuring proper escaping:
⍞←1 ⎕JSON 'abc',(⎕UCS 9 10),'def'
@Adám "abc\t\ndef"
This brings us to ⎕UCS which in its monadic form flips characters and their Unicode code points:
⍞←⎕UCS 954 945 955 951 956 941 961 945
Jan 10, 2018 19:51
@Adám καλημέρα
(Our documentation manager lives in Greece.)
The dyadic form takes a left argument specifying an encoding scheme and converts to and from byte values rather than code points:
⍞←'UTF-8' ⎕UCS 206 179 206 181 206 185 206 177 32 207 131 206 191 207 133
@Adám γεια σου
⎕←2⎕JSON ('hello' , 'world')
@RosLuP

Rebuilding user command cache... done
⍎DOMAIN ERROR: Invalid left argument
 __field_initialize_result_←(⎕NS ⍬).⍎'⎕CY''salt''⋄⎕SE.UCMD''←box on -fns=on -trains=tree''⊣enableSALT⋄''''' ⋄ ⎕←2 ⎕JSON('hello','world')
                                                                                                              ∧

Real time: 1.049 s
User time: 0.953 s
Sys. time: 0.045 s
CPU share: 95.13 %
Exit code: 0
@RosLuP Just 0 and 1 for import and export.
Jan 10, 2018 19:58
⍞←⎕JSON 'hello' ⊂'world'
@RosLuP ERROR: Use ⎕← for a full error report
⎕VFI is Verify and Fix Input. It takes a string and returns two lists. It cuts the string into space separated fields. Then it attempts to convert each field to a number. If it succeeds then the corresponding element of the left result list is 1 (else 0) and the corresponding element of the right list is the number (else 0).
@RosLuP You need to stop from taking 'hello' as left argument:
⍞←⎕JSON 'hello' (⊂'world')
@Adám ["hello",world]
⎕←⎕VFI '123 four 42'
@Adám
┌─────┬────────┐
│1 0 1│123 0 42│
└─────┴────────┘
Jan 10, 2018 20:00
You can also specify one or more valid field separators as left argument:
⎕←';/'⎕VFI '123 four,42 5/2/4'
@Adám
┌─────┬─────┐
│0 1 1│0 2 4│
└─────┴─────┘
Here 123 four were grouped because space is not a separator anymore, and so it is an invalid number. So too with 42 5. Only 2 and 4 were valid.
You can get just the valid numbers with:
⎕←//';/'⎕VFI '123 four,42 5/2/4'
@Adám
┌───┐
│2 4│
└───┘
2 messages moved to trash
⎕XML is converts to and from XML, but the corresponding APL format is rather involved. I usually just use ⎕XML to verify that my XML is valid or to normalise whitespace:
⍞←⎕JSON 'h' 'e' 'l' 'l' 'o'
Jan 10, 2018 20:04
@RosLuP ERROR: Use ⎕← for a full error report
⎕←⎕XML⍣2 ⊢ '<xml><document id="001">An introduction to XML</document></xml>'
@Adám
<xml>
  <document id="001">An introduction to XML</document>
</xml>
And that concludes the lesson for today.
 
Conversation ended Jan 10, 2018 at 20:07.