Stax is a new standard library for HaXe designed for developers who prefer declarative programming and productivity over program size and speed.
The standard library that comes with HaXe is pragmatically designed. The library is small, and there are only a few abstractions. As a result, the HaXe standard library is an excellent choice when the most important features are program size and performance.
Stax lies on the other end of the spectrum. Stax emphasizes functional concepts, generic abstractions, productivity, composability, and testability. When these features are most important, Stax makes an excellent choice.
Getting Starting with Stax
Stax includes a useful bundle of functionality in the Prelude. To get started using the functionality in the Prelude, simply add the following two lines to your applications:
import Prelude; using PreludeExtensions;
This brings a slew of functionality into your application:
Tuples, of arity 1 – 5, which are ideal containers for a bundle of times when a class would be too heavyweight.
var t = Tuple2.create(23, "foo");
Option, which eliminates the need for
nulland makes code much safer.
var o = Some(23); var n = None;
- Future, which makes it easy to chain asynchronous operations.
Conversions, which enable you to convert between primitives.
var float = 12.toFloat();
Array enhancements, which allow you to write functional code with arrays.
[123, 24].map(function(i) return i * 2;);
Function enhancements, which allow you to manipulate functions like first-class values.
var h = f.compose(g); h.curry()(2)(3);
Basic typeclasses, which allow you to hash, order, and show all built-in HaXe primitives.
var hasher = Int.HasherF(); var hash = hasher.hash(3243);
The major abstractions introduced in the Prelude are covered in more depth in section 3.
The above constitutes all the functionality built into the Prelude. However, Stax includes many more components, such as:
- A unified collections library, including immutable, fully-featured versions of Set, Map, and List.
- JSON encoding, decoding, and transformation;
- Functional reactive library;
- And more!
Using Prelude Abstractions
Stax Collections Library
The Stax collections library, contained in the package haxe.data.collections, includes three types of collections:
- Lists (haxe.data.collections.List), which represent ordered sequences of elements;
- Sets (haxe.data.collections.Set), which represent unordered collections of unique elements;
- Maps (haxe.data.collections.Map), which represent partial functions from one type (the key) to another (the value).
All collection are immutable, support arbitrary element types, and are unifed via a
Collection interface, which provides access to functionality that is universally available across all kinds of collections (
Collections implement the
Foldable interface, so it’s possible to gain access to more advanced functionality by using the
Collections can be created through a static factory method on the collection classes named create. This function typically requires access to equal, hash, or ordering type classes for the specified element type(s).
Stax has a fully-typed representation of the vast majority of the DOM, including the latest W3C standards and widely supported non-standards.
You can import this representation and helper functions with the following two lines of code:
import js.Env; import Dom;
The first line imports a