How to write special characters in Julia code - julia

Recently I have seen Julia code like this with special characters:
How can I write Julia code like this? Do I need to have a special editor or can I do this also with e.g. Notepad++?

Julia allows you to use various Unicode symbols for variable and function names, and it makes sense in a lot of the scientific domains Julia is used in, making the code be more alike the scientific notation. (It's sometimes called "executable math" for this reason, though at this point it's more like "executable scientific notation".)
However, in any new code you write, it's not generally necessary to use these symbols, and you can write the code with the basic ASCII characters. Julia language and its standard libraries always provide ASCII equivalents for the Unicode functions they use.
For eg., ⊻ is the XOR character, and you can write 2 ⊻ 11 to get the XOR of the two numbers. But you can also instead write xor(2, 11) to get the same result, and they're exactly equivalent to the language. It's considered good practice for external packages as well, to provide their interface in terms of ASCII-typable names to the extent possible, even if internally Unicode-only names are used.
With that said, if you find you do need to work with code that uses such Unicode symbols, there are a few options:
Option 1: Autohotkey-script for converting LaTeX-like input to unicode characters.
Usually, the way to enter these character in Julia is using LaTeX-like syntax, for eg. \epsilon for ϵ. The AutoHotKey script provided here gives somewhat similar functionality, allowing you to type LaTeX-like input and converting it automatically into the corresponding symbols.
Option 2: A copy-paste list of symbols
This is pretty ad-hoc, but can work for situations where you just need to edit some existing code. You can use a clipboard manager like ClipX, or Win10's inbuilt one, to keep a copy of all the symbols the script uses. At any point, you can choose and insert the particular symbol you need.
Option 2a: Copy-paste from the REPL/Jupyter/Pluto
I'll put this option too here since it uses the clipboard as well. This makes use of the usual workflow for writing Julia code - you'll probably want to try out the code in the REPL or in a notebook anyway, so you can use the tab-completion features there, and then copy that code into your editor. Julia has a clipboard function too, so after trying out median(sin.(-ℯ:0.01:π)) in the REPL, you can surround that in triple quotes and pass it to clipboard like this: """median(sin.(-ℯ:0.01:π))""" |> clipbaord from the REPL itself.
Option 3: Use JuliaEditorSupport/julia-NotepadPlusPlus
This is sort of the "official" option, but only the syntax highlighting file has been updated recently, and the quality and reliability of the Autohotkey script here isn't clear. I'd be hesitant to recommend this option.
Option 4: Use a better supported editor
It's hard to let go of familiar tools, but in this case, this is probably the most reliable option long term. Julia for VSCode is probably the best supported editor right now, but the plugins for Vim and Emacs are also pretty good. If VS Code is too heavy (and Vim and Emacs unfamiliar), you can also try the plugin for Sublime Text which should be both easy to use and light.

Related

R command dir.create and file.path

I've just started learning r and confused by the following question given in the course:
Create a directory in the current working directory called “testdir2” and a subdirectory for it called “testdir3”, all in one command by using dir.create() and file.path().
I couldn't get it to accept my answer and then found another site online giving the answers. This is the answer the other site gave:
dir.create(file.path('testdir2', 'testdir3'), recursive = TRUE)
After copy/pasting this answer it still didn't let me progress in the course. Is there something wrong with the answer?
Also why would i want to use file.path to create the folders. Would it not make more sense to do this:
dir.create("testdir2/testdir3", recursive = TRUE)
What is the purpose of using the file.path function to create folders?
The purpose of using file.path() to create folders is so that you can write a function, script, or package that can be used by people that are using different kinds of computers. Different kinds of computers, or Platforms, use different file separators. Unix systems use the forward slash: /. This includes macs. Windows systems use the back slash: \.
Try looking at .Platform and you'll see there are a number of variables that you can refer to in order to create platform independent code. .Machine is another one.
Anyhow, the idea is that file.path(dir1, dir2, dir3) can create a valid path no matter what kind of platform R is running on.
As to why an auto grader didn't accept your answer, they can be very finicky, especially about hidden whitespace characters you can sometimes pick up when copying and pasting. Sometimes they test the output that your command produces, but sometimes (bad) auto graders just test the input, so even if your command would produce the same behavior, if it's not exactly the same, the auto grader won't accept it.
I got this code to work:
dir.create(file.path("testdir2","testdir3"), recursive = TRUE)
I think you just needed double quotes around "testdir2" and "testdir3".

