forum-msg-id-135537-edit

Original date:2021-01-24 10:32:37 Edited by: _tom Subject: Phix wiki 3

When programming in Phix you learn to recognize similarities between data values and structures.

Complete Sequence Actions

Symmetrical indexing provides a complete set of operations for accessing|mutating items and slices in a sequence or string.

You can get an item or a slice from a sequence:

sequence s = { 1,2,3,4} 
          -- one item 
? s[1]    // 1 
 
          -- slice with one item 
? s[1..1] // {1} 
 
           -- slice with three items 
? s[1..3]  //  {1,2,3} 

Slicing is a left to right action:

sequence s = { 1,2,3,4} 
? s[1..3 ]              // {1,2,3} 
? s[1..-2]              // {1,2,4} 
 
//but 
? s[3..1]   --> error 
            -- slice length is negative [3..1] 

You can increase the length of a sequence by one item by appending, or the length n of a new sequence:

Appending increase the length of a sequence by one item. Concatenating increases the length of a sequence by the length n:

sequence s 
 
s = "jello" 
s = append(s, "jiggles" ) 
? s                      -- appending 
                         -- longer by one 
                         // { "jello", "jiggles" } 
 
s = "hello" 
s = s & ", world" 
? s                      -- concatenating 
                         -- longer by n = length(", world") 
                         // "hello, world" 

Prepending and concatenating to the head of a sequence works the same way.

A {} is an empty sequence, which is identical to "" an empty string. You can insert an empty sequence as an item, or use it to delete an item or slice:

sequence str 
 
str = "hello" 
str[1] = "" 
? str               -- mutating an item 
                    // { {}, 'e','l','l','o' } 
 
str = "hello" 
str[1..1] = "" 
? str                -- deleting an item 
                     // "ello" 

Slicing also works for concatenating slices:

string str 
 
str = "hello" 
str[1..0] = "hi, "    -- concatenate before first item 
? str                 //  "hi, hello" 
 
str = "hello"         -- concatenate within string 
str[4..3] = "***"     // "hel***lo" 
? str 
 
str = "hello" 
str[ $+1..$] = " be well"      -- concatenate after last item 
? str                          // "hello be well" 

Slicing works for inserting items:

sequence x = "hello" 
x[4..3] = { "###" }     -- creating a nested sequence 
? x                     //  {'h','e','l',"###",'l','o'} 
 
? flatten(x)            -- producing a string from nested 
                        //"hel###lo" 

Without symmetrical indexing values used for indexing and slicing differ, which is really hard to explain without specialized diagrams.

All possible changes to a string or sequence are handled simply with indexes and slices.

Debugging features

Source code safety is ensured by:

  • data type checking
  • enforcing subscript bounds
  • variables must be declared and explicitly assigned values
  • argument are passing by-value (but with the efficiency of copy-on-write)
  • no pointers

Additional features

The Phix developer is provided with:

  • human readable error messages
  • automatic memory management
  • garbage collection
  • interpretation and compilation of source code
  • gui libraries based on IUP
  • a standard library that includes features like: ipc, json, curl, zip, gmp, regular expressions, sockets, and unit testing
  • debugger and execution profiling
  • execution tracing

Rosetta Code

Phix does not directly support: lambda expressions, closures, nested functions, currying, partial function application, function composition, function prototyping, monads, generators, anonymous recursion, the Y combinator, aspect oriented programming, interfaces, delegates, first class environments, implicit type conversion (of the destructive kind), interactive programming, inverted syntax, list comprehensions, meta-programming, operator-function-builtin overloading, pointers (other than to raw allocated memory), topic variables, enforced singletons, safe mode, s-expressions, or formal proof construction.

The Rosetta website has over one thousand programming challenges, and states "...we do not (and cannot) have solutions to every task in every language."

Phix has over one thousand completed Rosetta examples. A compact language like Phix is not limited by its simple nature.

http://www.rosettacode.org/wiki/Rosetta_Code

Execution modes

Interpreter Compiler

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu