Coder Social home page Coder Social logo

Add fromArray method about kefir HOT 11 CLOSED

kefirjs avatar kefirjs commented on May 23, 2024
Add fromArray method

from kefir.

Comments (11)

rpominov avatar rpominov commented on May 23, 2024

Actually .once() was removed in master branch, and next version probably won't have it.

I still don't sure synchronous sources (.fromArray and .once) a good thing to have in an FRP library. The problem is those sources don't fit well into original FRP semantics. This topic was briefly discussed here baconjs/bacon.js#383 (comment) , and Bacon also has an issue about this specific problem baconjs/bacon.js#367 .

For now I try to avoid adding synchronous sources. It would be good to have here some use case examples of how .fromArray() / .once() could be useful.

from kefir.

brandonhorst avatar brandonhorst commented on May 23, 2024

I find it mostly useful in combination with flatMap. Let's say I have a function that accepts a Stream a single input and can return any number of outputs. If I want to flatMap these, it's trivial with .once() and .fromArray().

stream.map(function (index) {
  var options = [null, "single", ["array1", "array2"]];
  return options[index];
}).flatMap(function (option) {
  if (option === null) {
    return Kefir.never();
  } else if (typeof option.length !== 'undefined') {
    return Kefir.fromArray(option);
  } else {
    return kefir.once(option);
  }
}).log();

Envision inputStream = [2, 0, 1]::

<value> array1
<value> array2
<value> single
<end>

Without kefir.once(), how would you handle a case like this?

from kefir.

rpominov avatar rpominov commented on May 23, 2024

You can use .constant instead of .once.

But for .fromArray there is no alternative now. In some cases .sequentially(0, arr) may be used, but it not quite same thing as .fromArray(arr).

I also consider adding support for simple arrays in methods like .flatMap or .zip, so you can return just an array from mapping function and all its items will be emitted. But I don't sure if it better than just have .fromArray method.

Also, although it a bit awkward, you can use .withHandler when you need .flatMap with arrays support:

stream.map(function (index) {
  var options = [null, "single", ["array1", "array2"]];
  return options[index];
}).withHandler(function(emitter, event) {
  if (event.type === 'end') {
    emitter.end();
  }
  if (event.type === 'value') {
    var option = event.value;
    if (option === null) {
      // nothing to do here
    } else if (typeof option.length !== 'undefined') {
      option.forEach(emitter.emit);
    } else {
      emitter.emit(option);
    }
  }
}).log();

I hope to write docs for .withHandler soon :)

from kefir.

brandonhorst avatar brandonhorst commented on May 23, 2024

.withHandler() may actually work better than .flatMap() for my purposes. I'll toy around with it.

That's my only real use-case for .fromArray() so I'll close this out - thank!

from kefir.

beckyconning avatar beckyconning commented on May 23, 2024

surely that withHandler configuration is a common case? i think there is need for this or something like pluckElements.

use case might be getting a list of change notifications, splitting them up into individual change notifications, then getting the changes outlined in the change notifications

with flatMap and fromArray:

changes.pluck('results')
    .flatMapLatest(function (results) { Kefir.fromArray(results); })
    .pluck('id')
    .flatMap(getDoc);

with pluckElements

changes.pluckElements('results')
    .pluck('id')
    .flatMap(getDoc);

from kefir.

beckyconning avatar beckyconning commented on May 23, 2024

basically any time you get a list as input and want to operate on an stream of its elements currently you have to use the handler function above.

from kefir.

rpominov avatar rpominov commented on May 23, 2024

Take a look at the .transform method. I think it is what you need.

Btw .transform will be renamed to .flatten in v0.2.10 which I will probably release later today.

from kefir.

beckyconning avatar beckyconning commented on May 23, 2024

oh i see!

Kefir.sequentially(2000, [[1,2,3],[4,5,6],[7,8,9]]).transform(function (xs) { return xs; }).log()

this is even better than using fromArray with flatMap. renaming this to flatten is really going to help people find this feature. i'd recommend including an example like the one above in the docs too.

thanks!

from kefir.

rpominov avatar rpominov commented on May 23, 2024

Thanks, I'll add example like this.

You can also omit handler function argument, it's function(x) {return x} by default.

Kefir.sequentially(2000, [[1,2,3],[4,5,6],[7,8,9]]).transform().log()

from kefir.

beckyconning avatar beckyconning commented on May 23, 2024

thanks! i was going to suggest that it should have that behaviour as defauly but then it and it worked lol!

thanks for all the great work. kefir is a really elegant library : )

from kefir.

gnapse avatar gnapse commented on May 23, 2024

I came to this repo to find some issue about fromArray not being available, or to post that issue myself. But I have to say that after reading this discussion and the referenced issue in the bacon library, and after doing some tests around all that, I'm glad this function is not included (or any other synchronous sources for that matter). It forces other developers to dig into the issue and find about the drawbacks of synchronous streams, just like I did. As for a workaround, you can always call sequentially with a very low interval value, perhaps even zero.

from kefir.

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.