What's an easier way to do tab completion of many superscripts?

I know I can type x\^n and tab to get xⁿ.
But how can I input xᵃ⁺¹ easily, without doing x\^a tab \^+ tab \^1 tab.
I tried using brackets, that doesn't work.
At the moment, there's no shortcut for expanding extended latex superscripts or subscripts via tab expansion. Julia is not actually interpreting the latex you write at tab time – all the symbols are hard-coded in the Julia source as a mapping from simple latex to unicode (you can find them all here). A better approach might be to define some keyboard shortcuts to write unicode sub/superscripts directly.

Trouble of understanding the concept of packages in Common Lisp

A time ago I started learning Common Lisp, but now I have come to my first real stumbling block, understanding a concept. I started to change my learning projects to move from single file sources to packages. Everything so far went as expected, but then, I stumbled upon one file, a sudoku game I coded, that behaves other then I thought. You can find it here: https://github.com/Silberbogen/cl-sudoku
When I started (spiele-sudoku) after I switched inside the package via (in-package :cl-sudoku), everything works fine, but when I start it via (cl-sudoku:spiele-sudoku), only my input of coordinates is excepted, while any other input seems not to be interpreted.
What concept do I miss, so I could start the game via (cl-sudoku:spiele)?
You use read-from-string to read your input. That will intern any word encountered as a symbol into the current package.
In your main function, you use case to compare with symbols, but those are interned into the cl-sudoku package. So, if your current package is cl-sudoku, it will work, otherwise not.
You should not use read or read-form-string to parse user input (if you absolutely must, at least bind *read-eval* to nil). Instead call intern yourself (possibly in combination string-upcase) to create symbols in the right package. If you want to use package-independent symbols, intern them into the KEYWORD package, so that you can do case on keywords.
It might be helpful to use ecase or ccase, or at least log some debug information on invalid input.

Dribble in R: how to duplicate all i/o into a file?

Is there something like the Common Lisp function DRIBBLE or Unix command tee in R?
Specifically, I want everything I type and everything R prints back to me to be appended to a file (tee only captures stdout; I want everyting: errors, warnings, print, cat, my input).
I found a 10 year old message on the subject which offers a weak version of that (it does not capture the output from cat/print).
The standard function sink only captures (not duplicates) the R's output; and it does not capture my input.
Is there a better way?
Look at the txtStart function (and related functions) in the TeachingDemos package. I think that it does everything you want except capturing errors (and the TaskCallback system in the R guts needs to be updated for that to happen).
The other option is to run R inside of another environment such as ESS (inside of Emacs) (there are others, but I am less familiar with them). Then everything is captured in the editor/buffer and can be saved to a file.
Note that the sink function does have a split argument that works like tee to show the output on screen as well as duplicate it to the file, but it still only does the output, not the input commands.
You probably want sink() -- see help(sink) for examples.

How to convert R expressions to LaTeX/TeX without using a CAS?

I would like to be able to convert R expressions (ex. 1/2*x^2) or formulas into a character string that contains the LaTeX (ex. "\frac{1}{2} \times x^{2}" - or something similar). I know that expression() can be used for something similar, generating mathematical notation in figures, but I'm not sure how to convert a similar type of expression into LaTeX. I want to be able to include this into a Sweave document.
Please give an example (like with the 1/2*x^2), instead of just saying "use the tikzDevice package". It sounds like the Hmisc package might be able to do what I want, but I haven't figured out how yet.
I'm wanting to avoid using a CAS (computer algebra system), because I want it not have to rely on a complex external application, like that Ryacas uses, and want it to be able to run quickly.
latexTranslate provides valid LaTeX output from string input but only partial expansion. The translation of x/y will not be converted to the prefix version, \frac {1} {2}.
require(Hmisc)
latexTranslate("1/2*x^2")
#[1] "1/2*x$^{2}$"

Resources