Code Monkey home page Code Monkey logo

Comments (7)

johnynek avatar johnynek commented on June 11, 2024 2

this is a good catch, but there is no reason to Eval.later(fa.length) imo. I would either just do val length = fa.length or lazy val length = fa.length.

Let's consider two cases:

  1. fa.length == 0, in which case it is very fast, in in that case we neve call f.
  2. fa.length > 0, in which case we have to call f at least once and evaluate fa.length

So I think eagerly evaluating it is fine. That said, I guess it is possible you would never evaluate the result, in which case we could avoid evaluating fa.length if we use a lazy val.

Finally... there is a deeper issue here...

fa(i) is O(N) for many Seq. We really shouldn't use that. That said... why aren't we just calling this:

def iterateRight[A, B](iterable: Iterable[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = {

from cats.

rudolmi avatar rudolmi commented on June 11, 2024 2

@johnynek Sure, I'll do that.

from cats.

rudolmi avatar rudolmi commented on June 11, 2024 1

So when could this be a part of release? I'm asking because I'm excited about my first contribution and also expecting a business impact so it's good to have some estimate in hand. Is there an appetite for a patch version maybe?

from cats.

danicheg avatar danicheg commented on June 11, 2024 1

If snapshots work for you, you can already use 2.10-7499220-SNAPSHOT.

from cats.

armanbilge avatar armanbilge commented on June 11, 2024

Good catch, that seems reasonable to me!

I used .value on the length Eval for simplicity but it can be just as well flatMapped

I'm wondering if we can do something like this:

  Eval.defer {
    val length = fa.length
    def loop(i: Int): Eval[B] = if (i < length) f(fa(i), Eval.defer(loop(i + 1))) else lb
    loop(0)
  }

from cats.

rudolmi avatar rudolmi commented on June 11, 2024

I would either just do val length = fa.length

this defies laziness which is desired IMO

or lazy val length = fa.length

lazy val synchronizes whole class as opposed to Eval which should synchronize only itself iirc

why aren't we just calling iterateRight

That would be great! I'm all for it.

from cats.

johnynek avatar johnynek commented on June 11, 2024

@rudolmi do you want to send a PR changing Seq to use iterateRight?

from cats.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.