caolan / caolan/highland

Head forking confusion

Open
#214 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
3.4k
Forks
145
PR merge metrics
No merged PRs in 30d

Description

Hey,

I'm having trouble understanding how to get the head of a stream. For example

```
var base = _([1,2,3,4,5,6,7,8,9,10]);
var head = base.fork().head()
head.each(console.log);
head.resume();
var sum = base.fork().reduce(0, add)
sum.each(console.log);
sum.resume();
```

Doesn't calculate sum. I assume this is because the head fork quits consuming after the first element. There's two solutions I can come up with.

```
var base = _([1,2,3,4,5,6,7,8,9,10]);
var head = base.fork().head()
head.each(function (i) {
console.log(i);
head.destroy();
});
head.resume();
var sum = base.fork().reduce(0, add)
sum.each(console.log);
sum.resume();
```

Does not work. No idea why. Isn't destroy supposed to detach the consumer from source thereby prevent blocking?

Another way that comes to mind is to use observe instead of fork. This works. However, if I understand correctly, this will keep buffering the source stream.

What am I missing here?

Contributor guide

Open the contributing guide

Research direction

Start with the fork(), head(), destroy(), and observe() entry points used in the examples, and trace how each consumer interacts with the shared source. Reproduce the provided reduce example and determine whether head() can stop consuming without blocking the other fork; done means the behavior is explained or corrected and the example calculates the sum.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
stream-processing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.