Giter Site home page Giter Site logo

Comments (4)

ivanopagano avatar ivanopagano commented on June 15, 2024 2

I like the unfolding idea, for its declarative approach.
So every concrete collection should provide an implementation of something similar to the following? (code simplified, no type parameter bounds were hurt for this example)

trait Unfolder[COLL[_]] {
  def unfold[A, B](seed: A)(f: (A) ⇒ Option[(B, A)]): COLL[B]
}

Is there any "traditional" collection type which would not fit with this approach?

from collection-strawman.

julienrf avatar julienrf commented on June 15, 2024 2

I think an unfold library could be implemented separately.

from collection-strawman.

odersky avatar odersky commented on June 15, 2024

I usually use a ListBuffer as an accumulator, like this:

def fibonacci(n: Int): List[Int] = {
  def loop(remaining: Int, prev: Int, prevPrev: Int, buf: ListBuffer[Int]): ListBuffer[Int] =
    if (remaining > 0) {
      val value = prev + prevPrev
      loop(remaining - 1, value, prev, buf += value)
    } else buf.toList
  loop(n - 2, 1, 0, ListBuffer(0, 1))
}

It's "purely functional" code, but with a mutable type used linearly.

from collection-strawman.

julienrf avatar julienrf commented on June 15, 2024

Also, just to get a comparison basis, here is what an unfold based version could look like:

def fibonacci(n: Int): List[Int] =
  List(0, 1) ++ List.unfold((n - 2, 0, 1)) {
    case (0, _, _) => None
    case (n, prevPrev, prev) =>
      val value = prevPrev + prev
      Some(value -> (n - 1, prev, value))
  }

The nice thing is that we don’t have to manage the loop and the accumulator by ourselves.

from collection-strawman.

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.