bug-apl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-apl] Two things I have to share


From: Juergen Sauermann
Subject: Re: [Bug-apl] Two things I have to share
Date: Fri, 26 Aug 2016 16:06:51 +0200
User-agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.4.0

Hi Xtian,

nice functions.

Regarding ⎕PW and other system variables:
When you localize a system variable in GNU APL then the localized value of the variable is
set to the its default value (rather than giving ⎕PW ERROR as in APL2 when the variable is
localized and not assigned a new value:

            )CLEAR
CLEAR WS
 
∇FOO;⎕PW
 'local ⎕PW after localizing it' ⎕PW
 ⎕PW←200                       
 'local ⎕PW after assigning it' ⎕PW

    
         'initial ⎕PW' ⎕PW
 initial ⎕PW 80
         ⎕PW←555
         '⎕PW before FOO' ⎕PW
 ⎕PW before FOO 555
         FOO
 local ⎕PW after localizing it 80
 local ⎕PW after assigning it 200
         '⎕PW after FOO' ⎕PW
 ⎕PW after FOO 555

/// Jürgen


On 08/25/2016 03:28 AM, Christian Robert wrote:
In my original "df" function I had "⎕pw" localized, but even after setting it to 10000 inside the function
it looks like the default (80) was still was in effect while executing.

Xtian.

On 2016-08-24 20:52, Christian Robert wrote:
First my "df" function

∇z←df;names;func;lambda;t
⎕pw←10000
names←((⊂[⎕io+1]⎕nl 3 4)~¨' ')~ ⊂"df"
lambda←({'λ'=↑⎕cr ⍵}¨names)/names
→(2> ⍴lambda)/nosrtl
lambda←{⍵[⎕av⍋⊃⍵]} lambda
nosrtl: func←names~lambda
→(2>⍴func)/nosrtf
func←{⍵[⎕av⍋⊃⍵]} func
nosrtf: func←{⍵ (22 ⎕cr ⎕cr ⍵)} ¨ func
lambda←{⍵,'←{',({⌽ {(+/^\' '=⍵)↓⍵} ⌽ ⍵} 2↓((⎕cr ⍵)[1+⎕io;])),({{⌽ {(+/^\' '=⍵)↓⍵} ⌽ ⍵} {(∨\t=';')/t←(⎕cr ⍵)[⎕io;]} ⍵} ⍵),'}'} ¨ lambda
z←,[⍳0],func,(⊂" "),lambda




I made a workspace named "df" with the only function "df" inside, so I
can do:

        )copy df df

from whithin any workspaces

example:


      )wsid
IS Util
      )copy df df
