Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fork/join and general parallelization? #36

Open
chadobado opened this issue Apr 24, 2015 · 1 comment
Open

Fork/join and general parallelization? #36

chadobado opened this issue Apr 24, 2015 · 1 comment

Comments

@chadobado
Copy link

Curious how forking and joining might be accomplished with this library?

..similar to this.

Something like this looks really verbose (and isn't really parallel), so assuming there is a better way?

var processedBags = []

var transformFragile = compose(
    filter(x => x.bagType == 'fragile'),
    map(x => merge(x,{luggageTag: 'Handle with care'}))
);

var transformHeavy = compose(
    filter(x => x.bagType == 'heavy'),
    map(x => merge(x,{luggageTag: 'Watch your back!!'}))
);

var transformNormal = filter(x => x.bagType == 'normal')

processedBags = into(processedBags, transformFragile, bags)
processedBags = into(processedBags, transformHeavy, bags)
processedBags = into(processedBags, transformNormal, bags)

Also attempted other variations w/a switch statement inside a mapper. Also doesn't feel right.

Thanks in advance.

@jaawerth
Copy link

jaawerth commented Sep 5, 2015

You'd probably want to do it with iterators. Fork spits out an object of key-iterator pairs - you can then keep working on them as needed and merge them back together. The bags use-case probably isn't a good one for forking (there's a better way to do it), but here's what forking would look like at least how I sometimes do it:

// Reusable fork and merge
var fork = filtersObj => itemList => seq(filtersObj, map( ([key, filter]) => [key, toIter(itemList, filter)] ));
var merge = forkedObj => toArray(forkedObj, mapcat(kp => kp[1]));

var transformFragile = compose(
    filter(x => x.bagType === 'fragile'),
    map(x => Object.assign({}, x, {luggageTag: 'Handle with care'}))
);
var transformHeavy = compose(
    filter(x => x.bagType === 'heavy'),
    map(x => Object.assign({}, x, {luggageTag: 'Watch your back!!'}))
);
var transformNormal = filter(x => x.bagType === 'normal')

var transform = compose(merge, fork({transformFragile, transformHeavy, transformNormal}));
var processedBags = transform(bags);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants