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

@ -1,11 +1,6 @@
// Constant array
var intArray := (1, 2, 3, 4, 5).
// Weak array
var stringArr := Array.allocate(5);
stringArr[0] := "string";
// Generic array
var stringArr := Array new:5.
stringArr[0] := "string".
// Typified array
Array<literal> arr := V<literal>(5).
arr[0] := "a".
arr[1] := "b".
// initialized array
var intArray := new int[](1, 2, 3, 4, 5).

View file

@ -1,5 +1,5 @@
//Create and initialize ArrayList
var myAl := system'collections'ArrayList new; append:"Hello"; append:"World"; append:"!".
var myAl := new system'collections'ArrayList().append:"Hello".append:"World".append:"!";
//Create and initialize List
var myList := system'collections'List new; append:"Hello"; append:"World"; append:"!".
var myList := new system'collections'List().append:"Hello".append:"World".append:"!";

View file

@ -1,4 +1,4 @@
//Create a dictionary
var dict := system'collections'Dictionary new.
dict["Hello"] := "World".
dict["Key"] := "Value".
var dict := new system'collections'Dictionary();
dict["Hello"] := "World";
dict["Key"] := "Value";

View file

@ -0,0 +1,69 @@
USING: assocs deques dlists lists lists.lazy sequences sets ;
! ===fixed-size sequences===
{ 1 2 "foo" 3 } ! array
[ 1 2 3 + * ] ! quotation
"Hello, world!" ! string
B{ 1 2 3 } ! byte array
?{ f t t } ! bit array
! Add an element to a fixed-size sequence
{ 1 2 3 } 4 suffix ! { 1 2 3 4 }
! Append a sequence to a fixed-size sequence
{ 1 2 3 } { 4 5 6 } append ! { 1 2 3 4 5 6 }
! Sequences are sets
{ 1 1 2 3 } { 2 5 7 8 } intersect ! { 2 }
! Strings are just arrays of code points
"Hello" { } like ! { 72 101 108 108 111 }
{ 72 101 108 108 111 } "" like ! "Hello"
! ===resizable sequences===
V{ 1 2 "foo" 3 } ! vector
BV{ 1 2 255 } ! byte vector
SBUF" Hello, world!" ! string buffer
! Add an element to a resizable sequence by mutation
V{ 1 2 3 } 4 suffix! ! V{ 1 2 3 4 }
! Append a sequence to a resizable sequence by mutation
V{ 1 2 3 } { 4 5 6 } append! ! V{ 1 2 3 4 5 6 }
! Sequences are stacks
V{ 1 2 3 } pop ! 3
! ===associative mappings===
{ { "hamburger" 150 } { "soda" 99 } { "fries" 99 } } ! alist
H{ { 1 "a" } { 2 "b" } } ! hash table
! Add a key-value pair to an assoc
3 "c" H{ { 1 "a" } { 2 "b" } } [ set-at ] keep
! H{ { 1 "a" } { 2 "b" } { "c" 3 } }
! ===linked lists===
T{ cons-state f 1 +nil+ } ! literal list syntax
T{ cons-state { car 1 } { cdr +nil+ } } ! literal list syntax
! with car 1 and cdr nil
! One method of manually constructing a list
1 2 3 4 +nil+ cons cons cons cons
1 2 2list ! convenience word for list construction
! T{ cons-state
! { car 1 }
! { cdr T{ cons-state { car 2 } { cdr +nil+ } } }
! }
{ 1 2 3 4 } sequence>list ! make a list from a sequence
0 lfrom ! a lazy list from 0 to infinity
0 [ 2 + ] lfrom-by ! a lazy list of all even numbers >= 0.
DL{ 1 2 3 } ! double linked list / deque
3 DL{ 1 2 } [ push-front ] keep ! DL{ 3 1 2 }
3 DL{ 1 2 } [ push-back ] keep ! DL{ 1 2 3 }
! Factor also comes with disjoint sets, interval maps, heaps,
! boxes, directed graphs, locked I/O buffers, trees, and more!

View file

@ -0,0 +1,11 @@
% create a list
L = [a,b,c,d],
% prepend to the list
L2 = [before_a|L],
% append to the list
append(L2, ['Hello'], L3),
% delete from list
exclude(=(b), L3, L4).

View file

@ -0,0 +1,14 @@
% create an empty dict call 'point'
D1 = point{},
% add a value
D2 = D1.put(x, 20).put(y, 30).put(z, 20),
% update a value
D3 = D2.put([x=25]),
% remove a value
del_dict(z, D3, _, D4),
% access a value randomly
format('x = ~w, y = ~w~n', [D4.x, D4.y]).

View file

@ -1,36 +1,38 @@
pr.=0 /*define a default for all elements for the stemmed array.*/
pr. = /*define a default for all elements for the array*/
pr.1 =2 /*note that this array starts at 1 (one). */
pr.2 =3
pr.3 =5
pr.4 =7
pr.6 =11
pr.6 =13
pr.7 =17
pr.8 =19
pr.9 =23
pr.10=29
pr.11=31
pr.12=37
pr.13=41
pr.14=43
pr.1 = 2 /*note that this array starts at 1 (one). */
pr.2 = 3
pr.3 = 5
pr.4 = 7
pr.5 = 11
pr.6 = 13
pr.7 = 17
pr.8 = 19
pr.9 = 23
pr.10 = 29
pr.11 = 31
pr.12 = 37
pr.13 = 41
pr.14 = 43
pr.15 = 47
y.=0 /*define a default for all years. */
y. = 0 /*define a default for all years (Y) to be zero*/
y.1985 = 6020
y.1986 = 7791
y.1987 = 8244
y.1988 = 10075
x = y.2000 /*X will have a value of zero (0). */
fib.0 = 0 /*arrays may start with zero (0). */
fib.1 = 1
fib.2 = 1
fib.3 = 2
fib.4 = 3
fib.5 = 5
fib.6 = 8
fib.7 =17
x = y.2012 /*the variable X will have a value of zero (0).*/
do n=-5 to 5 /*define an array from -5 ──► 5 */
sawtooth.n=n
end /*n*/ /*eleven elements will be defined. */
fib.0 = 0 /*this stemmed arrays will start with zero (0). */
fib.1 = 1
fib.2 = 1
fib.3 = 2
fib.4 = 3
fib.5 = 5
fib.6 = 8
fib.7 = 17
do n=-5 to 5 /*define a stemmed array from -5 to 5 */
sawtooth.n = n /*the sawtooth array is, well, a sawtooth curve*/
end /*n*/ /*note that eleven elements will be defined. */

