fig recipe: no double spaces

today we can build, like a lego model, this function:

#### license: creative commons cc0 1.0 (public domain) 
#### http://creativecommons.org/publicdomain/zero/1.0/ 
function no_double_spaces text
    sp " " 
    doublesp sp times 2
    while
        ck instr text doublesp
        iftrue ck
            fix text split fix doublesp join fix sp
            text fix
        else 
            now return text
            fig
        wend
    fig

 

then we are going to call the function like this:

# try the command
before    "     theres   a lot     of   extra    spaces  here"    print
after     no_double_spaces before                                 print

 

and that will output the following text:

     theres   a lot     of   extra    spaces  here
 theres a lot of extra spaces here

 

the function will remove any groups of spaces more than one space long, replacing them with a single space.

 

now for our 11 ingredients:

1. print – prints the main variable (the main variable is named at the beginning of most lines)

2. function – lets you create your own fig commands

3. times n – lets you multiply a number or string (or array) “times n” where n is the multiplier

4. while and wend – these mark the start and end of a loop block

5. instr t f – changes main variable to the index of string or array t, where f is found

6, 7. iftrue t and else and fig – do the part after iftrue, if t is not zero; otherwise, do the part after else

8. split t w – change the main variable to an array from splitting string t by delimiter w

9. join t w – the opposite of split: join array t with the string w put between each joining

10. return v – leave the function, then set the main variable that used it to the contents of v

11. fig – marks the end of any block command (could be used instead of wend, too.)

 

step 1: define our function. the name will be “no_double_spaces” and the single input will be “text”. function definitions are blocks, so finish with the “fig” command:

function no_double_spaces text
    PLACEHOLDER
    fig

 

step 2: create variables “sp” and “doublesp”. set the first one to a single space with quotes. then on the next line, set doublesp to the contents of sp (make a copy of sp):

function no_double_spaces text
    sp " "
    doublesp sp PLACEHOLDER
    fig

 

step 3: make doublesp double, by multiplying it times 2:

function no_double_spaces text
    sp " "
    doublesp sp times 2
    PLACEHOLDER
    fig

 

step 4: (note this indentation is optional, you can make it all left-justified if you like.) add a while loop. the loop will just keep going until you use the break command (we are going to be clever and use return, which leaves the function– instead of break, which exits the loop.)

function no_double_spaces text
    sp " "
    doublesp sp times 2
    while
       PLACEHOLDER
       wend
    fig

 

step 5: set “ck” variable and add instr, searching the text variable for the string doublesp

function no_double_spaces text
    sp " "
    doublesp sp times 2
    while
       ck instr text doublesp # ck will be non-zero if double space is found
       PLACEHOLDER
       wend
    fig

 

step 6: add the iftrue / else / fig block after the line for ck:

function no_double_spaces text
    sp " "
    doublesp sp times 2
    while
       ck instr text doublesp

       iftrue ck
           PLACEHOLDER-TRUE
       else 
           PLACEHOLDER-FALSE
           fig

       wend
    fig

 

step 7: if ck is not true (no double spaces found) return text and leave the function:

function no_double_spaces text
    sp " "
    doublesp sp times 2
    while
       ck instr text doublesp
       iftrue ck
           PLACEHOLDER-TRUE
       else 
           now return text
           fig
       wend
    fig

 

step 8: kind of a tricky one… if ck is true (double spaces found) split variable “fix” by a doublespace, creating an array: (then on the line after that, set “text” to the fixed version.)

function no_double_spaces text
    sp " "
    doublesp sp times 2
    while
       ck instr text doublesp
       iftrue ck
           fix text    split fix doublesp PLACEHOLDER
           text fix # copy the fixed version to text
       else 
           now return text
           fig
       wend
    fig

 

step 9: join the array that was split by doublespaces, using sp: (this replaces each double space with a single space)

function no_double_spaces text
    sp " "
    doublesp sp times 2
    while
       ck instr text doublesp
       iftrue ck
           fix text    split fix doublesp    join fix sp
           text fix
       else 
           now return text
           fig
       wend
    fig

 

step 10: call your new fig command, and voila!

function no_double_spaces text
    sp " "
    doublesp sp times 2
    while
       ck instr text doublesp
       iftrue ck
           fix text    split fix doublesp    join fix sp
           text fix
       else 
           now return text
           fig
       wend
    fig

# try the command
before    "     theres   a lot     of   extra    spaces  here"    print
after     no_double_spaces before                                 print

 

bonus step 1:

no_double_spaces changes all instances of doublesp to sp; we can “comment out” the definitions (first two lines inside the function) of those two variables like this:

function no_double_spaces text
    #### sp " "              #### well, these lines
    #### doublesp sp times 2 #### wont do much now
    while
       ck instr text doublesp
       iftrue ck
           fix text    split fix doublesp    join fix sp
           text fix
       else 
           now return text
           fig
       wend
    fig

 

bonus step 2:

now change the name of the function to string_replace_all:

function string_replace_all text PLACEHOLDER
    while
       ck instr text doublesp
       iftrue ck
           fix text    split fix doublesp    join fix sp
           text fix
       else 
           now return text
           fig
       wend
    fig

 

bonus step 3:

since it still wants to change doublesp to sp, add those as function parameters:

function string_replace_all text doublesp sp
    while
       ck instr text doublesp
       iftrue ck
           fix text    split fix doublesp    join fix sp
           text fix
       else 
           now return text
           fig
       wend
    fig

 

bonus step 4:

call your new string replace function:

#### license: creative commons cc0 1.0 (public domain) 
#### http://creativecommons.org/publicdomain/zero/1.0/ 
function string_replace_all text doublesp sp
    while
       ck instr text doublesp
       iftrue ck
           fix text    split fix doublesp    join fix sp
           text fix
       else 
           now return text
           fig
       wend
    fig

# try the command
before    "would you like some pancakes? because pancakes are the best!"    print
after     string_replace_all before "pancakes" "waffles"                    print

 

the program outputs:

would you like some pancakes? because pancakes are the best!
would you like some waffles? because waffles are the best!

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s