SWI-Prolog / pengines

Pengine and Prolog scratchpad
BSD 2-Clause "Simplified" License
57 stars 16 forks source link

Simple JS functions on top of the Pengines JS API? #7

Open torbjornlager opened 10 years ago

torbjornlager commented 10 years ago

Food for thought: Maybe a couple of simple JS functions should be defined on top of the JS API? Here for example is a function that will populate a DOM element identified by idwith the answer(s) to goal:

function populate(template, goal, id) {
    var pengine = new Pengine({
        ask: goal,
        template: template,
        onsuccess: function() {
            document.getElementById(id).innerHTML = this.data[0]
        }
    });
    return pengine;
}

Test with:

p = populate("X", "member(X, [1,2,3])", "mydiv");
p.next();
p.next();

populate("Xs", "findall(X, member(X, [1,2,3]), Xs)", "mydid");

Seems mostly useful for deterministic queries and should perhaps support only those? If so, we can replace the line

ask: goal,

with

ask: "once((" + goal + "))",

and we don't need populate() to return anything.

Anniepoo commented 8 years ago

It seems to me too short a goal.

I've been thinking about this for a while. I'd love to see a logical, reactive end to end framework built on pengines. I've been fiddling around, more with designs than code, but am not happy with what I have.

If you don't know 'angularjs' , (I think react is similar) you might want to look at it just as a model of what might happen. Angular's big issue is that it's only on the browser. I'd like to make that distinction more transparent.