Comments (4)
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.
I think an unfold library could be implemented separately.
from collection-strawman.
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.
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)
- TraversableOnce is missing
- Rename `to` to `as` HOT 10
- `seq` operation has been removed HOT 3
- Rewrite `--` operation to `&~` or `diff` HOT 1
- Rewrite `retain` to `filterInPlace`
- ArrayOps does not have a `view` operation anymore HOT 1
- Modularize the migration rewrite rules HOT 1
- Reorganize repositories
- Bring back Iterator.duplicate
- Set.apply / Map.apply don't use SetN / MapN
- We need dev-oriented documentation for M4 HOT 2
- ArrayOps doesn’t provide a sliding operation
- Make scala.Iterable[+A] = scala.collection.immutable.Iterable[A] HOT 8
- Setup collections-compat release infra
- add `()` to `toArray` HOT 1
- resolve status of `scala.collection.immutable.useBaseline` HOT 6
- IterableOnce.scala: def toSet[B >: A]: immutable.Set[B] should be def toSet: immutable.Set[A] HOT 2
- Main criticism HOT 3
- close or transfer all open issues and archive the repository HOT 1
- Delete and archive Gitter room HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from collection-strawman.