SAVED 2016-08-24 20:28:15 (GMT-4)
      df
  Execute  ╔═════════════════════════════════════════════════════════════════════════════════════════════════════╗
           ║z←Execute cmd;fh                                                                                     ║
           ║fh ← ⎕FIO[24] cmd                   ⍝ popen(cmd,"r")                                                 ║
           ║z←⊂ 19 ⎕cr ⎕ucs ¯1↓⎕fio [8] fh      ⍝ Read first line, drop final LF, convert in byte, convert in APL║
           ║Loop: →(0≠⎕FIO[10] fh)/Fini          ⍝ Check for end-of-file                                         ║
           ║z←z,⊂ 19 ⎕cr ⎕ucs ¯1↓⎕fio [8] fh    ⍝ Add an other line to the array                                 ║
           ║→Loop                               ⍝ Loop until no other lines of output                            ║
           ║Fini: ⊣ ⎕FIO[25] fh                  ⍝ Close the PIPE                                                ║
           ║z←⊃¯1↓z                              ⍝ Disclose the result.                                          ║
           ╚═════════════════════════════════════════════════════════════════════════════════════════════════════╝
  Fntb  ╔═══════════════════════════════════════════════════════════╗
        ║Z←Fntb f_name;⎕io;RTB                                      ║
        ║⍝                                                          ║
        ║⍝ Remove all trailing blanks from a ⎕cr 'f_name',          ║
        ║⍝ so it will be easy to cut & paste without trailing blanks║
        ║⍝ intended for mail and debug purpose.                     ║
        ║⍝                                                          ║
        ║⎕io←1                                                      ║
        ║RTB←{(∼⌽∧\⌽⍵=' ')/⍵} ⍝ Remove trailing blanks from '⍵'     ║
        ║Z←,[⍳0] RTB¨⊂[2]Z←⎕cr f_name                               ║
        ╚═══════════════════════════════════════════════════════════╝
  Pull  ╔══════════════════════════════════════════════════════════════════════════════════════════════════════════╗
        ║z←Pull file;fh                                                                                            ║
        ║⎕fx 19 ⎕cr ¯1 ↓ ⎕fio[26] 18 ⎕cr file ⍝ Read a text file and ⎕FX it, utf-8 aware for file name and contents║
        ╚══════════════════════════════════════════════════════════════════════════════════════════════════════════╝
  Push  ╔════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
        ║z←Push func;f2c;fh                                                                                          ║
        ║f2c←{(⎕ucs 10),⍨¨{(∼⌽∧\⌽⍵=' ')/⍵}¨{⊂[1+⎕io]⎕cr ⍵} ⍵}    ⍝ Internal macro to transform a function to text    ║
        ║fh←'w' ⎕fio[3] 18 ⎕cr {(∼⌽∧\⌽⍵=' ')/⍵}func              ⍝ Open a file, no trailing blanks, honor utf-8 names║
        ║⊣{⍵ ⎕fio[43] fh} ¨ f2c func                             ⍝ Write each line, no trailing blanks, LF terminated║
        ║⊣⎕fio[4] fh                                             ⍝ Close the file                                    ║
        ║z←func                                                  ⍝ Just return the function name                     ║
        ╚════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
  PushAll  ╔═══════════════════════╗
           ║z←PushAll              ║
           ║z←Push ¨ ⊂[⎕io+1] ⎕nl 3║
           ╚═══════════════════════╝
  depth  ╔═══════════════════════════════════╗
         ║Z←(F depth N) A                    ║
         ║⍝ Apply monadic function at Depth N║
         ║→(N<≡A)/Recur                      ║
         ║Z←F a ⋄ →0                         ║
         ║Recur: Z←(F depth N)¨A             ║
         ╚═══════════════════════════════════╝
  f2def  ╔══════════════════════════════════════════════════════════════╗
         ║f2def name;⎕io;ltrim;rtrim;z;⎕pw                              ║
         ║⎕io←1 ⋄ ⎕pw←1000 ⋄ ltrim←{(+/^\' '=⍵)↓⍵} ⋄ rtrim←{⌽ ltrim ⌽ ⍵}║
         ║z←⊂[2] ⎕cr name                                               ║
         ║z[1]←⊂'      ∇',⊃z[1]                                         ║
         ║z←(⊂'      )erase ',name),z                                   ║
         ║z←(⊂'      )sic'),z                                           ║
         ║z←(⊂'      '),z                                               ║
         ║z←z,⊂"∇"                                                      ║
         ║(⎕ucs 10),¨ rtrim ¨ z                                         ║
         ╚══════════════════════════════════════════════════════════════╝
  last  ╔══════════════════════════════════╗
        ║Z←L (F last) R;V                  ║
        ║⍝ Apply F along the last dimention║
        ║⍝ Build enclose last FN (V)       ║
        ║Z←'Z←V A' 'Z←⊂[(0≠⍴⍴A)/⌈/⍴⍴A]A'   ║
        ║⎕ES (0=↑0⍴⎕FX Z)/1 2 ⍝ Fix OK?    ║
        ║⍝ Z←F¨(V R) or Z←(V L)F¨(V R)     ║
        ║Z←'Z←⊃',(0≠⎕NC 'L')/'(V L)'       ║
        ║Z←Z,'F¨(V R)'                     ║
        ║⍝ (Do-or-Die idiom)               ║
        ║'⎕ES ⎕ET' ⎕EA Z                   ║
        ╚══════════════════════════════════╝
  mad  ╔════════════════════════════════╗
       ║Z←L mad R                       ║
       ║⍝ Multiset Asymmetric Difference║
       ║Z←(~L pde R)/L                  ║
       ╚════════════════════════════════╝
  pde  ╔═════════════════════════════╗
       ║Z←L pde R                    ║
       ║⍝ Progressive Dyadic Epsilon ║
       ║Z←((⍴L)⍴⍋⍋L⍳L,R)∊(⍴R)⍴⍋⍋L⍳R,L║
       ╚═════════════════════════════╝
  pdi  ╔═════════════════════════════╗
       ║Z←L pdi R                    ║
       ║⍝ Progressive Dyadic Iota    ║
       ║Z←((⍴L)⍴⍋⍋L⍳L,R)⍳(⍴R)⍴⍋⍋L⍳R,L║
       ╚═════════════════════════════╝
  readfile_fast  ╔═════════════════════════════════════════════════════════════════════════════════════╗
                 ║z←readfile_fast name;fd;lines;⎕io                                                    ║
                 ║⎕io←1 ⍝ Bring a file into a vector of strings, utf8 aware for both name and contents.║
                 ║→(0≠"r" ⎕fio[31] 18 ⎕cr name)/Error           ⍝ Can not read file ? → Error          ║
                 ║z←⎕fio[26] 18 ⎕cr name                        ⍝ First pass, read the whole file      ║
                 ║lines←⍳+/((↑"\n")=z)                          ⍝ Compute the iota for each line       ║
                 ║z←(⍴lines)⍴⍬                                  ⍝ Preallocate "z" to the right size    ║
                 ║fd←⎕fio[3] 18 ⎕cr name                        ⍝ Open the file                        ║
                 ║⊣ {⊣z[⍵]←⊂19 ⎕cr ⎕ucs ¯1↓⎕fio[8] fd} ⍤0 lines ⍝ Put each line in the preallocated "z"║
                 ║⊣ ⎕fio[4] fd ⋄ →0                             ⍝ Close the file and return            ║
                 ║Error: ⎕ES ∊'Error on file "',name,'": ',⎕fio[2] | ⎕fio[1] ''                        ║
                 ╚═════════════════════════════════════════════════════════════════════════════════════╝
  replace  ╔═════════════════════════════════════╗
           ║z←s replace p;fr;to;P;i;⎕io          ║
           ║⍝ -------------                      ║
           ║→(2≤≡s)/ForEach                      ║
           ║→(2≤⍴⍴s)/Matrix                      ║
           ║→Vector                              ║
           ║⍝ -------------                      ║
           ║ForEach: z←{⍵ replace p}¨s ◊ →0      ║
           ║Matrix: z←⊃ {⍵ replace p}¨ ⊂[2]s ◊ →0║
           ║⍝ -------------                      ║
           ║Vector: p←,¨p ◊ z←s←,s ⋄ ⎕io←1       ║
           ║Loop:→((⍴p)<2)/0                     ║
           ║(fr to)←2↑p ◊ p←2↓p                  ║
           ║P←¯1↓∧/[1](¯1+⍳⍴fr)⌽fr∘.=z,⎕ucs 1    ║
           ║i←0                                  ║
           ║Sanitize: i←i+1                      ║
           ║→(i>(⍴P)-⍴fr)/Substitute             ║
           ║→(0=P[i])/Sanitize                   ║
           ║((⊂ i+¯1+⍳⍴fr)⌷P)←(⍴fr)↑1            ║
           ║→Sanitize                            ║
           ║Substitute: P←⌽(P/⍳⍴P)               ║
           ║⊣ {z←((¯1+⍵)↑z),to,(¯1+⍵+⍴fr)↓z}¨P   ║
           ║→Loop                                ║
           ╚═════════════════════════════════════╝
  time  ╔════════════════════════╗
        ║time ∆cmd;∆start;∆stop  ║
        ║∆start←⎕fio ¯1          ║
        ║⍎ ∆cmd                  ║
        ║∆stop←⎕fio ¯1           ║
        ║⎕←(∆stop-∆start)÷⎕fio ¯2║
        ╚════════════════════════╝
  time2  ╔═══════════════════════╗
         ║time2 ∆cmd;∆start;∆stop║
         ║∆start←↑1↓⎕ai          ║
         ║⍎ ∆cmd                 ║
         ║∆stop←↑1↓⎕ai           ║
         ║⎕←1000÷⍨∆stop-∆start   ║
         ╚═══════════════════════╝

 D←{24 ⎕cr ⍵}
 QuickInput←{⍎¨⍵⍴'⍞'}
 Quote←{'''',((1+''''=⍵)/⍵),''''}
 cut_to_window←{((⍴,⍵)⌊(¯2+⎕fio ¯8))↑,⍵}
 ltrim←{(+/^\' '=⍵)↓⍵}
 no_multi_blanks←{(~⍵∧1⌽⍵←' '=⍵)/⍵}
 rtrim←{⌽ {(+/^\' '=⍵)↓⍵} ⌽ ⍵}
 sortvs←{⍵[⎕av⍋⊃⍵]}
 split←{⊂[(0≠⍴⍴⍵)/⌈/⍳⍴⍴⍵]⍵}
 to_utf8←{⍎↑(2=≡⍵)↓⌽ "19 ⎕cr ¨⍵" "19 ⎕cr ⍵"}
 trim←{⌽ ltrim ⌽ ltrim ⍵}
 typeof←{↑0⍴⊂ ⍵}
 vertical←{,[⍳0]⍵}



So, it present your functions (alpha sorted) and then your Lambda (alpha sorted and local vars aware).

the useful things are that it give a very quick look of the whole workspace
and present the lamda's in a way it is easy to cut, modify and paste them.

-------------

The second share is the function "readfile_fast". It is, to my knowledge, the fastest
way (in gnuapl) to load a large file (like /var/log/messages). I successfully loaded a 200,000+ plus lines
file into my workspace within a few seconds. (Order N), (need memory and permission, avoid ")save", may take a while)

here it is:

∇z←readfile_fast name;fd;lines;⎕io
⎕io←1 ⍝ Bring a file into a vector of strings, utf8 aware for both name and contents.
→(0≠"r" ⎕fio[31] 18 ⎕cr name)/Error           ⍝ Can not read file ? → Error
z←⎕fio[26] 18 ⎕cr name                        ⍝ First pass, read the whole file
lines←⍳+/((↑"\n")=z)                          ⍝ Compute the iota for each line
z←(⍴lines)⍴⍬                                  ⍝ Preallocate "z" to the right size
fd←⎕fio[3] 18 ⎕cr name                        ⍝ Open the file
⊣ {⊣z[⍵]←⊂19 ⎕cr ⎕ucs ¯1↓⎕fio[8] fd} ⍤0 lines ⍝ Put each line in the preallocated "z"
⊣ ⎕fio[4] fd ⋄ →0                             ⍝ Close the file and return
Error: ⎕ES ∊'Error on file "',name,'": ',⎕fio[2] | ⎕fio[1] ''




enjoy both shares,

Xtian.

ps: both functions may/have bugs, no doubts.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]