jeudi 28 juillet 2016

Functional Programming approach for Queue pattern

Can a queue handling async operations be implemented under the principles of Functional Programming? It's an AsyncQueue basically.

var list = [];
var inProgress = false;

function Queue() {}

Queue.prototype.enqueue = function(data) {
    list.push(data);
    if(!inProgress) {
         inProgress = true;
         start(list.shift());
    }
}

function start(data) {
    // initiate some async activity, and call dequeue on completion
}

function dequeue() {
    if(list.length) {
       start(list.shift());
    } else {
       isProgress = false;
    }
}

Evidently there's some kind of state getting managed, both with the list and inProgress, not sure how can this be converted into it's Functional Programming equivalent. F# code also welcome.

Aucun commentaire:

Enregistrer un commentaire