Should there be a difference in result between `tr...
# arrow
m
Should there be a difference in result between
traversEither
on a List vs a Nel or am I using it wrong? If I run below Kotest code, The Nel one ends up with 4 elements (the head gets repeated at the end of the list)
Copy code
"Test traverseEither on list" {
        val t = listOf(1, 2, 3).traverseEither { i ->
            i.right()
        }

        val list = (t as Either.Right<List<String>>).value
        list shouldHaveSize 3 // true [1, 2, 3]
    }
    "Test traversEither on NEL" {
        val fromList = Nel.fromListUnsafe(listOf(1, 2, 3))
        val t = fromList.traverseEither { it.right() }

        val list = (t as Either.Right<List<String>>).value
        list shouldHaveSize 3 // false [1, 2, 3, 1]
    }
a
AFAIK there should be no difference, so that seems to be a bug
would you like to create an issue on Arrow with that same example? Otherwise I’ll do it later today or this week
m
On phone. I'll post next time I'm on computer and post here unless you've already done it. Same result on 0.12.0, and 0.13.1.
🙏 1
👍 1
y
@aballano digging through the source code a bit, it seems the culprit is this function:
Copy code
inline fun <E, A, B> NonEmptyList<A>.traverseEither(f: (A) -> Either<E, B>): Either<E, NonEmptyList<B>> =
  foldRight(f(head).map(::nonEmptyListOf)) { a, acc ->
    when (val res = f(a)) {
      is Right -> acc.map { bs -> nonEmptyListOf(res.value) + bs }
      is Left -> res
    }
  }
`f(head).map(::nonEmptyListOf)`This part right here causes it to first go through the head, then go through every element from first to last (including the head again). I'm not entirely sure what the solution would be here, except maybe doing
tail.foldRight(...){...}
but I'm not sure if that'll produce exactly the same result
a
yes, indeed, I think that’ll be correct
that’s an easy contribution right there if you want to 😉 Some tests should be added as well tho
y
For sure! That should be easy to contribute yeah. I'm on it!
👏 2
@aballano Had a few hiccups with needing to download JDK8 to actually build Arrow, but thankfully that was easy to handle. The pull request has been submitted along with a couple tests. Hopefully it should be all good!
🙌 1
a
thank you! I’ll take a look this week
y
Seems as though the CI build literally just failed lol so I'll take a quick look to see if I caused that somehow... Well this is hilarious, it seems to be a ktlint check that failed because of an extra space. I'll fix it ASAP lol! Edit: Done!