View file

@ -1 +1 @@
pr.0=14 /*number of entries in the stemmed array.*/
pr.0= 15 /*number of (data) entries in the stemmed array. */

View file

@ -1,5 +1,6 @@
do j=1 while pr.j\==0
say 'prime' j 'is' pr.j
do j=1 while pr.j\==''
say 'prime' j "is" pr.j
end /*j*/
/*at this point, J=15. */
j=j-1 /*J now has the count of primes stored.*/
/*at this point, J=16 (because of the DO */
/*loop incrementing the index. */
j= j-1 /*J now has the count of primes stored. */

View file

@ -1,10 +1,15 @@
primeList='2 3 5 7 11 13 17 19 23 29 31 37 41 43' /* or ... */
primeList= 2 3 5 7 11 13 17 19 23 29 31 37 41 43
primeList = '2 3 5 7 11 13 17 19 23 29 31 37 41 43' /* or ··· */
primeList = 2 3 5 7 11 13 17 19 23 29 31 37 41 43
/*in this case, the quotes (') can be dropped. */
/*in this case, the quotes (') can be elided.*/
primes=words(primeList)
primes= words(primeList) /*the WORDS BIF counts the number of blank─ */
/*separated words (in this case, prime numbers)*/
/*in the value of the variable "primeList".*/
do j=1 for primes /*can also be coded: do j=1 to primes */
say 'prime' j 'is' word(primeList,j)
do j=1 for primes /*can also be coded as: do j=1 to primes */
say 'prime' j "is" word(primeList, j)
/*this method (using the WORD BIF) isn't */
/*very efficient for very large arrays (those */
/*with many many thousands of elements). */
end /*j*/

View file

@ -1,38 +1,50 @@
pr.=0 /*define a default for all elements for the stemmed array.*/
pr.2 =1
pr.3 =1
pr.5 =1
pr.7 =1
pr.11=1
pr.13=1
pr.17=1
pr.19=1
pr.23=1
pr.29=1
pr.31=1
pr.37=1
pr.41=1
pr.43=1
/*─────────────────────────────────────────────────────────────────────*/
pr. = 0 /*define a default for all elements for the array.*/
pr.2 = 1
pr.3 = 1
pr.5 = 1
pr.7 = 1
pr.11 = 1
pr.13 = 1
pr.17 = 1
pr.19 = 1
pr.23 = 1
pr.29 = 1
pr.31 = 1
pr.37 = 1
pr.41 = 1
pr.43 = 1
pr.47 = 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
primes=0
do j=1 for 10000 /*this method isn't very efficient*/
if pr.j==0 then iterate
primes = primes+1
end /*j*/
say '# of primes in list:' primes
/*─────────────────────────────────────────────────────────────────────*/
do j=1 for 10000 /*this method isn't very efficient. */
if \pr.j then iterate /*Not prime? Then skip this element. */
primes = primes + 1 /*bump the number of primes (counter).*/
end /*j*/
/*note that the 10000 is a small "∞".*/
say '# of primes in list:' primes
/*──────────────────────────────────────────────────────────────────────────────────────*/
#primes=0
do j=1 for 10000 /*this method isn't very efficient*/
if pr.j\==0 then #primes = #primes+1
end /*j*/
do j=1 for 10000 /*this method is not very efficient. */
if pr.j\==0 then #primes = #primes + 1 /*Not zero? Bump the number of primes.*/
end /*j*/ /* [↑] not as idiomatic as 1st program*/
say '# of primes in list:' #primes
/*─────────────────────────────────────────────────────────────────────*/
s=0 /*yet another inefficient method. */
do k=1 for 10000 /*this method isn't very efficient*/
Ps = Ps + (pn.k\==0) /*more obtuse, if ya like that. */
say 'prime' Ps "is:" k /*might as well echo the prime #. */
end /*k*/
say '# of primes in the list:' #primes
/*──────────────────────────────────────────────────────────────────────────────────────*/
Ps=0
do k=1 for 10000 /*and yet another inefficient method. */
if pr.k==0 then iterate /*Not a prime? Then skip this element.*/
Ps = Ps + 1 /*bump the counter for the # of primes.*/
say 'prime' Ps "is:" k /*might as well echo this prime number.*/
end /*k*/
say 'number of primes found in the list is' Ps
say 'The number of primes found in the list is ' Ps
/*──────────────────────────────────────────────────────────────────────────────────────*/
pr.0 = 47 /*hardcode the highest prime in array. */
# = 0
do k=2 to pr.0 /*and much more efficient method. */
if \pr.k then iterate /*Not a prime? Then skip this element.*/
# = # + 1 /*bump the counter for the # of primes.*/
say 'prime' Ps "is:" k /*might as well echo this prime number.*/
end /*k*/
say 'The number of primes found in the list is: ' #