Fixes #426. Add fj.data.NonEmptyList.sequence*, traverse*, foldLeftN, foldRightN, init, last. by drewctaylor · Pull Request #427 · functionaljava/functionaljava · GitHub
Skip to content

Fixes #426. Add fj.data.NonEmptyList.sequence*, traverse*, foldLeftN, foldRightN, init, last.#427

Open
drewctaylor wants to merge 1 commit into
functionaljava:series/4.xfrom
drewctaylor:functionaljava-426
Open

Fixes #426. Add fj.data.NonEmptyList.sequence*, traverse*, foldLeftN, foldRightN, init, last.#427
drewctaylor wants to merge 1 commit into
functionaljava:series/4.xfrom
drewctaylor:functionaljava-426

Conversation

@drewctaylor

Copy link
Copy Markdown
Contributor

Fixes #426. Add fj.data.NonEmptyList.sequence*, traverse*, foldLeftN, foldRightN, init, last.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is overly inefficient. init is O(n), last is O(n) and foldRight is O(n), so we have overall O(n^2). Compare this to List foldRight, which is O(n) for the reverse and then O(n) for foldLeft, for an overall O(n).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be reversing and then folding left,

@mperry mperry left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job, we'll need some updates though before accepting the PR.

/**
* Fold the list, from right to left.
*
* @param combine the combine function

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the param and return values, I don't think the textual description adds clarity to the types on the function parameters. Applies elsewhere.

public final <B> B foldRightN(
final F2<A, B, B> combine,
final F<A, B> transform) {
return tail().isEmpty() ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the F<A, F<B, B>> version should be implemented by one calling the other using curry/uncurry.

final F<A, B> transform) {
return tail().isEmpty() ?
transform.f(head()) :
init().foldRight(combine, transform.f(last()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be reversing and then folding left,

return tail;
}

public List<A> init()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial: Prefer the { on the same line as the method name. Applies elsewhere.

* @param combine the combine function
* @return the output
*/
public final A foldRight1(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer params on the same line as the method name unless it gets overly long, say >80 chars.

public <R, B> Either<NonEmptyList<B>, R> traverseEitherLeft(
final F<A, Either<B, R>> f) {
return foldRightN(
element -> either -> f.f(element).left().bind(elementInner -> either.left().map(nonEmptyList -> nonEmptyList.cons(elementInner))),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functions and those below use foldRightN, which needs fixing.

public class NonEmptyListTest {
@Test
public void testSequenceEither() {
assertEquals(right(nel("zero")), sequenceEither(nel(right("zero"))));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using ints, instead of strings would be easier and better.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 65.16854% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 52.64%. Comparing base (f0bbae5) to head (21b2315).
⚠️ Report is 9 commits behind head on series/4.x.

Files with missing lines Patch % Lines
core/src/main/java/fj/data/NonEmptyList.java 65.16% 31 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@               Coverage Diff                @@
##             series/4.x     #427      +/-   ##
================================================
+ Coverage         52.43%   52.64%   +0.20%     
- Complexity         2565     2617      +52     
================================================
  Files               153      153              
  Lines              9244     9314      +70     
  Branches            482      481       -1     
================================================
+ Hits               4847     4903      +56     
- Misses             4244     4256      +12     
- Partials            153      155       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Successfully merging this pull request may close these issues.

Add fj.data.NonEmptyList.sequence*, traverse*, foldLeftN, foldRightN, init, last.

3 participants