Giter Club home page Giter Club logo

Comments (5)

JensRavens avatar JensRavens commented on July 24, 2024

I never really thought about using merge that much (just used it 2 days ago for the first time myself). It was mostly intended to be flatMapped into a common value on the next step anyway (like merging the text of a draft with a text that just got typed).

While I would prefer an array of Signals as the argument, this would not be type safe anymore (it would just be an array of signals, but you won't know each of their types).

Maybe you have an idea how to improve that situation?

For your current case if would be possible to specify a merge operation for signals of the same type like merge(signals: [Signal<T>]).

from interstellar.

schwa avatar schwa commented on July 24, 2024

I'm wondering if a multiple merge functions that took multiple signals might be better?

merge(a: Signal, b: Signal)
merge(a: Signal, b: Signal, c: Signal)
merge(a: Signal, b: Signal, c: Signal, d: Signal)

etc.

This way you keep type safety and a flat return structure and still support a reasonable number of signals.

In my code i have four signals at once (building up complex UI from two async http calls, a reverse geocode, and a map snapshot image). I could make them serial but they are intrinsically independent of each other.

from interstellar.

IwanKaramazow avatar IwanKaramazow commented on July 24, 2024

This was actually something I ran into immediately when using this library.
I mainly use Interstellar for form validation. Every form gets its own signal.
They all have to compose somehow to a main signal which holds a value that represents if all forms in certain view are valid. As a quick solution to this problem I came up with a function:

func combineSignals<T>(signals: [Signal<T>], f: (T,T)-> T) -> Signal<T> {
    return signals.dropFirst().reduce(signals.first!){ $0.merge($1).map { f($0) }
    }
}

It takes an array of signals and a function which 'merges' both values of a tuple into one.
Although this is very specific to my use case, it'll probably a good idea to somehow generalize this in version 2.0.

Maybe implement the transducers protocol?

from interstellar.

JensRavens avatar JensRavens commented on July 24, 2024

If all signals do have the same type, it would be possible to define a merge function like this:

extension Signal {
  public class func combine<T>(signals: [Signal<T>]) -> Signal<[T]>
}

from interstellar.

vittoriom avatar vittoriom commented on July 24, 2024

I would also be quite interested in such a function 👍

from interstellar.

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.