Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Julia interaction


May 14, 2021 Julia


Table of contents


Interaction

Julia has a full-featured interactive command line REPL (read-eval-print loop) built into the julia I n addition to allowing for quick and easy evaluation of Julia statements, he also has a searchable history feature, tab-filling, and more useful shortcuts, and specialized help, as well as shell mode. REPL can be started with a simple argumentless call or double-click execution:

    $ julia
                   _
       _       _ _(_)_     |  A fresh approach to technical computing
      (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
       _ _   _| |_  __ _   |  Type "help()" to list help topics
      | | | | | | |/ _` |  |
      | | |_| | | | (_| |  |  Version 0.3.0-prerelease+2834 (2014-04-30 03:13 UTC)
     _/ |\__'_|_|_|\__'_|  |  Commit 64f437b (0 days old master)
    |__/                   |  x86_64-apple-darwin13.1.0

    julia>

If you want to exit the interactive ^D tap the control key plus the d key - or edit quit() and then tap the return key. REPL will julia> hint.

Different prompt modes

Julia mode

THEPL has four main operating modes. T he first and most common one is the Julian tip. I t is the default mode of julia> H ere you can enter the Julia expression. Knocking back after a complete expression has been entered evaluates the entry and displays the results of the last expression.

    julia> string(1 + 2)
    "3"

There are many unique features to interact with. I n addition to displaying the results, rePL also binds the results to ans The sign at the end of the line can be used as a flag to suppress the display results.

    julia> string(3 * 4);

    julia> ans
    "12"

Help mode

When the pointer is at the beginning of a line, the ? prompt changes to help mode. Julia will try to print help or documentation in help mode:

    julia> ? # upon typing ?, the prompt changes (in place) to: help>

    help> string
    Base.string(xs...)

       Create a string from any values using the "print" function.

In addition to the method name, the completed method call can see which method was called by the specified parameter. Macros, types, and variables can also be queried.

    help> string(1)
    string(x::Union(Int16,Int128,Int8,Int32,Int64)) at string.jl:1553

    help> @printf
    Base.@printf([io::IOStream], "%Fmt", args...)

       Print arg(s) using C "printf()" style format specification
       string. Optionally, an IOStream may be passed as the first argument
       to redirect output.

    help> String
    DataType   : String
      supertype: Any
      subtypes : {DirectIndexString,GenericString,RepString,RevString{T<:String},RopeString,SubString{T<:String},UTF16String,UTF8String}

想要退出帮助模式可以在一行的开始按下退格键。

Shell mode

Help mode is useful for quick access to documents, and another common task is to use system shells to execute system commands. J ust as when the cursor is at the beginning ? to enter help mode, you can (;) using a sign ( s) . And when you want to exit mode, you can press the backbar at the beginning of a line.

    julia> ; # upon typing ;, the prompt changes (in place) to: shell>

    shell> echo hello
    hello

Find mode

In all of the above methods, all execution lines are saved to the history file and can be found. I n order to initialize an incremental search of the previous history, tap ^R key plus the r key r keyboard. T he prompt is changed (reverse-i-search)': and as you tap, the query request will appear in the reference. T he most recent result that meets the matching requirements will be dynamically updated in the console on the right. If you want to find older results, use the same query, and then tap ^R

^R is a reverse query, while ^S a positive query with a prompt of (i-search)': . The two can be used together to move relatively to the previous or 1 match.

Key binding

Julia REPL is a good use of key binding. T here are a number of control key bindings described ^D (for exit, for queries for s ^R ^S but there are many more key bindings here. I n addition to the control keys, there are many meta-key bindings. These key bindings vary from platform to platform, but most terminals use alt - or option - by default - to select a key to send meta-keys (or by configuration).

Program control
^D Exit (when buffer is empty)
^C Interrupt or cancel
Return/Enter, ^J The new line and executes the last line if the last line is completed
meta-Return/Enter A new line is not executed
? or ; Enter help or shell mode (at the beginning of a line)
^R, ^S Incremental history search
Cursor movement
Right arrow, ^F Move one character to the right
Left arrow, ^B Move one character to the left
Home, ^A Move to the beginning of the line
End, ^E Move to the end of the line
^P Change the previous or next history entry
^N Change to the next history entry
Up arrow Move to the previous line (or previous history entry)
Down arrow Move to the next line (or a later history entry)
Page-up A historical entry that matches the text before switching to the previous cursor
Page-down A historical entry that matches the text before switching to the next cursor
meta-F Move a word to the right
meta-B Move a word to the left
Editing
Backspace, ^H Delete the previous character
Delete, ^D Remove a character back (when the buffer has text)
meta-Backspace Delete the previous word
meta-D Delete a word back
^W Delete all text that was previously blank until recently
^K Kill to the end of the line and place the text in the buffer
^Y Insert text from the kill buffer
^T Swap characters based on the cursor
Delete, ^D Remove one character back (when there is text in the buffer)

Custom shortcuts

Julia REPL shortcuts can be REPL.setup_interface() the dictionary type is passed to the user. T he dictionary keyword can be a character, or a string. C haracters * . . represent the default default action. ^x the shortcut Control key plus x key. M eta key plus x key can write "\\Mx" The dictionary data nothing (for ignoring the operation), or the parameter is (PromptState, AbstractREPL, Char function. For example, in order to implement binding up and down keys to the search history, you can .juliarc.jl :

 import Base: LineEdit, REPL

  const mykeys = {
    # Up Arrow
    "\e[A" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_prev(s, LineEdit.mode(s).hist)),
    # Down Arrow
    "\e[B" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_next(s, LineEdit.mode(s).hist))
  }

  Base.active_repl.interface = REPL.setup_interface(Base.active_repl; extra_repl_keymap = mykeys)

See base/LineEdit.jl .

Tab supplement

In Julia REPL (or REPL in help mode), you can enter the first few characters of a function or type name, and then tab to display possible options:

  julia> stri
  stride     strides     string      stringmime  strip

  julia> Stri
  StridedArray    StridedVecOrMat  String
  StridedMatrix   StridedVector

Tab keys can also replace LaTeX mathematical characters with Unicode and display possible options:

 julia> \pi[TAB]
  julia> π
  π = 3.1415926535897...

  julia> e\_1[TAB] = [1,0]
  julia> e₁ = [1,0]
  2-element Array{Int64,1}:
   1
   0

  julia> e\^1[TAB] = [1 0]
  julia> e¹ = [1 0]
  1x2 Array{Int64,2}:
   1  0

  julia> \sqrt[TAB]2     # √ is equivalent to the sqrt() function
  julia> √2
  1.4142135623730951

  julia> \hbar[TAB](h) = h / 2\pi[TAB]
  julia> ħ(h) = h / 2π
  ħ (generic function with 1 method)

  julia> \h[TAB]
  \hat              \heartsuit         \hksearow          \hookleftarrow     \hslash
  \hbar             \hermitconjmatrix  \hkswarow          \hookrightarrow    \hspace