www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Infinite fibonacci sequence, lazy take first 42 values

reply Alain De Vos <devosalain ymail.com> writes:
Following java program creates an infinite fibonacci sequence 
(stream) an takes the first 42 values of it.


import java.util.function.UnaryOperator;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class test3 {
     public static void main(String[] args) {
         int beginvalue[] = new int[] { 0, 1 };
         UnaryOperator<int[]> fibo_seq = a -> new int[] { a[1], 
a[0] + a[1] };
         Stream<int[]> s = Stream.iterate(beginvalue, fibo_seq);
         IntStream fibStream = s.mapToInt(a -> a[1]);
         IntStream limitedstream = fibStream.limit(42);
         limitedstream.forEach(System.out::println);
     }
}

How would this program look converted to dlang ?
Maybe there are multiple solutions ?
Apr 20 2022
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 4/20/22 19:11, Alain De Vos wrote:

 Maybe there are multiple solutions ?
Indeed. :) I have a Range struct here: http://ddili.org/ders/d.en/ranges.html#ix_ranges.infinite%20range Another one with fibers here: http://ddili.org/ders/d.en/fibers.html The same chapter uses Generator: http://ddili.org/ders/d.en/fibers.html#ix_fibers.Generator,%20std.concurrency And Phobos has an example here: https://dlang.org/library/std/range/recurrence.html Ali
Apr 20 2022
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Thursday, 21 April 2022 at 03:41:24 UTC, Ali Çehreli wrote:
 On 4/20/22 19:11, Alain De Vos wrote:

 Maybe there are multiple solutions ?
Indeed. :) I have a Range struct here: http://ddili.org/ders/d.en/ranges.html#ix_ranges.infinite%20range
My favorite is the struct range. Because it is more understandable and personalized. Moreover, you can limit it without using ```take()```. ```d struct FibonacciRange(int l) { long a = 1, b = 1; bool empty() { return a > l; } long front() const { return a; } void popFront() { auto t = a; a = b; b += t; } } enum limit = 16; void main() { import std.stdio; FibonacciRange!limit fibs; fibs.write(": "); import std.algorithm; auto total = fibs.sum; total.writeln; assert(total == 33); } ``` SDB 79
Apr 20 2022
next sibling parent reply Alain De Vos <devosalain ymail.com> writes:
On Thursday, 21 April 2022 at 04:36:13 UTC, Salih Dincer wrote:
 On Thursday, 21 April 2022 at 03:41:24 UTC, Ali Çehreli wrote:
 On 4/20/22 19:11, Alain De Vos wrote:

 Maybe there are multiple solutions ?
Indeed. :) I have a Range struct here: http://ddili.org/ders/d.en/ranges.html#ix_ranges.infinite%20range
My favorite is the struct range. Because it is more understandable and personalized. Moreover, you can limit it without using ```take()```. ```d struct FibonacciRange(int l) { long a = 1, b = 1; bool empty() { return a > l; } long front() const { return a; } void popFront() { auto t = a; a = b; b += t; } } enum limit = 16; void main() { import std.stdio; FibonacciRange!limit fibs; fibs.write(": "); import std.algorithm; auto total = fibs.sum; total.writeln; assert(total == 33); } ``` SDB 79
This example limits the maximum value returned by the fibonacci function. f(n) < limit But it does not allow to return the n-th element of a fibonacci function.
Apr 20 2022
parent Salih Dincer <salihdb hotmail.com> writes:
On Thursday, 21 April 2022 at 05:00:53 UTC, Alain De Vos wrote:
 This example limits the maximum value returned by the fibonacci 
 function. f(n) < limit
 But it does not allow to return the n-th element of a fibonacci 
 function.
You are free to use ```take():``` ```d struct FibonacciRange(long l) { /*...*/ } enum limit = long.max; void main() { FibonacciRange!limit fibs; auto fibFirst90 = fibs.take(90); auto total = fibFirst90.sum; total.writeln; // "7540113804746346428" == fib(92)-1 auto fibInfinite = recurrence!("a[n-1] + a[n-2]")(1L, 1L); assert(fibInfinite.take(92).array[$-1]-1 == total); } ``` SDB 79
Apr 21 2022
prev sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Thursday, 21 April 2022 at 04:36:13 UTC, Salih Dincer wrote:
 My favorite is the struct range.  Because it is more 
 understandable and personalized.  Moreover, you can limit it 
 without using ```take()```.
And it's inherently lazy, so no extra processing/calculation other than what's requested.
Apr 21 2022
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Apr 21, 2022 at 02:11:03AM +0000, Alain De Vos via Digitalmars-d-learn
wrote:
 Following java program creates an infinite fibonacci sequence (stream)
 an takes the first 42 values of it.
[...]
 How would this program look converted to dlang ?
 Maybe there are multiple solutions ?
Code: ------- void main() { import std; writeln(recurrence!((a,n) => a[n-1] + a[n-2])(1, 1).take(42)); } ------- Output: ------- [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296] ------- T -- "Computer Science is no more about computers than astronomy is about telescopes." -- E.W. Dijkstra
Apr 21 2022