We sure have made a lot of functions so far. There seem to be a few variables in our workspace as well. We should save them somewhere for later.

What’s a workspace?

If you have been using Dyalog, the session log is the page with all of your input and output so far. You can scroll up the session log (with a mouse or using the Page Up key) and see everything you have done so far).

A workspace is a collection of names. We can obtain some lists of names using system commands.

	  )fns    ⍝ Functions
	  )vars   ⍝ Variables (arrays)	  

These commands have the special )COMMAND syntax, and are only used when interacting with the session. They return no result and cannot be used programmatically; they cannot be used in a function.

System Functions

The session is sometimes used to refer to the interactive mode of operation (AKA calculator mode AKA immediate execution mode), in contrast to under program control, which is when something happens as the result of a line in a program/function.

For example:

      myvar ← 2×⍳3   ⍝ Declare a variable in the session
      )erase myvar   ⍝ Use a system command to erase the variable

If we try to use a system command inside a function, it won’t work.

      ]dinput        ⍝ The ]DInput user command lets us write mult-line dfns in the session
                     ⍝ Alternatively, press Shift+Enter with the cursor | on a name
      multifn←{
        ⍝ These statements are executed "under program control"
        ⎕←5+5
        var ← 2+2    ⍝ This variable only exists when this function is running
        )erase var   ⍝ This won't work
      }

System functions are functions of the form ⎕FUNCTION and do return a result. Some have shy results which can be used by subsequent functions, or printed to the session output with ⎕←.

 multifn←{
    ⍝ These statements are executed "under program control"
    ⎕←5+5
    var ← 2+2    ⍝ This variable only exists when this function is running
    ⎕EX var      ⍝ This will work
}

The Name List ⎕NL function lists names.

	  ⎕NL 2    ⍝ List variables as a text matrix
	  ⎕NL 3    ⍝ List functions
	  ⎕NL-⍳9   ⍝ List all names as a nested vector of character vectors

Namespaces

If nested arrays are arrays inside arrays; namespaces are a bit like a workspace within a workspace. They are objects which contain collections of names, and these names can be listed as before, but using the dot . syntax from object-oriented programming.

      )ns ns         ⍝ Create an empty namespace called ns
      ns.var←1 2 3   ⍝ Create a variable in ns called var
      ns.fn←{⍺,⍵}    ⍝ Create a function in ns called fn
      ⎕nl-9  	     ⍝ List the names of objects in the current namespace
┌──┐
│ns│
└──┘
      ns.⎕nl-⍳9      ⍝ List all names in ns
┌──┬───┐
│fn│var│
└──┴───┘
      )cs ns         ⍝ Change into ns
      ⎕this.⎕nl-⍳9   ⍝ The current namespace is ⎕THIS
┌──┬───┐
│fn│var│
└──┴───┘
      #.⎕nl-⍳9       ⍝ The root namespace is #
┌──┐
│ns│
└──┘

Mutable Objects

Variables are pass-by-value. This means that if one name is used to assign another name, changes to the first name are not reflected in the second name.

      var1←1 2 3
      var2←var1     ⍝ The value of var1 is assigned to var2
      var1←var1+6   ⍝ The value of var2 is changed
      ⎕←var2          ⍝ var2 retains the previous value
1 2 3

Namespaces are objects and are pass-by-reference. All names which are assigned a reference can be used to refer to the original object.

      )ns ns1
#.ns1
      ns1.name←'Bob'
      ns2←ns1
      ns2.name←'Steve'
      ⎕←ns1.name
Steve

Saving and Loading

There is one more type of system-thing to mention, although you have seen the ]box one before. Commands which begin with a right-square-bracket ] are called User Commands. These are also only used while interacting with the session, but you can customise them and create your own.

Dyalog webinar: Creating and Managing your own User Commands

The example below shows how to save and load a workspace.

	  ]cd /tmp
	  )save MyFirstWS
	  )clear
	  )load MyFirstWS

i Note: If you have not been using Dyalog, you should download and install it now. From now on we begin to deal with aspects of the session and interactions with the file system and other external systems which might not be possible to without a local installation of Dyalog.

Tasks

  1. In terms of array rank, what type of array is a namespace?
  2. What is the rank of ⎕NL x for any scalar or vector x?
  3. What is the rank of ⎕NL -x for any scalar or vector x?
  4. Save Your Work
    1. Use ]cd to change to a directory on your machine where you would like to save your work
    2. Use )wsid WSName to change the name of your active workspace
    3. Use )save to save your workspace
    4. Use )clear to clear your workspace
    5. Create a new namespace called ‘Day1’
    6. Use Day1.⎕CY'WSName' to copy the contents of your saved workspace into the Day1 namespace
    7. Now )save again

i Note: ⎕SAVE will overwrite any existing workspace file without asking first. Use )SAVE when saving workspaces.