Home

Awesome

FXP - AS3 Functional Programing

As implied by the name, FXP is a functional programing library for use with actionscript 3. It was initially inspired by the mostly adequate guide, which, even though uses javascript, is worth a read to get started. I won't enter into details about functional programming concepts provided by this library.

The library is divided into 3 main subpackages:

Maintainer

Please use github for issues and pull requests.

Short example

A short example of what this lib allows (yeah unpure), just gives a hint of what kind of abstractions you'll get.

// DisplayObjectContainer -> DisplayObject -> DisplayObject
public static var addChild:Function = F.curry(function(parent:DisplayObjectContainer, child:DisplayObject):DisplayObject {
    parent.addChild(child);
    return child;
});

// DisplayObjectContainer -> Array<DisplayObject> -> Array<DisplayObject>
public static var addChilds:Function = F.curry(function(parent:DisplayObjectContainer, children:Array):Array {
    return F.array.map(addChild(parent), children);
});

// Boolean -> DisplayObject -> DisplayObject
public static var setVisible:Function = F.curry(function(value:Boolean, object:DisplayObject):DisplayObject {
    object.visible = value;
    return object;
});

// DisplayObjectContainer -> DisplayObject -> DisplayObject
public static var addInvisibleChild:Function = F.compose(setVisible(false), addChild);

// DisplayObjectContainer -> Array<DisplayObject>
public static var buildObjectInto:Function = function(root:DisplayObjectContainer):Array {
    return addChilds(root, [
        new Button("Button 1"),
        new Button("Button 2")
    ]);
    .concat(addInvisibleChild(root, new Button("My Hidden Button")));
}

For the purists, same thing with IO (see fxp.monads.IO)

// DisplayObjectContainer -> DisplayObject -> IO<DisplayObject>
public static var addChild:Function = F.IO(function(parent:DisplayObjectContainer, child:DisplayObject):DisplayObject {
    parent.addChild(child);
    return child;
});

// DisplayObjectContainer -> Array<DisplayObject> -> Array<IO<DisplayObject>>
public static var addChilds:Function = F.IO(function(parent:DisplayObjectContainer, children:Array):Array {
    return F.array.map(addChild(parent), children);
});

// Boolean -> DisplayObject -> DisplayObject
public static var setVisible:Function = F.curry(function(value:Boolean, object:DisplayObject):DisplayObject {
    object.visible = value;
    return object;
});

// DisplayObjectContainer -> DisplayObject -> IO<DisplayObject>
public static var addInvisibleChild:Function = F.compose(F.map(setVisible(false)), addChild);

// DisplayObjectContainer -> Array<IO>
public static var buildObjectInto:Function = function(root:DisplayObjectContainer):Array {
    return addChilds(root, [
        new Button("Button 1"),
        new Button("Button 2")
    ])
    .concat(addInvisibleChild(root, new Button("My Hidden Button")));
}

// Our only unpure effect-land function
public static function unsafeBuildObjectInto(root:DisplayObjectContainer):void {
    IO.utils.performArray(buildObjectInto(root));
}

What happened here? F.IO() takes an unpure function as argument and turns it into a function that returns an IO monad instance. All but unsafeBuildObjectInto are now pure function, guaranteed to be free of side-effects!

Getting started

The simplest way to include Fxp into your project is to copy the files somewhere the compiler will find it.

cd /some/path
git clone https://github.com/j3k0/fxp-as3.git

Add /some/path/fxp-as3/src into the "source-path" of your project.

Why no .swc? Because.

API Documentation

Important notice: all functions provided by the API are "curried". If you don't know what that means, please read the mostly adequate guide before going any further.

License

The New BSD License.

Copyright (c) 2015, Fovea.cc

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.