The Avail Programming Language
Mobile Users: Click here to view our text rendering warning.

Expression Cookbook

Here can be found handy expressions that illustrate a wide variety of small programming tasks.

Each recipe begins with a question that you might ask, like "How do I create a literal array?" Very likely, you are coming from some other programming language, and framing your question in its terms. We have taken care to include alien terminology in the introduction of each section. This means that a search of the page from your browser will be likely to turn up the right information. This is also a good way to learn Avail's native terminology.

Following each question is a list of answers, each of which is provided as a sample expression. Most of these expressions are prefixed with an exclamation point ! (U+0021). You can use the Availuator to run any of these expressions from the workbench. Simply copy the entire expression, including the leading exclamation point, paste it into the workbench's input field, and press Enter.[1] If you want to use one of these expressions in a module, then do not include the leading exclamation point in the copy. This character is part of the send of "`!_", the Availuator's primary expression evaluator.

After the sample expressions are hyperlinks into the Stacks. Each hyperlink targets a method used in a sample expression. Only methods of primary relevance to the topic are targeted.

Note that the results of the expressions are deliberately not included on this page. Consider this strong encouragement to play with the recipes in the Availuator! :-)


Logic Math Conditionals Collections Maps
Sets Tuples Loops Strings Exceptions

Logic

What are the basic logical operators?

Keywords: not, negation, conjunction, and, disjunction, or, XOR, NAND, Sheffer stroke, NOR, converse.

"_∧_" "_∨_" "_`↑_" "_↓_"
"¬_" "_⊕_" "_↚_" "_↛_"
"_←_" "_→_" "_↔_"


Math

How do I do arithmetic?

Keywords: addition, subtraction, multiplication, division, modulus, exponentiation.

Note that mathematical order of operations are preserved in Avail (PEDMAS).

"_+_" "_-_" "_×_"
"_÷_" "_^_" "_mod_"

What are the comparison operators?

Keywords: greater than, less than, equal, greater than or equal, less than or equal, not equal

Character comparison is based upon a character's numeric code point

"_>_" "_<_" "_=_" "_≤_" "_≥_" "_≠_"

What other math operations are available?

Keywords: factorial, GCF, LCD, natural log, logarithm, maximum, minimum, summation, n-ary product

How do I do bitwise operations?

Keywords: NOT, AND, OR, XOR, shift



Conditionals

How do I build conditionals?

Keywords: IF-THEN-ELSE

How do I build chained conditionals?

Keywords: ELSE-IF



Collections

Synonyms: tuples, arrays, maps, sets, containers, lists, dictionary, groups, bag, queue, stack.

Each of the displayed methods are generally applicable for tuples, maps, sets given some potential syntactical differences.

How do I check to see if a collection is empty?

How do I test the elements of a collection?

How do I count the number elements of a collection that meet a specific criteria?

How do I filter the elements of a collection?

Or, "How do I find every element of a tuple/set/map that satisfies a predicate function?"

Or, "How about rejecting each element of a tuple/set/map that satisfies a predicate function?"

This does the same computation, but in parallel.

Or, "How do I group or classify the elements of a tuple/set by the result of a function returning a classification?"



Maps

Synonyms: associative array, dictionary, hash table.

How do I specify an empty map?

How do I create a literal map?

Literal maps can be constructed using arbitrary value producing expressions, not just literal expressions.

If the same key is repeated, then only the rightmost binding for that key will occur in the resulting map.

How do I add a binding to a map?

Maps are immutable, so you can't add a binding. But it's easy to produce a new map given an existing map and a key→value binding.

If the key matches one already present in the map, then the binding is "replaced" in the new map.

How do I remove a binding from a map?

Maps are immutable, so you can't remove a binding. But it's easy to derive a new map that doesn't include a binding for a particular key.

How do I get the size / cardinality of a map?

How do I subscript / index / query a map?

Or, "How do I look up a value in a map given its key?"

If you don't know if a subscript will be valid, then you can provide an else function to apply if the subscript isn't valid.

How do I enumerate the keys of a map?

How do I enumerate the values of a map?

How do I enumerate the bindings of a map?

Or, "How do I convert a map into a tuple of key-value pairs?"

How do I check to see if an element is a map key?



Sets

How do I specify an empty set?

How do I create a literal set?

Literal sets can be constructed using arbitrary value producing expressions, not just literal expressions.

How do I get the size / cardinality of a set?

How do I add/remove an element to a set?

