leebyron / iterall

🌻 Minimal zero-dependency utilities for using Iterables in all JavaScript environments.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Set and Get methods

rt2zz opened this issue · comments

Libraries that work with iterables often need to get or set values by key or index. Would it make sense for iterall to support get & set?

Can you explain a bit more how you would expect this to work?

Case in point we have a long standing issue to support immutablejs map as state for redux-persist. Rather than make a special case for Map, it would be nice to be able to drop in iterall to support any type of iterable.

To do this we would need get and set, e.g.:

var get = require('iterall').get
var set = require('iterall').set

//...
state = state.set('fooReducer', 'bar')
var foo = state.get('fooReducer')

Of course this assumes we treat all iterables as immutable which is a bit opinionated...

I do not have an easy solution, but if get & set were part of the protocol I think redux-persist and likely many other projects would be eager to adopt.

state = state.set('fooReducer', 'bar')
var foo = state.get('fooReducer')

This example assumes set and get are prototype methods on whatever state is. What would they look like for the versions that are imported?

And I'm still a bit confused. I'm not sure I understand how you're thinking get and set might be implemented or how they should behave for any given Iterable.

Consider:

function* infiniteBlue() {
  while (true) {
    yield "blue";
  }
}

var iterable = infiniteBlue();

// get?
// set?

What would get and set do with this? What would be the API you imagine and what behavior would you expect?

Ah sorry, in my example I meant for it to be get(state, 'fooReducer') rather than a prototype method.

Nevertheless, your point is taken that not all iterables have a concept of "keys".