mobily / ts-belt

🔧 Fast, modern, and practical utility library for FP in TypeScript.
https://mobily.github.io/ts-belt
MIT License
1.11k stars 31 forks source link

`A.takeWhile` acting like `A.filter` #60

Closed ssnielsen closed 1 year ago

ssnielsen commented 1 year ago

First of all, thanks for a solid library - it's really a breeze to use!

While doing a bit of Advent of Code, I noticed unexpected output from A.takeWhile - essentially it seems to act more like A.filter

Examples:

// ts-belt:
A.takeWhile([3, 5, 3], n => n < 4); 
// Returns [3, 3]

I'd expect to get back the single-element list: [3].

Ramda (and other similar libraries):

// ramda:
R.takeWhile(n => n < 4, [3, 5, 3])
// Returns [3]

Smells a bit like a bug to me 😉

Also - looking at a bit further, it looks like this case isn't covered by the tests

Btw, the version in question is 3.13.1

ssnielsen commented 1 year ago

Btw, if you're ok with it, I'm definitely willing to give it a go at fixing it 😉 (forgot to say that above)

mobily commented 1 year ago

@ssnielsen thanks for spotting this issue! indeed, takeWhile was implemented incorrectly, I fixed it up in v4.0.0-rc.2 :)

test case: https://github.com/mobily/ts-belt/commit/235f223a879fc45cc1a2da7cf27d8cf78d15a4e0#diff-39838e57d36e74e0f3b610bc317412d00c08c0f67a99d5b8f1cae4784ae33cfbR9

benchmarks:

CleanShot 2022-12-10 at 12 59 17@2x
lightwave commented 1 year ago

Any chance this fix might be back-ported to v3?