This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,8 @@
# creating an empty array and adding values
a = [] # => []
a[0]: 1 # => [1]
a[3]: 2 # => [1, nil, nil, 2]
# creating an array with the constructor
a = Array new # => []

View file

@ -0,0 +1,9 @@
# creating an empty hash
h = <[]> # => <[]>
h["a"]: 1 # => <["a" => 1]>
h["test"]: 2.4 # => <["a" => 1, "test" => 2.4]>
h[3]: "Hello" # => <["a" => 1, "test" => 2.4, 3 => "Hello"]>
# creating a hash with the constructor
h = Hash new # => <[]>

View file

@ -0,0 +1,21 @@
def emptyList = []
assert emptyList.isEmpty() : "These are not the items you're looking for"
assert emptyList.size() == 0 : "Empty list has size 0"
assert ! emptyList : "Empty list evaluates as boolean 'false'"
def initializedList = [ 1, "b", java.awt.Color.BLUE ]
assert initializedList.size() == 3
assert initializedList : "Non-empty list evaluates as boolean 'true'"
assert initializedList[2] == java.awt.Color.BLUE : "referencing a single element (zero-based indexing)"
assert initializedList[-1] == java.awt.Color.BLUE : "referencing a single element (reverse indexing of last element)"
def combinedList = initializedList + [ "more stuff", "even more stuff" ]
assert combinedList.size() == 5
assert combinedList[1..3] == ["b", java.awt.Color.BLUE, "more stuff"] : "referencing a range of elements"
combinedList << "even more stuff"
assert combinedList.size() == 6
assert combinedList[-1..-3] == \
["even more stuff", "even more stuff", "more stuff"] \
: "reverse referencing last 3 elements"
println ([combinedList: combinedList])

View file

@ -0,0 +1,24 @@
def emptyMap = [:]
assert emptyMap.isEmpty() : "These are not the items you're looking for"
assert emptyMap.size() == 0 : "Empty map has size 0"
assert ! emptyMap : "Empty map evaluates as boolean 'false'"
def initializedMap = [ count: 1, initial: "B", eyes: java.awt.Color.BLUE ]
assert initializedMap.size() == 3
assert initializedMap : "Non-empty map evaluates as boolean 'true'"
assert initializedMap["eyes"] == java.awt.Color.BLUE : "referencing a single element (array syntax)"
assert initializedMap.eyes == java.awt.Color.BLUE : "referencing a single element (member syntax)"
assert initializedMap.height == null : \
"references to non-existant keys generally evaluate to null (implementation dependent)"
def combinedMap = initializedMap \
+ [hair: java.awt.Color.BLACK, birthdate: Date.parse("yyyy-MM-dd", "1960-05-17") ]
assert combinedMap.size() == 5
combinedMap["weight"] = 185 // array syntax
combinedMap.lastName = "Smith" // member syntax
combinedMap << [firstName: "Joe"] // entry syntax
assert combinedMap.size() == 8
assert combinedMap.keySet().containsAll(
["lastName", "count", "eyes", "hair", "weight", "initial", "firstName", "birthdate"])
println ([combinedMap: combinedMap])

View file

@ -0,0 +1,16 @@
def emptySet = new HashSet()
assert emptySet.isEmpty() : "These are not the items you're looking for"
assert emptySet.size() == 0 : "Empty set has size 0"
assert ! emptySet : "Empty set evaluates as boolean 'false'"
def initializedSet = new HashSet([ 1, "b", java.awt.Color.BLUE ])
assert initializedSet.size() == 3
assert initializedSet : "Non-empty list evaluates as boolean 'true'"
//assert initializedSet[2] == java.awt.Color.BLUE // SYNTAX ERROR!!! No indexing of set elements!
def combinedSet = initializedSet + new HashSet([ "more stuff", "even more stuff" ])
assert combinedSet.size() == 5
combinedSet << "even more stuff"
assert combinedSet.size() == 5 : "No duplicate elements allowed!"
println ([combinedSet: combinedSet])

View file

@ -0,0 +1,8 @@
# Creation of collections:
s := "abccd" # string, an ordered collection of characters, immutable
c := 'abcd' # cset, an unordered collection of characters, immutable
S := set() # set, an unordered collection of unique values, mutable, contents may be of any type
T := table() # table, an associative array of values accessed via unordered keys, mutable, contents may be of any type
L := [] # list, an ordered collection of values indexed by position 1..n or as stack/queue, mutable, contents may be of any type
record constructorname(field1,field2,fieldetc) # record, a collection of values stored in named fields, mutable, contents may be of any type (declare outside procedures)
R := constructorname() # record (creation)

