transient assignment

Don't hold back

transient assignment

PostPosted by drab » Sat Mar 18, 2017 5:20 am

There could be a modified mode of assignment, called transient assignment, denoted by ←∘ .

A transiently assigned name is "local" to, and persists only for, the duration of the current line.

So, the environment would not get cluttered with names needed only temporarily on one line.

Here are some examples where this could be very useful ...

t←∘⎕t ⋄ ... ⋄ ⎕t-t

v∘.≤v←∘⍳3
1 1 1
0 1 1
0 0 1

It could be especially useful with system variables, as a simpler alternative to the variant operator.

⎕io←∘0 ⋄ c2←⎕av[256 256⊤⎕av⍳⎕av[8592]]

If a transiently assigned name is a system variable, or has a previous value, then it will revert to its previous value after the line is run.
drab
 
Posts: 315
Joined: Thu Oct 09, 2014 6:23 am

Re: transient assignment

PostPosted by forummaster » Thu Mar 23, 2017 7:39 pm

drab wrote:There could be a modified mode of assignment, called transient assignment, denoted by ←∘ .

A transiently assigned name is "local" to, and persists only for, the duration of the current line.

So, the environment would not get cluttered with names needed only temporarily on one line.

Here are some examples where this could be very useful ...

t←∘⎕t ⋄ ... ⋄ ⎕t-t

v∘.≤v←∘⍳3
1 1 1
0 1 1
0 0 1

It could be especially useful with system variables, as a simpler alternative to the variant operator.

⎕io←∘0 ⋄ c2←⎕av[256 256⊤⎕av⍳⎕av[8592]]

If a transiently assigned name is a system variable, or has a previous value, then it will revert to its previous value after the line is run.

You can already do this using anonymous functions because, in this context, all names assigned a value are automatically localized. To do this, enclose the line (or just a few statements) in braces and move the assignment to the "result" outside the braces along with a Dex function ().

For example, instead of
      ⎕io←∘0 ⋄ c2←⎕av[256 256⊤⎕av⍳⎕av[8592]]
use
      c2←⊢{⎕io←0 ⋄ ⎕av[256 256⊤⎕av⍳⎕av[8592]]}

The braces create a niladic anonymous function (it's a function because neither ⍺⍺ nor ⍵⍵ occur in the text and it's niladic because neither nor occur in the text).

The Dex function essentially executes the niladic function. If you don't use the Dex function, then the niladic function is assigned to the variable (in this case, c2). Then, each time you call that niladic function, it executes the code in it. This is useful when you need to repeat useful calculations, even something as simple as {⎕TS}.

In the case where you are timing an expression as in
      t←∘⎕t ⋄ ... ⋄ ⎕t-t
be sure to Sink the result of the timing expression so that the timing difference is the "result"
      {t←⎕t ⋄ ←... ⋄ ⎕t-t}
If you need to display the result you are timing, use ⎕← instead of just .
forummaster
 
Posts: 567
Joined: Wed Jan 23, 2013 1:00 pm

Re: transient assignment

PostPosted by drab » Fri Mar 24, 2017 7:39 am

Nifty.

I didn't know you could put diamonds inside of braces.
drab
 
Posts: 315
Joined: Thu Oct 09, 2014 6:23 am

Re: transient assignment

PostPosted by forummaster » Sat Mar 25, 2017 10:42 am

drab wrote:Nifty.

I didn't know you could put diamonds inside of braces.

Yes, and if you assign the anonymous function to a name, you can edit it using the editor. See Function Editing for more details.
forummaster
 
Posts: 567
Joined: Wed Jan 23, 2013 1:00 pm


Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 0 guests

cron