Arrows - Beautiful pipelines (Haskell - Typescript)

I'm always talking about arrows in Haskell and how it changed the way I think about pipelines. So, let's do something with it!

The main idea is how we can generalize the composition of computations.

NOTE: This post will only deal with simple functions. On a next one, we can do other more elaborated stuff.

NOTE 2: All the examples could be simplified, they are just to mere examples.

In theory, it says that an Arrow adds an “interface” for composition. We need a class that can hold a Function and promotes it into an instance of arrow.

The template variables <I, O> mean input and output.

class Arrow<I, O> {
  constructor(public readonly f: (value: I) => O) {}
}

To simplify, let’s make function to help us called `arr`. It just lift a function into an instance of the Arrow class.

function arr<I, O>(f: (value: I) => O) {
  return new Arrow(f);
}

In the Haskell package, Control.Arrow, the composition is defined as `>>>`. In our case, we are going to use a method of the Arrow class called `next`.

NOTE: Pay attention that the arrow `a` has its input the output of our current arrow and returns “D”.

public next<D>(a: Arrow<O, D>): Arrow<I, D> {
  const self = this;
  return new Arrow((value: I) => {
    return a.run(self.run(value));
  });
}

The last thing we need is to create a method to actually run our pipeline.

class Arrow<I, O> {
    constructor(public readonly f: (value: I) => O) {}

    public run(value: I): O {
        return this.f(value);
    }

    public next<D>(a: Arrow<O, D>): Arrow<I, D> {
        const self = this;
        return new Arrow((value: I) => {
            return a.run(self.run(value));
        });
    }
}

This gives us the ability to compose some simple functions.

const pipeline = arr(
    function(x: number) { return x + 1 }
).next(
    arr(
        function (y: number): string { return y.toString(); }
    )
);

console.log(pipeline.run(1));

Another useful composition is to send an argument to 2 “parallel” functions. It takes 2 arrows and returns an arrow that returns a pair of the results using one argument for both functions.

function fork<I, O, OO>(g: Arrow<I, O>, f: Arrow<I, OO>): Arrow<I, [O, OO]> {
    return arr((value: I) => {
        return [g.run(value), f.run(value)];
    });
}

Now, things are starting to get interesting.

const pipeline = fork(
    arr(function(x: number) { return x + 1 }),
    arr(function(y: number): string { return y.toString(); })
);

const [v0, v1] = pipeline.run(1);
console.log(v0 == 2 && v1 == “1”);

Another example just to illustrate how the composition works.

const pipeline = arr(
    function(): number { return 1; } // a constant arrow
).next(fork(
    arr(function(x: number) { return x + 1 }),
    arr(function(y: number): string { return y.toString(); })
));

const [v0, v1] = pipeline.run(undefined);
console.log(v0 == 2 && v1 == “1”);

As we are working of pairs of values, we can also define 2 more interesting functions: one that acts on the `first` element, and another for the `second` one.

function first<I, II, O>(g: Arrow<I, O>): Arrow<[I, II], [O, II]> {
    return arr(([v0, v1]: [I, II]) => [g.run(v0), v1]);
}

function second<I, II, O>(g: Arrow<II, O>): Arrow<[I, II], [I, O]> {
    return arr(([v0, v1]: [I, II]) => [v0, g.run(v1)]);
}

Here is what we can do.

const pipeline = arr(
    function(x: number) { return x + 1}
).next(
    fork(
        arr(function (x: number) { return x + 1; }), 
        arr(function (x: number) { return x.toString(); })
    )
).next(
    first(arr(function (value: number) { return value * 2; }))
).next(
    second(arr(function(txt: string) { return `bla bla bla ${txt}`; }))
).next(
    arr(function([v0, v1]) { return `[${v0}] ${v1}`; })
);

console.log(pipeline.run(1) == “[6] bla bla bla 2”);

There are many ways to compose and create other variants and compositions. With this building blocks, you can go ahead and implement what you need.

Check out the demo in this Typescript Playground.

On this topic, the next post we are going to compose a lot more stuff.

References:

Generalizing Monads to Arrows

Arrows: A General Interface to Computation

Haskell/Understanding arrows