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!

 

 

apps vs. code

an app is made of code. you can code an app that makes:

  • apps from code: (text -> gui)
  • code from apps (apps that manage or produce code as text)
  • apps from apps (apps that manage or produce code graphically)
  • code from code (automatic translation from one kind of text to another. this is what “compilers” do.)

 

creating an app, whether you “write code” or not, is a useful and educational experience. it can teach you important things about apps, and code.

in my opinion, people should definitely learn to code if possible; even if they never design an app. designing an app is a great idea, too– but there is no substitute for the knowledge that just letters and numbers and punctuation (really just numbers) can make absolutely anything “digital” (just look at that word) happen.

computers translate things into numbers, and numbers into EVERYTHING that computers do. they are magic calculators that change the world. they can make music, video, images, organize, communicate (and publish) globally, teach, inspire, (and also do math!) an app is a product: code is a domain. accept no substitutes (but appreciate the applications, and the simplicities, of both.)

 

 

fig 2.9 with puppy: “tahr”

its years now since i used puppy linux, and the way its developed has somewhat changed (…not too much.)

puppy linux “tahr” includes python 2 out of the box, so i thought id run fig in it. heres what you do:

you may want a script to run, so here is one i posted for a demo:

#text mode: 
#### license: creative commons cc0 1.0 (public domain) 
#### http://creativecommons.org/publicdomain/zero/1.0/ 
now display 
function circle x y r c 
    cr r  int  plus 1 
    p -3.14159 
    try 
        rcpr 1 divby r 
    except 
        rcpr .005 
    resume 
    while 
        now p plus rcpr swap now p 
        ifmore p 3.14159 
            break 
            fig 
        cosx p  cos  times cr  plus x  int 
        siny p  sin  times cr  plus y  int 
        now     pset cosx siny c 
        wend 
    fig 
x 
y 
r 
c 
xf = 1  
yf = 1  
rf = .5  
cf = 1 
try 
    while 
        now circle (x, y), r, c : sleep .01 
python 
    x = x + xf ; y = y + yf ; c = c + cf ; r = r + rf 
    if x < 1:    xf = 1  
    if x > 638/2:  xf = -1 
    if y < 1:    yf = 1  
    if y > 477/2:  yf = -1 
    if c < 1:    cf = 1  
    if c > 14:   cf = -1 
    if r < 1:    rf = .5  
    if r > 200/4:  rf = -.5 
    fig 
        wend 
except 
    now colortext 7 : highlight 0 : cls : system : end 
    resume 

 

  • copy the file to
    circles.fig
  • run
    fig circles.fig
  • fig will translate to
    circles.fig.py
  • run
    ./circles.fig.py

theres a bug in this program, if you have pygame installed. either comment out #

now display

… or add

display

to the right of this line:

now circle (x, y), r, c : sleep .01

the line “now display” will turn off automatic pixel updating, but the pixels wont be told to update. this is a non-issue in text mode, but in pygame it may not work properly if left un-altered.

 

why i dont recommend coding with chrome

actually, i will show you this video if you havent found it already: https://youtu.be/v9Xse_PDmH0

i think “coding with chrome” has some great ideas. im going to talk about where i think its major limitations are.

first of all, there are lots of projects like this. i think (based on the video) theyve done a remarkable job packaging some of them up, but i dont think theyre worth relying on. so why not?

first of all, i do not think chrome is an ethical piece of software. you have your criteria, i have mine. chrome doesnt even let you look at the code it uses, but lots of browsers do.

the best environment for learning code in my opinion, is one that lets you see the code it is itself written in. you may never look at it, but just knowing its there means that someone can take over and improve/maintain/update that environment. and that has a major practical implication:

no one is giving any evidence that “coding with chrome” isnt just another project that google will drop, and google has dropped many. while old versions of google phone flop and flounder without proper support, google keeps churning out projects. and with a reputation for “open source,” google keeps giving you more and more that is “hands off.”

someone can build 90% or more of what “coding with chrome” has right now, and keep it going past the time that google abandons it. when i was learning to code, there were not many environments that would last (in a practical, maintainable way) beyond the direct involvement of the company. today, there are too many truly free/libre/open projects and platforms to waste time with this sort-of-but-not-really-yours stuff that google (or anyone) offers.

but i also wont be surprised when google does drop coding with chrome. and then what? look for the next thing? well heck, i can do that right now. good luck “coding with chrome,” we couldve done cool stuff together.

but im not going to spend time on a platform that makes all the rules for me. one of the best things about learning to code and free (as in freedom) software is: its not up to a giant corporation how long you spend making full use of the tool that is offered. because when a company like google abandons (or through mergers or companies closing loses) the opportunity to maintain an interesting project like “coding with chrome,” new stewards can step in to maintain it.

and since i think that is ideal, does “coding with chrome” offer anything not already available that is worth losing that feature?

not at all! there are too many alternatives to worry what proprietary, feature-softlocked toys google makes without offering the opportunity to someone who can maintain them on their behalf.

its still worth looking at, because sometimes a tool is influential enough to take note of how it is put together. also worth noting is how much google borrowed to make “coding with chrome” possible:

…like the html rendering engine originally from kde and then apple. (the javascript engine from google is drastically improved.) or the concepts from drag-and-drop coding. or the super-friendly curriculum from sites like code.org.

but nearly as much as this, i simply dont like google. and i dont like chrome, which theyve tied in. “coding with chrome” ought to sweeten the deal a little; but i look at it as browser advertising, and chromebook promotion. i think google has taught people enough bad lessons about computing already, to make me think twice before entrusting them with any sort of curriculum.

my opinion, obviously.