View file

@ -0,0 +1,6 @@
s ||:= "xyz" # concatenation
c ++:= 'xyz' # union
insert(S,"abc") # insert
T["abc"] := "xyz" # insert create/overwrite
put(L,1) # put (extend), also push
R.field1 := "xyz" # overwrite

View file

@ -0,0 +1,4 @@
S := S ++ S2 # union of two sets or two csets
S ++:= S2 # augmented assignment
L := L ||| L2 # list concatenation
L |||:= L2 # augmented assignment

View file

@ -0,0 +1,87 @@
c =: 0 10 20 30 40 NB. A collection
c, 50 NB. Append 50 to the collection
0 10 20 30 40 50
_20 _10 , c NB. Prepend _20 _10 to the collection
_20 _10 0 10 20 30 40
,~ c NB. Self-append
0 10 20 30 40 0 10 20 30 40
,:~ c NB. Duplicate
0 10 20 30 40
0 10 20 30 40
30 e. c NB. Is 30 in the collection?
1
30 i.~c NB. Where?
3
30 80 e. c NB. Don't change anything to test multiple values -- collections are native.
1 0
2 1 4 2 { c NB. From the collection, give me items two, one, four, and two again.
20 10 40 20
|.c NB. Reverse the collection
40 30 20 10 0
1+c NB. Increment the collection
1 11 21 31 41
c%10 NB. Decimate the collection (divide by 10)
0 1 2 3 4
{. c NB. Give me the first item
0
{: c NB. And the last
40
3{.c NB. Give me the first 3 items
0 10 20
3}.c NB. Throw away the first 3 items
30 40
_3{.c NB. Give me the last 3 items
20 30 40
_3}.c NB. (Guess)
0 10
keys_map_ =: 'one';'two';'three'
vals_map_ =: 'alpha';'beta';'gamma'
lookup_map_ =: a:& $: : (dyad def ' (keys i. y) { vals,x')&boxopen
exists_map_ =: verb def 'y e. keys'&boxopen
exists_map_ 'bad key'
0
exists_map_ 'two';'bad key'
1 0
lookup_map_ 'one'
+-----+
|alpha|
+-----+
lookup_map_ 'three';'one';'two';'one'
+-----+-----+----+-----+
|gamma|alpha|beta|alpha|
+-----+-----+----+-----+
lookup_map_ 'bad key'
++
||
++
'some other default' lookup_map_ 'bad key'
+------------------+
|some other default|
+------------------+
'some other default' lookup_map_ 'two';'bad key'
+----+------------------+
|beta|some other default|
+----+------------------+
+/ c NB. Sum of collection
100
*/ c NB. Product of collection
0
i.5 NB. Generate the first 5 nonnegative integers
0 1 2 3 4
10*i.5 NB. Looks familiar
0 10 20 30 40
c = 10*i.5 NB. Test each for equality
1 1 1 1 1
c -: 10 i.5 NB. Test for identicality
1

View file

@ -0,0 +1,4 @@
+ vector : ARRAY[INTEGER];
vector := ARRAY[INTEGER].create_with_capacity 32 lower 0;
vector.add_last 1;
vector.add_last 2;

View file

@ -0,0 +1,4 @@
+ set : HASHED_SET[INTEGER];
set := HASHED_SET[INTEGER].create;
set.add 1;
set.add 2;

View file

@ -0,0 +1,4 @@
+ list : LINKED_LIST[INTEGER];
list := LINKED_LIST[INTEGER].create;
list.add_last 1;
list.add_last 2;

View file

@ -0,0 +1,4 @@
+ dict : HASHED_DICTIONARY[INTEGER/*value*/, STRING_CONSTANT/*key*/];
dict := HASHED_DICTIONARY[INTEGER, STRING_CONSTANT].create;
dict.put 1 to "one";
dict.put 2 to "two";

View file

@ -0,0 +1,13 @@
Lst = {3, 4, 5, 6}
->{3, 4, 5, 6}
PrependTo[ Lst, 2]
->{2, 3, 4, 5, 6}
PrependTo[ Lst, 1]
->{1, 2, 3, 4, 5, 6}
Lst
->{1, 2, 3, 4, 5, 6}
Insert[ Lst, X, 4]
->{1, 2, 3, X, 4, 5, 6}