Giter Club home page Giter Club logo

Comments (6)

paldepind avatar paldepind commented on June 28, 2024

That is a very good question! You're indeed right that the function passed to flyd.stream needs to have its dependents in scope to be of much use. But let me show you why I think that actually isn't a real issue.

The documentation sadly doesn't explain this properly (sorry), but flyd.stream is considered only to be the low level building block of Flyd. You should use flyd.stream to create modules for Flyd – but nothing else. flyd.stream is a meant to be a powerful way to implement new abstractions in modules.

This is how I would use it to tackle the problem in your specific example. First of all, note the following things about the function you're exporting in callback.js:

  1. It actually doesn't need access to self and changed.
  2. It operates on two streams.
  3. The function would be more naturally expressed as a function taking two arguments: function(x, y) { return x + y; }

Now, what if we had an abstraction that allowed us to apply a function taking two arguments to two streams and get a new stream? Such that we could simply define the exported function as above and invoke it with the two streams. Building this abstraction with flyd.stream is very easy:

module.exports = function(fn, stream1, stream2) {
  return flyd.stream([stream1, stream2], function() {
    return fn(stream1(), stream2());
  });
};

With this function your example would be implemented like this:

// callback.js

module.exports = function(x, y) {
  return x + y;
}

// index.js

var lift2 = require('./lift2'); // The function defined above
var callback = require('./callback');

var x = flyd.stream(4);
var y = flyd.stream(6);

var sum = lift2(callback, x, y);

A more general function like the above would work with functions taking any number of arguments. Such a function is implemented in the module flyd-lift.

I hope the above explanation makes sense to you. Do you still think it is a problem that the callback to flyd.stream needs to have its dependent streams in scope? It hasn't been a problem for me so far but you might see something I've missed. If is actually is a problem then passing the dependent streams to the callback after self and changed might be the solution.

If you have any ideas about how I could make the documentation clearer and more easy to understand please let me know :)

from flyd.

paldepind avatar paldepind commented on June 28, 2024

Oh, and btw: When I say that flyd.stream is primarily for implementing modules I only mean the stream(dependencies, fn) variant. flyd.stream(constant) should generally be used for creating what I refer to as "top level" streams in the tutorial.

from flyd.

sporto avatar sporto commented on June 28, 2024

Not really sure if this is a problem to be honest, I posted the issue because this jumped to my attention when reading the readme. Feel free to close it.

from flyd.

paldepind avatar paldepind commented on June 28, 2024

Ok. I will keep it in mind but for now I don't think this is an actual problem.

from flyd.

jayrbolton avatar jayrbolton commented on June 28, 2024

Oh, and btw: When I say that flyd.stream is primarily for implementing modules I only mean the stream(dependencies, fn) variant. flyd.stream(constant) should generally be used for creating what I refer to as "top level" streams in the tutorial.

@paldepind I found this issue after having some of these same questions and seeing other people react skeptically when I showed them the readme.

I agree it's not an actual problem as a low-level utility function, but I might suggest that we simply move its description down to the bottom of the readme under a section such as "Creating Modules" or "Extending Functionality", and instead put examples for higher level abstractions like map, merge, lift, etc at the top.

from flyd.

paldepind avatar paldepind commented on June 28, 2024

@jayrbolton

I think that is made clear in the readme:

This is not a demonstration of how you would write code with Flyd on a day to day basis. For that take a look at the examples.

This tutorial will however introduce you to the minimal but powerful core that Flyd provides and show you how it can be used to build FRP abstractions.

But I like your suggestion about adding examples at the top.

from flyd.

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.