metarhia / common

Metarhia Common Library 🗜️
https://metarhia.com
MIT License
63 stars 34 forks source link

Chain composition #37

Closed tshemsedinov closed 6 years ago

tshemsedinov commented 7 years ago

@DzyubSpirit I think to compose chain we can use following syntax:

const composedChain = compose().filter(x => x > 0).map(x => x * 2).map(x => ++x);

And then use it:

const result = composedChain(data);

It will be equivalent for expression/superposition:

const result = data.filter(x => x > 0).map(x => x * 2).map(x => ++x);

But unknown data. We can also do it easier:

const composedChain = data => data.filter(x => x > 0).map(x => x * 2).map(x => ++x);
const result = composedChain(data);

But in case of simple compose we also can do as lambda:

const composedFunc = x => f1(f2(x));

But function compose gives us flexibility and unification. So chain composition may give us something interesting. Need more examples, I think.

DzyubSpirit commented 7 years ago

@tshemsedinov, one more option is:

const composedChain = compose(
  filter(x => x > 0), arr => (arr[0] = 2, arr), map(x => ++x)
);
tshemsedinov commented 7 years ago

@DzyubSpirit this syntax require filter, map and other methods visible in global context.

DzyubSpirit commented 7 years ago

@tshemsedinov, no, it can be used as that:

const M = module;
const composedChain = compose(
  M.filter(x => x > 0), arr => (arr[0] = 2, arr), M.map(x => ++x)
);

That is harder, I agree. There is trade off between complexity of getting functions from module and ability to use custom functions (like second in example)

tshemsedinov commented 6 years ago

Moved: https://github.com/metarhia/metasync/issues/274