Composer Functional Programming Library

Post your working scripts, libraries and tools
Posts: 95
Joined: 20 May 2014, 21:24

Composer Functional Programming Library

17 Oct 2014, 15:38

This Project is Abandoned

Composer is dependent on Plaster, which has been abandoned for reasons discussed on its page.

AutoHotkey's lack of tracing garbage collection (to handle, for example, closures) seems to preclude implementing a functional programming language atop it.

Composer is a library of common functional programming constructs that is intended to encourage writing composeable code in AutoHotkey. This is challenging because the built-in commands and functions make heavy use of mutable global variables and goto labels, commands cannot be composed, the call stack size is fixed, tail call elimination is not performed, and anonymous functions and closures are not supported. Code without observable side effects and unstructured control flow (i.e. composeable code) is easier to test and reuse.

Composer requires Plaster for error handling and polymorphism.

It is written in AutoHotkey L v1.

It is currently in a very early state of development. It is incomplete, lacks proper documentation, error handling, and testing. The API is unstable.

Only the constructs necessary to build functions at run time have been completed. Those are Compose, Curry, Delay, and FixArity.

Compose nests a pair of functions, combining them into a single function, like g(f(x)) in mathematics. Composed functions can be composed. It is often used when you want to build a function at run time and need to pass a function's return value to another function, as opposed to passing the function as a value.

Code: Select all

Add2(X) {
    return X + 2

MulBy5(X) {
    return X * 5

Add2ThenMulBy5 := Compose.(Func("MulBy5"), Func("Add2"))

MsgBox, % Add2ThenMulBy5.(3)  ; Shows: 25
Curry converts any function that takes more than one argument into a function that takes a single argument and returns a copy of itself with that argument closed-over (stored) until it has all the arguments needed to run. It is often used when you want to build a function at run time and need to pass a function to another function as a value, as opposed to passing its return value.

Code: Select all

MyAdd(X, Y) {
    return X + Y

CurriedMyAdd := Curry.(Func("MyAdd"))
Add8         := CurriedMyAdd.(8)

MsgBox, % Add8.(6)  ; Shows: 14
Delay combines any function with an array of parameters to pass to the function, resulting in a function that takes no arguments. Curried functions cannot store their last argument, unlike those produced by Delay. Delay can be used to perform lazy evaluation.

Code: Select all

MyDiv(X, Y) {
    return X / Y

Delayed7 := Delay.(Func("MyDiv"), [21, 3])

MsgBox, % Delayed7.()  ; Shows: 7.000000
FixArity converts any variadic function to a non-variadic function. It is used to adapt variadic functions for use with Curry.

Code: Select all

Array1to2 := FixArity.(Array, 1, 2)

MsgBox, % Repr.(Array1to2.())  ; throws Exception("type error",, "FixArity(Func(""Array""), 1, 2).() expected >= 1 argument")

MsgBox, % Repr.(Array1to2.("a", "b"))  ; Shows: ["a", "b"]
Compose, Curry, Delay, and FixArity, use __Call to produce objects that are callable like the function references returned by Func. These objects can store data, which can be used to close over the values they need to run. They have the same interface as function references, so they can be used anywhere a 'normal' function reference is used. I mention this in case anyone wonders how it works.
(15.12 KiB) Downloaded 88 times
Last edited by [Shambles] on 17 Jul 2015, 07:11, edited 7 times in total.
User avatar
Posts: 7898
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada

Re: Composer Functional Programming Library

17 Oct 2014, 18:17

Post some example usage! ;)

Re: Composer Functional Programming Library

17 Oct 2014, 20:19

joedf wrote:Post some example usage! ;)
Will do. I want to tighten up some error handling before moving on to what I expect to be the easier, but more visible, parts of the library. That means my examples might not be all that practical. Most people will want to curry things like Map, but I don't have Map in the library yet, for example.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 22 guests