.. _structures:
===============
Data Structures
===============
.. Modified: 2018-12-05/03:03-0500 btiffin
.. Copyright 2016 Brian Tiffin
.. GPL 3.0+ :ref:`license`
.. This file is part of the Unicon Programming documentation.
.. image:: images/unicon.png
:align: center
.. only:: html
:ref:`genindex`
:floatright:`Unicon`
One of the features that elevates Unicon from a high level language to a
*very* high level language is the range of structured data types.
Unicon mutable data types
=========================
.. index:: List
.. _list:
List (arrays)
-------------
Unicon lists are *mutable*, ordered collections of data. Each element can be
any datatype. Lists are created with square bracket syntax, or the
:ref:`list-function` function. ``list()`` accepts an optional size parameter,
followed by an optional default initial value.
:ref:`&null` is the default initial value if not specified.
Appending to a list is supported with the :ref:`|||` operator, which can be
augmented with assignment (as is the case for almost all Unicon operators).
List functions include stack and queue like operations, along with arbitrary
positioning.
- :ref:`push`, :ref:`pop` (front of the list)
- :ref:`put`, :ref:`pull` (end of the list)
- :ref:`insert`\ (L, i, x) will insert |x| at position *i*
- :ref:`delete` \ (L, i,...) will delete the elements at position *i* and
other given indexes.
Subscript indexing of a list is supported with square bracket syntax.
``List[index]``. This is where Unicon lists, and arrays
look very similar. Lists are one-dimensional vectors. Subscripting will fail
if the requested element(s) are out of current list size range.
Create new list sections with square bracket and colon syntax,
``L[start:end]``. *This syntax creates new lists*.
Subscript range access creates new lists. List sections are not variable;
the original list is not modifiable using sections. Where as ``Groceries[1]
:= "Milk"`` modifies the ``Groceries`` list, ``Groceries[1:3] := "Milk"`` is
invalid; the ``Groceries[1:3]`` section expression is not a variable and
assignment does not apply.
The :ref:`get` function will remove and return multiple elements from the head
of a list.
.. index:: comprehension
list comprehension
..................
While the normal list creation syntax is ``[v1, v2,...]``, Unicon also
features list `comprehension`. The syntax is ``[: expr :]``, all values from
the expression become values in the returned list.
.. literalinclude:: examples/list-operations.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/list-operations.icn`
.. command-output:: unicon -s list-operations.icn -x
:cwd: examples
--------
.. index: Set
.. _set:
Set
---
Unicon Set data is an unordered, *mutable* data structure that follows set
theory. Sets are initialized with the :ref:`set` function.
Union, intersection, difference, insertion and membership testing are all
supported. Set elements are unique within a set.
:ref:`cset` is an internally efficient form of character sets.
.. literalinclude:: examples/set-operations.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/set-operations.icn`
.. command-output:: unicon -s set-operations.icn -x
:cwd: examples
--------
.. index:: Table
.. _table:
Table
-----
Unicon tables are associative arrays, key-value pairs. Keys can be any type,
values can be any type. Created with the :ref:`table` function, access is by
square bracket subscripting (where the subscript is a key specifier).
:ref:`insert`, :ref:`delete` are supported.
Structure operators such as :ref:`* ` size and :ref:`? ` random
element operators (returning a random table value, not a key) are also built
to work with tables.
The :ref:`!` generate elements operator, generates values not keys.\ [#kv]_
The function :ref:`key`\ (T) generates the keys of a table.
Subscripted access will return a default value if lookup does not find the
key. The default value is the optional argument of the :ref:`table`
declaration function, or :ref:`&null`.
The :ref:`member` function succeeds if the given key exists in the table, or
fails otherwise.
.. literalinclude:: examples/table-operations.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/table-operations.icn`
.. command-output:: unicon -s table-operations.icn -x
:cwd: examples
.. todo:: more on tables
.. [#kv]
The fact that the :ref:`\! ` operator generates values and not
keys for table data was deemed an early Icon language design mistake.
Before Icon v7, you needed to convert a table to a paired list to get at
keys. The :ref:`key` function was added to get around this design flaw.
Key value pair associative data tables were originally in :ref:`SNOBOL`.
This extremely useful Unicon data structure has a long history,
--------
.. index:: Record
.. _record structure:
Record
------
Records are fixed length\ [#fixed]_, named field structures, created with a
globally named initializer defined with the :ref:`record` reserved word at
compile time. Unicon record structures can also be defined at runtime with the
:ref:`constructor` function.
Compile time ``record`` definitions occur outside of procedures in source
code. The initializer is global to all procedures within a compile unit.
:ref:`constructor` definitions are contained within the same scope as the
variable used to store the return value from the function.
``record`` data plays a background role in the object oriented features of
Unicon, but programmers don't need to worry about that, details managed by the
compiler.
.. literalinclude:: examples/record-operations.icn
:language: unicon
:start-after: ##+
.. only:: html
.. rst-class:: rightalign
:download:`examples/record-operations.icn`
.. command-output:: unicon -s record-operations.icn -x
:cwd: examples
.. [#fixed]
Records are fixed length in terms of always having the same number of
named fields. The field elements themselves can be any data type, of
variant sizes. This is not the same as a COBOL fixed length record, which
always have the same number of bytes in each field and in each record
hierarchy.
.. only:: html
..
--------
:ref:`genindex` | Previous: :doc:`datatypes` | Next: :doc:`expressions`
|