-
Notifications
You must be signed in to change notification settings - Fork 4
Flow of control
Pipefish commands have flow of control in a way that Pipefish functions don't and can't. This is demonstrated in the file examples/flow.pf.
Let's look at the commands in it one at a time.
Unlike a Pipefish function, which reaches a return value and returns, Pipefish commands do things, and so can do them one after another.
seq :
post "It's just one thing ..." to Output()
post "... after another." to Output()
And so this means that in Imperative Pipefish one conditional can follow on another:
check :
5 % 2 == 0 :
post "5 is even" to Output()
else :
post "5 is odd" to Output()
6 % 2 == 0 :
post "6 is even" to Output()
else :
post "6 is odd" to Output()
It is recommended that every conditional block should end with an else clause. First, for consistency and second because when one conditional block follows on another like this, Pipefish identifies the end of the first conditional block with the else clause.
If you want one branch of a conditional to do nothing at all, then you should tell it to return ok: e.g. the following rewrite of the check command above will do nothing unless 5 becomes even or 6 becomes odd:
check :
5 % 2 == 0 :
post "5 is even" to Output()
else :
OK
6 % 2 == 0 :
OK
else :
post "6 is odd" to Output()
In Imperative Pipefish a for loop can contain statements rather than expressions.
cmd
hello(n) :
for i = 0; i < n; i + 1 :
post "Hello!"
Note that this particular for loop neither has nor needs bound variables. As it is imperative, it will either return OK, which you needn't concern yourself with; or an error, which you can deal with as explained in the next page.
🧿 Pipefish is distributed under the MIT license. Please steal my code and ideas.
- Getting started
- Language basics
- The type system and built-in functions
- Functional Pipefish
- Encapsulation
- Imperative Pipefish
-
Imports and libraries
- The crypto/aes library
- The crypto/bcrypt library
- The crypto/rand library
- The crypto/rsa library
- The crypto/sha_256 library
- The crypto/sha_512 library
- The database/sql library
- The encoding/base_32 library
- The encoding/base_64 library
- The encoding/csv library
- The encoding/json library
- The files library
- The fmt library
- The html library
- The image library
- The image/bmp library
- The image/color library
- The image/jpeg library
- The image/png library
- The lists library
- The markdown library
- The math library
- The math/big library
- The math/cmplx library
- The math/rand library
- The net/http library
- The net/mail library
- The net/smtp library
- The net/url library
- The os/exec library
- The path library
- The path/filepath library
- The reflect library
- The regexp library
- The strconv library
- The strings library
- The terminal library
- The time library
- The unicode library
- Advanced Pipefish
- Developing in Pipefish
- Deployment
- Appendices