Giter VIP home page Giter VIP logo

Comments (6)

benlesh avatar benlesh commented on May 26, 2024

@domenic I need to more elaborate straw-man to understand what you're suggesting. Can you type up an example?

from es-observable.

trxcllnt avatar trxcllnt commented on May 26, 2024

@Blesh I remember mentioning a potential Function.create to @domenic in New York last year, and this sounds like the same idea. Essentially, something like:

// polyfill
Function.create = (proto, body) => {
    let newFn = (...args) => { return body(...args); };
    for(var key in proto) {
        if(proto.hasOwnProperty(key)) {
            newFn[key] = proto[key];
        }
    }
    return newFn;
};

let randomNumbersObs = Function.create(Observable.prototype, (observer) => {
    let id = setInterval(() => observer.next(Math.random()), 100);
    return _ => clearInterval(id);
});

let dispose = randomNumbersObs.map((i) => { value: i })({
    next(x) { console.log(x.value); }
});

from es-observable.

benlesh avatar benlesh commented on May 26, 2024

that is pretty interesting. As an orthogonal proposal, I think that's useful.

The real problem is that everyone would have to reimplement the guarantees of Observable. There is some inherent behavior in observable that pretty much requires it be a type.

from es-observable.

benjamingr avatar benjamingr commented on May 26, 2024

@Blesh well, it relates to observables (and other types) in interesting ways.

    class Observable extends Function { // the same way a generator is a function
        constructor(fn) { ... }
        map(fn) { ... }
        ...
        call(subscriber){
             // this is `.subscribe`
        }
    }

Which would make the API look like:

 var o = myObservableReturningFn.map(..).filter(...);

 o(v => console.log(v));

Instead of:

var o = myObservableReturningFn(...).map(..).filter(...);
o.subscribe(v => console.log(v));

The interesting thing is that because a function class is just an implementation of the continuation comonad we can apply this "non-strictness at invocation" to anything, functions returning arrays, promises or whatever. The limitation is that this comonad needs to be applied on a functor itself. We are basically building and composing functions here based on the return type getting an interesting signature of:

-- takes a function to `a` and return a function to `a` which is also an `a`
bind:: (() -> a) -> (a & (() -> a))

With a sufficiently clever typesystem this would be possible, in JS I don't think it is. We can settle for particular instances or "applying this pattern in the language". So the laziness is in not calling the function and not in an implicit laziness contract.

from es-observable.

zenparsing avatar zenparsing commented on May 26, 2024

I think the method-wrapper is important for readability, at least for Observable:

getObservable()(); // Looks weird
getObservable().subscribe(); // Better

from es-observable.

trxcllnt avatar trxcllnt commented on May 26, 2024

@zenparsing getObservable().call() is still an option, and you could implement subscribe to call under the hood.

from es-observable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.