September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1 @@
--- {}

View file

@ -0,0 +1,24 @@
begin
record T ( integer n, m );
reference(T) singleT;
integer numberOfElements;
singleT := T( 0, 0 );
numberOfElements := 3;
begin
reference(T) array tArray ( 1 :: numberOfElements );
% initialise the "right" way %
for i := 1 until numberOfElements do begin
tArray( i ) := T( i, i * 2 );
m(tArray( i )) := m(tArray( i )) + 1;
end for_i ;
write();
for i := 1 until numberOfElements do writeon( i_w := 1, s_w := 0, n(tArray( i )), ", ", m(tArray( i )), "; " );
% initialise the "wrong" way %
for i := 1 until numberOfElements do begin
tArray( i ) := singleT;
m(tArray( i )) := m(tArray( i )) + 1;
end for_i ;
write();
for i := 1 until numberOfElements do writeon( i_w := 1, s_w := 0, n(tArray( i )), ", ", m(tArray( i )), "; " )
end
end.

View file

@ -0,0 +1 @@
VAR a: ARRAY[1..N] OF T

View file

@ -0,0 +1,56 @@
MODULE DistinctObjects EXPORTS Main;
IMPORT IO, Random;
VAR
random := NEW(Random.Default).init();
TYPE
T = RECORD (* value will initialize to 2 unless otherwise specified *)
value: INTEGER := 2;
END;
CONST Size = 3;
VAR
(* initialize records *)
t1 := T { 3 };
t2 := T { 4 };
t3 : T; (* t3's value will be default (2) *)
(* initialize a reference to T with value 100 *)
tr := NEW(REF T, value := 100);
(* initialize an array of records *)
a := ARRAY[1..Size] OF T { t1, t2, t3 };
(* initialize an array of integers *)
b := ARRAY[1..Size] OF INTEGER { -9, 2, 6 };
(* initialize an array of references to a record -- NOT copied! *)
c := ARRAY[1..Size] OF REF T { tr, tr, tr };
BEGIN
(* display the data *)
FOR i := 1 TO Size DO
IO.PutInt(a[i].value); IO.Put(" , ");
IO.PutInt(b[i]); IO.Put(" , ");
IO.PutInt(c[i].value); IO.Put(" ; ");
END;
IO.PutChar('\n');
(* re-initialize a's data to random integers *)
FOR i := 1 TO Size DO a[i].value := random.integer(-10, 10); END;
(* modify "one" element of c *)
c[1].value := 0;
(* display the data *)
FOR i := 1 TO Size DO
IO.PutInt(a[i].value); IO.Put(" , ");
IO.PutInt(b[i]); IO.Put(" , ");
IO.PutInt(c[i].value); IO.Put(" ; ");
END;
IO.PutChar('\n');
END DistinctObjects.

View file

@ -0,0 +1,21 @@
use std::rc::Rc;
use std::cell::RefCell;
fn main() {
let size = 3;
// Clone the given element to fill out the vector.
let mut v: Vec<String> = vec![String::new(); size];
v[0].push('a');
println!("{:?}", v);
// Run a given closure to create each element.
let mut v: Vec<String> = (0..size).map(|i| i.to_string()).collect();
v[0].push('a');
println!("{:?}", v);
// For multiple mutable views of the same thing, use something like Rc and RefCell.
let v: Vec<Rc<RefCell<String>>> = vec![Rc::new(RefCell::new(String::new())); size];
v[0].borrow_mut().push('a');
println!("{:?}", v);
}