"_+_" "_-_"

How do I check to see if an element is in a set?

"_∈_" "_∉_" "_∋_" "_∌_"

How do I test for subset/proper subset of a set?

"_⊂_" "_⊄_" "_⊆_" "_⊈_"
"_⊉_" "_⊇_" "_⊃_" "_⊅_"

How do I calculate the set union/intersection/symmetric difference?

"_∩_" "_∪_" "_Δ_"

How do I create a set from a tuple?



Tuples

Synonyms: array, list, ordered collection, sequence, slice.

Be aware that tuple subscripts are one-based, not zero-based.

How do I specify an empty tuple?

How do I create a literal tuple?

Literal tuples can be constructed using arbitrary value producing expressions, not just literal expressions.

How do I create a tuple of related integers?

Or, "How do I create an interval?"

"_to_" "_to_by_"

How do I get the size / length / cardinality of a tuple?

How do I subscript / index a tuple?

If you don't know if a subscript will be valid, then you can provide an else function to apply if the subscript isn't valid.

How do I acquire the frequencies of the elements in a tuple?

How do I replace the element at the specified subscript of a tuple?

Tuples are immutable, so you cannot replace their elements. However, you can easily (and efficiently) derive a new tuple that differs from an old one by only one element.

How do I slice a tuple?

Or, "How do I get a consecutive range of elements from a tuple?"

If you don't know if a range will be valid, then you can provide an else function to apply if the range isn't valid.

How do I replace a slice of a tuple?

Or, "How do I replace a consecutive range of elements from a tuple?"

Tuples are immutable, so you cannot replace their elements. However, you can easily (and efficiently) derive a new tuple that differs from an old one by a consecutive range of elements.

How do I concatenate tuples?

How do I append an element onto a tuple?

Tuples are immutable, so you cannot append an element onto a table. However, you can easily (and efficiently) derive a new tuple that have the new element appended.

How do I split a tuple at an index or by a predicate?

How do I sort a tuple?



Loops

How do I create a simple index-based loop?

Keywords: for loop

Here's an old-school subscripting loop.

How do I create various while loops?

How can I loop through each element of a tuple/set/map/reader/stream?

Keywords: enhanced for loop

There are lots of ways to accomplish this, each of which is the same general syntax for each data structure. Here are just a few.

Here's how to loop through the tuple in parallel. Note that the result is unstable.

Here's how to do the same parallel loop for a set.

...And a map

How do I transform each element of a tuple?

Or, "How do I map a tuple through a function?"

These do the same computation, but in parallel.



Strings

Strings are tuples of Characters. Strings respond to all tuple methods. Each element of a string (character) respond to character methods.

How do I represent a character?

How do I get a character's code point? character representation from a code point?

How do I inspect characteristics of a character?

How do I convert characters?

How do I convert non-string values to strings?

Keywords: stringification, stringify, textual representation.

How do I print strings to the standard output stream?

Keywords: printing, standard out, I/O.

Printing returns no value, so it has to be executed in a "Run_" block on the Availuator.

"Print:_"

How do I trim leading and trailing whitespace?

"trim_"

How do I format strings?

Keywords: formatting, formatter, printf.

The input in the Availuator is a text field. Because of this, it doesn't recognize newlines. In order to break continuous strings over multiple lines, ignoring newlines and other whitespace, in an actual avail module, you'd insert a back slash (\) at the end of the line immediately preceding the newline character. On the next line, all white space is ignored up to the first occurrence of the combination of a back slash (\) immediately followed by a pipe (|). The following example is meant for use in an Avail module and will not run in the Availuator.

The following example has been created to work specifically in the Availuator. This will work as is in an Avail module, however, all the newlines will be captured as they are literally placed in the current code. To run this example in an Avail module utilizing continuous string breaking formatting, the above line breaking methodology would need to be implemented at each newline. Indentation at the next line would also be added before the "\|" characters. We considered not adding the following example because of this, but I had too much fun building it not to show it!



Exceptions

Exceptions are forms of objects in Avail.

Synonyms: errors.

How do I raise an exception?

Note that the result of running the following code snippets in the Availuator will result in the reporting an error.

"Raise_"

How do I handle exceptions?

Keywords: exception handling, error handling, unwind


[1] Be sure that you have already loaded the Availuator by double-clicking it in one of the tree views on the left side of the workbench. Otherwise the workbench won't be able to find the "`!_" entry point.

Return to Documentation