Welcome
Welcome to MicroAPL's Support Forum for APLX.


You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, join our community today!

Sorting the header of a method in a class

Topics related to the APLX interpreter itself, not to a specific platform

Sorting the header of a method in a class

Postby PGilbert on Thu Jun 25, 2009 2:55 am

I have a function called 'SortHeader' that will sort the header of function using []CR first to get the canonical representation of the function and than []FX to fix the modified vector. If I want to do the same thing on the methods of a class how would you approach this problem since I can extract the method one by one of a class but I don't know how to 'fix' them back one by one in the class.

Thanks in advance,

Pierre Gilbert
PGilbert
 
Posts: 59
Joined: Thu Jul 10, 2008 8:34 pm
Location: Montreal, Quebec, Canada

Re: Sorting the header of a method in a class

Postby MicroAPL on Thu Jun 25, 2009 11:23 am

As you point out, you can extract the text representation of a method using:

⎕CR 'MyClass.SomeMethod'


but you can't currently use ⎕FX to put it back. We ought to figure out a syntax for allowing this.

In the meantime, there are a couple of workarounds:

1) Your can use ⎕CR to get the text form of the whole class, do the sorting in the various methods, and re-fix the whole class using ⎕FX.

2) Another method is to fix the modified method temporarily as a global function, and then use ⎕IC to poke it back into the class. Obviously that only works if there isn't a name clash with an existing global function, but you could do the operation in a separate workspace if this is a problem.
MicroAPL
Site Admin
 
Posts: 166
Joined: Tue Jul 08, 2008 5:25 pm

Re: Sorting the header of a method in a class

Postby PGilbert on Thu Jun 25, 2009 2:30 pm

Thanks for your answer, I think the second method will be OK for me. For your information, after I have finished to write an APL function, I do what I call the 'clean-up'. The first step is to do the indentation of the control structure, I used to do that with a program, but thanks to Ctrl+I this is done by the editor. The second step is to sort the header and look for unused names (name in the header but not in the function). The third step is to make the code more readable by adding some spaces through the APL code (thanks to V5 that is allowing this option now), this is to fight the critic that APL code is difficult to read when all compacted (I agree with that critic).

Thanks for your Ctrl+I that is very helpfull for fixing the indentation, if one day you want to do a Ctrl+? to sort the header of the function or method to ease the 'clean-up' process, this will be welcome by me.

Regards,

Pierre Gilbert
PGilbert
 
Posts: 59
Joined: Thu Jul 10, 2008 8:34 pm
Location: Montreal, Quebec, Canada

Re: Sorting the header of a method in a class

Postby PGilbert on Fri Aug 21, 2009 2:52 am

Here is a copy of the function 'SortHeader' that will sort the header of a function or the methods of a class:

r„vr SortHeader name;header;i;inter;methods;vars
© Sort the Header of a Function or Class
©
© name = Function or Class Name
© r and vr are used when called Recursively

:If (›name) ¹ ŒDBR¨›[2] ŒNL 9 © Class Name ?
© name is a Class
methods „ –name,'.ŒNL 3' © Public Methods of the Class

:For i :In ¼1½½methods
vr „ –"2 ŒCR '",name,'.',methods[i;],"'" © ŒCR of the method
vr „ vr SortHeader '' © Call SortHeader Recursively to Sort the Header in vr

:If (ŒFX vr) » (methods[i;]~' ') © Fix the Method
© Failure
'ERROR: Not Able to Fix: ',methods[i;]
:Continue
:Else
© Success, Do Nothing
:EndIf

:If 1 = name ŒIC methods[i;] © Insert back the Method into the Class
© Success
name,'.',(methods[i;]~' '),' Sorted'
:Else
© Failure
'Not Able to Fix ',(methods[i;]~' '),' into ',name
:EndIf
:EndFor

:ElseIf (›name) ¹ ŒDBR¨›[2] ŒNL 3 © Function Name ?
© name is a Function
vr„2 ŒCR name
vr„vr SortHeader ''

:If (inter„ŒFX vr) » (name~' ')
© Failure
'Not Able to Fix ',name,' at line: ',•inter
:Else
© Success
'Function Sorted: ',name
:EndIf

:ElseIf 0 ¬ ŒNC 'vr' © vr Exists ?
© SORT vr (must be a nested vector from 2 ŒCR 'Function_Name')

© Removes the extra Blanks at the End of each line of the Function
vr„(-¯1+(²¨' '=¨vr)¼¨0)‡¨vr

© Extract the Header of the Function
header „ 1œvr

© Check if there is local variables to sort in header
:If ~';' ¹ header
© no ';' in header
r „ vr
:Else
© There is local variables in Header
© Split the Header
vars „ (header¼';')‡header

© Sort the variables
vars „ ';' ŒBOX (vars~' ')
vars „ vars[ŒAV“vars;]
vars „ (,';',vars)~' '

© Reassemble the header
header „ ((¯1+header¼';')†header),vars

© Insert the Sorted Header in ŒCR of function
(1œvr)„header

© Return the Result
r „ vr
:End

:Else
'ERROR: Argument Must Be the Name of a Function or a Class',ŒR
:End


Regards,

Pierre Gilbert
PGilbert
 
Posts: 59
Joined: Thu Jul 10, 2008 8:34 pm
Location: Montreal, Quebec, Canada


Return to APLX Interpreter