Libraries
Libraries are a mechanism to reuse and share Snapi code. Snapi includes a large set of built-in libraries but as a developer you can also create your own libraries of Snapi code.
Libraries can be created and stored as files and then loaded from other Snapi scripts as needed.
For example, assume a file at github://demo-org/demo-repository/package1.snapi
with the following contents:
multiply_by_two(v: int) = v * 2
multiply_by_four(v: int) = v * 4
Then this can be used by any other Snapi script as follows:
let
x = Library.Load("github://demo-org/demo-repository/package1.snapi")
in
x.multiply_by_two(2) + x.multiply_by_four(2) // Result is 12
Note that the name multiply_by_two
and multiply_by_four
are the program declarations defined in the script. Refer to the section on Program declarations for additional details on how to define program declarations.
Creating a library
To create a library, create a Snapi script with at least one program declaration.
Libraries can be stored in publicly accessible HTTP URLs (using GET), or on public GitHub repositories.
Program declarations are made available as functions when the library is imported.
Using a library
To use a library, do Library.Load
as shown in the example above. The location can be either:
- a public unathenticated HTTP URL (accessed using HTTP GET);
- or a public GitHub repository. For GitHub, the syntax to use in
Library.Load
must begithub://<user or organization name>/<repository name>
(assumes default branch) orgithub://<user or organization name>/<repository>[<branch>]
to specify a particular branch.
For instance:
let x = Library.Load("github://demo-org/demo-repository/path/package1.snapi[test]")
... this loads the file at /path/package1.snapi
in branch test
for the GitHub org demo-org
in repository demo-repository
. The result is a library with each of the program declarations available to use.