678b⟨test Stream677a⟩+≡ (676)⊲678a679a⊳ testStreamElements1(): () == { s: StreamI := stream 4; g:GeneratorI := generator s; n:GeneratorI := elements s; l1: ListI := [i for i in g for k in 0..9]; l2: ListI := [i for i in n for k in 0..9]; l: ListI := [4 for k in 0..9]; assertEquals(ListI, l, l1); assertEquals(ListI, [4], l2); } UsesGenerator617andI47.
679a⟨test Stream677a⟩+≡ (676)⊲678b679b⊳ testStreamElements2(): () == { t: StreamI := stream generator [2,3,5,7,11]; g3:GeneratorI := elements(t, 2); l3: ListI := [i for i in g3 for k in 0..9]; assertEquals(ListI, l3, [5,7,11]); g4:GeneratorI := elements(t, 10); l4: ListI := [i for i in g4 for k in 0..9]; assertEquals(ListI, l4, [11]); } UsesGenerator617andI47.
679b⟨test Stream677a⟩+≡ (676)⊲679a680a⊳ testStream5(): () == { l: ListString := ["Hello", " ", "World", "!"]; s: StreamString := stream generator l; assertEquals(I, 0, #s); assertEquals(String, "World", s.2); assertEquals(I, 3, #s); d: PrimitiveArrayString := data s; u:String := ""; for i in 0..prev #s repeat u := u + d.i; assertEquals(String, "Hello World", u); v:String := ""; for i in 0..9 for x in s repeat v := v + x; assertEquals(String, "Hello World!!!!!!!", v); assertEquals(I, 4, #s); } UsesI47andString65.
680a⟨test Stream677a⟩+≡ (676)⊲679b680b⊳ testStream6(): () == { import fromInteger; l: ListInteger := [2,3,5,7,11,0]; s: StreamInteger := stream generator l; a := constant? s; x := s.3; assertFalse(constant? s); y := s.5; assertFalse(constant? s); z := s.6; assertTrue(constant? s); } UsesInteger66.
680b⟨test Stream677a⟩+≡ (676)⊲680a681a⊳ testStream7(): () == { import fromString; s: StreamString := stream "I am constant"; assertTrue(constant? s); } UsesI47andString65.
681a⟨test Stream677a⟩+≡ (676)⊲680b681b⊳ testStream8(): () == { import fromInteger, ListInteger; s: StreamInteger := stream generator [1,2,3,4,5,6]; a: ListInteger := empty; b: ListInteger := empty; for i:I in 0.. for x in elements s repeat { a := cons(x, a); b := cons(s.(i+i), b); } assertEquals(ListInteger, [6,5,4,3,2,1], a); assertEquals(ListInteger, [6,6,6,5,3,1], b); } UsesI47andInteger66.
681b⟨test Stream677a⟩+≡ (676)⊲681a682a⊳ testStream9(): () == { import fromInteger; fib: StreamInteger := new(); g:GeneratorInteger := generate { yield 1; yield 1; for n:I in 0.. repeat yield fib.n + fib.(next n); } set!(fib, g); f: ListInteger := [x for x in fib while x < 100]; assertEquals(ListInteger, [1,1,2,3,5,8,13,21,34,55,89], f); } UsesGenerator617, I47, andInteger66.
682a⟨test Stream677a⟩+≡ (676)⊲681b682b⊳ testStream9b(): () == { import fromInteger; h(n:I, s: StreamInteger):Integer == { n < 2 => 1; s(n-1) + s(n-2); } fib: StreamInteger := stream h; f: ListInteger := [x for x in fib while x < 100]; assertEquals(ListInteger, [1,1,2,3,5,8,13,21,34,55,89], f); } UsesI47andInteger66.
682b⟨test Stream677a⟩+≡ (676)⊲682a683a⊳ testStream10(): () == { T ==>Integer; S ==> Stream T; import from T, S; (x: S) + (y: S): S == stream(a+b for a in x for b in y); (x: S) * (y: S): S == { local times(n:I): T == { t: T := 0; for k in 0..n | not zero?(xk := x.k) repeat { t := t + xk * y.(n-k); } t; } stream(times n for n:I in 0..); } s: Stream T := new(); 1: S := stream(generator [1, 0]); x: S := stream(generator [0, 1, 0]); set!(s, generator(1 + x * s * s)); l1: ListInteger := [1,1,2,5,14,42]; l2: ListInteger := [t for i in l1 for t in s]; assertEquals(ListInteger, l1, l2); } UsesI47andInteger66.
683a⟨test Stream677a⟩+≡ (676)⊲682b683b⊳ testStreamMap1(): () == { T ==>Integer; S ==> Stream T; import from T, S; f(n:I, x: S): T == {zero? n => 0; x(n-1) + 1} one: S := stream f; l1: List T := [0,1,2,3,4,5,6,7,8,9]; lone: List T := [t for i in l1 for t in one]; assertEquals(List T, l1, lone);
double(t: T): T == 2*t; two: S := map(double)(one); l2: List T := map(double)(l1); ltwo: List T := [t for i in l2 for t in two]; assertEquals(List T, l2, ltwo); } UsesI47andInteger66.
683b⟨test Stream677a⟩+≡ (676)⊲683a684⊳ testStreamMap2(): () == { T ==>Integer; S ==> Stream T; import from T, S; l1: List T := [0,1,2,3]; one: S := stream generator l1; lone: List T := [t for i:I in 0..5 for t in one]; assertEquals(List T, [0,1,2,3,3,3], lone);
double(t: T): T == 2*t; two: S := map(double)(one); ltwo: List T := [t for i:I in 0..5 for t in two]; assertEquals(List T, [0,2,4,6,6,6], ltwo); } UsesI47andInteger66.
684⟨test Stream677a⟩+≡ (676)⊲683b testStreamSet!1(): () == { T ==>Integer; S ==> Stream T; import from T, S; r: List T := [4,3,5,2,6,1,1,1,1,1]; l: List T := [4,3,5,2,6,1]; s: S := stream generator l; s.3 := -1; u: List T := [z for t in r for z in s]; assertEquals(List T, [4,3,5,-1,6,1,1,1,1,1], u); s.5 := -2; u: List T := [z for t in r for z in s]; assertEquals(List T, [4,3,5,-1,6,-2,1,1,1,1], u); s.6 := -3; u: List T := [z for t in r for z in s]; assertEquals(List T, [4,3,5,-1,6,-2,-3,1,1,1], u); s.8 := -4; u: List T := [z for t in r for z in s]; assertEquals(List T, [4,3,5,-1,6,-2,-3,1,-4,1], u);
a: S := stream 0; a.2 := 3; v: List T := [z for t:I in 0..3 for z in a]; assertEquals(List T, [0,0,3,0], v); } UsesI47andInteger66.