new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
3
Task/Collections/0DESCRIPTION
Normal file
3
Task/Collections/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{{clarified-review}}Collections are abstractions to represent sets of values. In statically-typed languages, the values are typically of a common data type.
|
||||
|
||||
Create a collection, and add a few values to it.
|
||||
2
Task/Collections/1META.yaml
Normal file
2
Task/Collections/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Basic language learning
|
||||
1
Task/Collections/AWK/collections-2.awk
Normal file
1
Task/Collections/AWK/collections-2.awk
Normal file
|
|
@ -0,0 +1 @@
|
|||
split("one two three",a)
|
||||
1
Task/Collections/AWK/collections-3.awk
Normal file
1
Task/Collections/AWK/collections-3.awk
Normal file
|
|
@ -0,0 +1 @@
|
|||
print a[0]
|
||||
1
Task/Collections/AWK/collections-4.awk
Normal file
1
Task/Collections/AWK/collections-4.awk
Normal file
|
|
@ -0,0 +1 @@
|
|||
for(i in a) print i":"a[i]
|
||||
1
Task/Collections/AWK/collections.awk
Normal file
1
Task/Collections/AWK/collections.awk
Normal file
|
|
@ -0,0 +1 @@
|
|||
a[0]="hello"
|
||||
12
Task/Collections/C/collections-2.c
Normal file
12
Task/Collections/C/collections-2.c
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
int* ar; /* Collection<Integer> ar; */
|
||||
int arSize;
|
||||
arSize = (rand() % 6) + 1;
|
||||
ar = calloc(arSize, sizeof(int) ); /* ar = new ArrayList<Integer>(arSize); */
|
||||
ar[0] = 1; /* ar.set(0, 1); */
|
||||
|
||||
int* p; /* Iterator<Integer> p; Integer pValue; */
|
||||
for (p=ar; /* p=ar.itereator(); for( pValue=p.next(); */
|
||||
p<(ar+arSize); /* p.hasNext(); */
|
||||
p++) { /* pValue=p.next() ) { */
|
||||
printf("%d\n",*p); /* System.out.println(pValue); */
|
||||
} /* } */
|
||||
11
Task/Collections/C/collections.c
Normal file
11
Task/Collections/C/collections.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#define cSize( a ) ( sizeof(a)/sizeof(a[0]) ) /* a.size() */
|
||||
int ar[10]; /* Collection<Integer> ar = new ArrayList<Integer>(10); */
|
||||
ar[0] = 1; /* ar.set(0, 1); */
|
||||
ar[1] = 2;
|
||||
|
||||
int* p; /* Iterator<Integer> p; Integer pValue; */
|
||||
for (p=ar; /* for( p = ar.itereator(), pValue=p.next(); */
|
||||
p<(ar+cSize(ar)); /* p.hasNext(); */
|
||||
p++) { /* pValue=p.next() ) { */
|
||||
printf("%d\n",*p); /* System.out.println(pValue); */
|
||||
} /* } */
|
||||
3
Task/Collections/Clojure/collections-2.clj
Normal file
3
Task/Collections/Clojure/collections-2.clj
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
'(1 4 7) ; a linked list
|
||||
(list 1 4 7)
|
||||
(cons 1 (cons 4 '(7)))
|
||||
3
Task/Collections/Clojure/collections-3.clj
Normal file
3
Task/Collections/Clojure/collections-3.clj
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
['a 4 11] ; somewhere between array and list
|
||||
(vector 'a 4 11)
|
||||
(cons ['a 4] 11) ; vectors add at the *end*
|
||||
3
Task/Collections/Clojure/collections-4.clj
Normal file
3
Task/Collections/Clojure/collections-4.clj
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#{:pig :dog :bear}
|
||||
(assoc #{:pig :bear} :dog)
|
||||
(set [:pig :bear :dog])
|
||||
4
Task/Collections/Clojure/collections.clj
Normal file
4
Task/Collections/Clojure/collections.clj
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{1 "a", "Q" 10} ; commas are treated as whitespace
|
||||
(hash-map 1 "a" "Q" 10) ; equivalent to the above
|
||||
(let [my-map {1 "a"}]
|
||||
(assoc my-map "Q" 10)) ; "adding" an element
|
||||
7
Task/Collections/Forth/collections-2.fth
Normal file
7
Task/Collections/Forth/collections-2.fth
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
include ffl/dcl.fs
|
||||
|
||||
dcl-create dl \ create a double linked list
|
||||
|
||||
3 dl dcl-append
|
||||
1 dl dcl-prepend
|
||||
2 1 dl dcl-insert \ dl[0] = 1 dl[1] = 2 dl[2] = 3
|
||||
7
Task/Collections/Forth/collections-3.fth
Normal file
7
Task/Collections/Forth/collections-3.fth
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
include ffl/hct.fs
|
||||
|
||||
10 hct-create ht \ create a hashtable with initial size 10
|
||||
|
||||
1 s" one" ht hct-insert \ ht["one"] = 1
|
||||
2 s" two" ht hct-insert \ ht["two"] = 2
|
||||
3 s" three" ht hct-insert \ ht["three"] = 3
|
||||
7
Task/Collections/Forth/collections.fth
Normal file
7
Task/Collections/Forth/collections.fth
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
include ffl/car.fs
|
||||
|
||||
10 car-create ar \ create a dynamic array with initial size 10
|
||||
|
||||
2 0 ar car-set \ ar[0] = 2
|
||||
3 1 ar car-set \ ar[1] = 3
|
||||
1 0 ar car-insert \ ar[0] = 1 ar[1] = 2 ar[2] = 3
|
||||
10
Task/Collections/Go/collections.go
Normal file
10
Task/Collections/Go/collections.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var a []interface{}
|
||||
a = append(a, 3)
|
||||
a = append(a, "apples", "oranges")
|
||||
fmt.Println(a)
|
||||
}
|
||||
1
Task/Collections/Haskell/collections-2.hs
Normal file
1
Task/Collections/Haskell/collections-2.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
1 : [2, 3, 4]
|
||||
1
Task/Collections/Haskell/collections-3.hs
Normal file
1
Task/Collections/Haskell/collections-3.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
[1, 2] ++ [3, 4]
|
||||
1
Task/Collections/Haskell/collections-4.hs
Normal file
1
Task/Collections/Haskell/collections-4.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
concat [[1, 2], [3, 4], [5, 6, 7]]
|
||||
1
Task/Collections/Haskell/collections.hs
Normal file
1
Task/Collections/Haskell/collections.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
[1, 2, 3, 4, 5]
|
||||
4
Task/Collections/Java/collections-2.java
Normal file
4
Task/Collections/Java/collections-2.java
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
//loop through myarrlist to sum each entry
|
||||
for ( i = 0; i < myarrlist.size(); i++) {
|
||||
sum += myarrlist.get(i);
|
||||
}
|
||||
3
Task/Collections/Java/collections-3.java
Normal file
3
Task/Collections/Java/collections-3.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
for(int i : myarrlist) {
|
||||
sum += i;
|
||||
}
|
||||
5
Task/Collections/Java/collections-4.java
Normal file
5
Task/Collections/Java/collections-4.java
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
//remove the last entry in the ArrayList
|
||||
myarrlist.remove(myarrlist.size()-1)
|
||||
|
||||
//clear the ArrayList
|
||||
myarrlist.clear();
|
||||
14
Task/Collections/Java/collections.java
Normal file
14
Task/Collections/Java/collections.java
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
List arrayList = new ArrayList();
|
||||
arrayList.add(new Integer(0));
|
||||
// alternative with primitive autoboxed to an Integer object automatically
|
||||
arrayList.add(0);
|
||||
|
||||
//other features of ArrayList
|
||||
//define the type in the arraylist, you can substitute a proprietary class in the "<>"
|
||||
List<Integer> myarrlist = new ArrayList<Integer>();
|
||||
|
||||
//add several values to the arraylist to be summed later
|
||||
int sum;
|
||||
for(int i = 0; i < 10; i++) {
|
||||
myarrlist.add(i);
|
||||
}
|
||||
5
Task/Collections/JavaScript/collections-2.js
Normal file
5
Task/Collections/JavaScript/collections-2.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var map = {};
|
||||
map['foo'] = 'xyz'; //equivalent to: map.foo = 'xyz';
|
||||
map['bar'] = new MyClass; //equivalent to: map.bar = new MyClass;
|
||||
map['1x; ~~:-b'] = 'text'; //no equivalent
|
||||
alert( map['1x; ~~:-b'] );
|
||||
5
Task/Collections/JavaScript/collections.js
Normal file
5
Task/Collections/JavaScript/collections.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var array = [];
|
||||
array.push('abc');
|
||||
array.push(123);
|
||||
array.push(new MyClass);
|
||||
alert( array[2] );
|
||||
8
Task/Collections/Lua/collections.lua
Normal file
8
Task/Collections/Lua/collections.lua
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
collection = {0, '1'}
|
||||
print(collection[1]) -- prints 0
|
||||
|
||||
collection = {["foo"] = 0, ["bar"] = '1'} -- a collection of key/value pairs
|
||||
print(collection["foo"]) -- prints 0
|
||||
print(collection.foo) -- syntactic sugar, also prints 0
|
||||
|
||||
collection = {0, '1', ["foo"] = 0, ["bar"] = '1'}
|
||||
10
Task/Collections/PHP/collections.php
Normal file
10
Task/Collections/PHP/collections.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
$a = array();
|
||||
# add elements "at the end"
|
||||
array_push($a, 55, 10, 20);
|
||||
print_r($a);
|
||||
# using an explicit key
|
||||
$a['one'] = 1;
|
||||
$a['two'] = 2;
|
||||
print_r($a);
|
||||
?>
|
||||
18
Task/Collections/Perl/collections.pl
Normal file
18
Task/Collections/Perl/collections.pl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
use strict;
|
||||
my @c = (); # create an empty "array" collection
|
||||
|
||||
# fill it
|
||||
push @c, 10, 11, 12;
|
||||
push @c, 65;
|
||||
# print it
|
||||
print join(" ",@c) . "\n";
|
||||
|
||||
# create an empty hash
|
||||
my %h = ();
|
||||
# add some pair
|
||||
$h{'one'} = 1;
|
||||
$h{'two'} = 2;
|
||||
# print it
|
||||
foreach my $i ( keys %h ) {
|
||||
print $i . " -> " . $h{$i} . "\n";
|
||||
}
|
||||
14
Task/Collections/PicoLisp/collections.l
Normal file
14
Task/Collections/PicoLisp/collections.l
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
: (setq Lst (3 4 5 6))
|
||||
-> (3 4 5 6)
|
||||
|
||||
: (push 'Lst 2)
|
||||
-> 2
|
||||
|
||||
: (push 'Lst 1)
|
||||
-> 1
|
||||
|
||||
: Lst
|
||||
-> (1 2 3 4 5 6)
|
||||
|
||||
: (insert 4 Lst 'X)
|
||||
-> (1 2 3 X 4 5 6)
|
||||
20
Task/Collections/Python/collections.py
Normal file
20
Task/Collections/Python/collections.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
collection = [0, '1'] # Lists are mutable (editable) and can be sorted in place
|
||||
x = collection[0] # accessing an item (which happens to be a numeric 0 (zero)
|
||||
collection.append(2) # adding something to the end of the list
|
||||
collection.insert(0, '-1') # inserting a value into the beginning
|
||||
y = collection[0] # now returns a string of "-1"
|
||||
collection.extend([2,'3']) # same as [collection.append(i) for i in [2,'3']] ... but faster
|
||||
collection += [2,'3'] # same as previous line
|
||||
collection[2:6] # a "slice" (collection of the list elements from the third up to but not including the sixth)
|
||||
len(collection) # get the length of (number of elements in) the collection
|
||||
collection = (0, 1) # Tuples are immutable (not editable)
|
||||
collection[:] # ... slices work on these too; and this is equivalent to collection[0:len(collection)]
|
||||
collection[-4:-1] # negative slices count from the end of the string
|
||||
collection[::2] # slices can also specify a stride --- this returns all even elements of the collection
|
||||
collection="some string" # strings are treated as sequences of characters
|
||||
x = collection[::-1] # slice with negative step returns reversed sequence (string in this case).
|
||||
collection[::2] == "some string"[::2] # True, literal objects don't need to be bound to name/variable to access slices or object methods
|
||||
collection.__getitem__(slice(0,len(collection),2)) # same as previous expressions.
|
||||
collection = {0: "zero", 1: "one"} # Dictionaries (Hash)
|
||||
collection['zero'] = 2 # Dictionary members accessed using same syntax as list/array indexes.
|
||||
collection = set([0, '1']) # sets (Hash)
|
||||
2
Task/Collections/R/collections-2.r
Normal file
2
Task/Collections/R/collections-2.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
integer(5)
|
||||
c(1L, -2L, 99L);
|
||||
2
Task/Collections/R/collections-3.r
Normal file
2
Task/Collections/R/collections-3.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
logical(5)
|
||||
c(TRUE, FALSE)
|
||||
2
Task/Collections/R/collections-4.r
Normal file
2
Task/Collections/R/collections-4.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
character(5)
|
||||
c("abc", "defg", "")
|
||||
3
Task/Collections/R/collections-5.r
Normal file
3
Task/Collections/R/collections-5.r
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
matrix(1:12, nrow=3)
|
||||
|
||||
array(1:24, dim=c(2,3,4)) #output not shown
|
||||
1
Task/Collections/R/collections-6.r
Normal file
1
Task/Collections/R/collections-6.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
list(a=123, b="abc", TRUE, 1:5, c=list(d=runif(5), e=5+6))
|
||||
13
Task/Collections/R/collections-7.r
Normal file
13
Task/Collections/R/collections-7.r
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
$a
|
||||
[1] 123
|
||||
$b
|
||||
[1] "abc"
|
||||
[[3]]
|
||||
[1] TRUE
|
||||
[[4]]
|
||||
[1] 1 2 3 4 5
|
||||
$c
|
||||
$c$d
|
||||
[1] 0.6013157 0.5011909 0.7106448 0.3882265 0.1274939
|
||||
$c$e
|
||||
[1] 11
|
||||
1
Task/Collections/R/collections-8.r
Normal file
1
Task/Collections/R/collections-8.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
data.frame(name=c("Alice", "Bob", "Carol"), age=c(23, 35, 17))
|
||||
3
Task/Collections/R/collections.r
Normal file
3
Task/Collections/R/collections.r
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
numeric(5)
|
||||
1:10
|
||||
c(1, 3, 6, 10, 7 + 8, sqrt(441))
|
||||
1
Task/Collections/REXX/collections-2.rexx
Normal file
1
Task/Collections/REXX/collections-2.rexx
Normal file
|
|
@ -0,0 +1 @@
|
|||
pr.0=14 /*number of entries in the stemmed array.*/
|
||||
5
Task/Collections/REXX/collections-3.rexx
Normal file
5
Task/Collections/REXX/collections-3.rexx
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
do j=1 while pr.j\==0
|
||||
say 'prime' j 'is' pr.j
|
||||
end /*j*/
|
||||
/*at this point, J=15. */
|
||||
j=j-1 /*J now has the count of primes stored.*/
|
||||
10
Task/Collections/REXX/collections-4.rexx
Normal file
10
Task/Collections/REXX/collections-4.rexx
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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. */
|
||||
|
||||
primes=words(primeList)
|
||||
|
||||
do j=1 for primes /*can also be coded: do j=1 to primes */
|
||||
say 'prime' j 'is' word(primeList,j)
|
||||
end /*j*/
|
||||
31
Task/Collections/REXX/collections-5.rexx
Normal file
31
Task/Collections/REXX/collections-5.rexx
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
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
|
||||
|
||||
primes=0
|
||||
do j=1 for 10000 /*this method isn't very efficent*/
|
||||
if pr.j==0 then iterate
|
||||
primes=primes+1
|
||||
end /*j*/
|
||||
|
||||
say '# of primes in list:' primes
|
||||
|
||||
Ps=0 /*another inefficient method. */
|
||||
do k=1 for 10000 /*this method isn't very efficent*/
|
||||
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 'number of primes found in the list is' Ps
|
||||
36
Task/Collections/REXX/collections.rexx
Normal file
36
Task/Collections/REXX/collections.rexx
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
pr.=0 /*define a default for all elements for the stemmed 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
|
||||
|
||||
y.=0 /*define a default for all years. */
|
||||
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
|
||||
|
||||
do n=-5 to 5 /*define an array from -5 ââ⺠5 */
|
||||
sawtooth.n=n
|
||||
end /*n*/ /*eleven elements will be defined. */
|
||||
9
Task/Collections/Ruby/collections-2.rb
Normal file
9
Task/Collections/Ruby/collections-2.rb
Normal 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 # => {}
|
||||
8
Task/Collections/Ruby/collections.rb
Normal file
8
Task/Collections/Ruby/collections.rb
Normal 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 # => []
|
||||
37
Task/Collections/Scala/collections.scala
Normal file
37
Task/Collections/Scala/collections.scala
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
scala> // Immutable collections do not change the original object
|
||||
scala> // A List
|
||||
scala> val list = Nil // Empty List
|
||||
list: scala.collection.immutable.Nil.type = List()
|
||||
scala> val list2 = List(1, 2) // List with two elements
|
||||
list2: List[Int] = List(1, 2)
|
||||
scala> val list3 = 3 :: list2 // prepend 3 to list2, using a special operator
|
||||
list3: List[Int] = List(3, 1, 2)
|
||||
scala> // A Set
|
||||
scala> val set = Set.empty[Char] // Empty Set of Char type
|
||||
set: scala.collection.immutable.Set[Char] = Set()
|
||||
scala> val set1 = set + 'c' // add an element
|
||||
set1: scala.collection.immutable.Set[Char] = Set(c)
|
||||
scala> // A Map
|
||||
scala> val map = Map(1 -> "A")
|
||||
map: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> A)
|
||||
scala> val map1 = map + (2 -> "Map")
|
||||
map1: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> A, 2 -> Map)
|
||||
scala> // Mutable collections can be modified
|
||||
scala> val queue = new scala.collection.mutable.Queue[Int]
|
||||
queue: scala.collection.mutable.Queue[Int] = Queue()
|
||||
scala> queue += 4
|
||||
res0: queue.type = Queue(4)
|
||||
scala> queue += 5
|
||||
res1: queue.type = Queue(4, 5)
|
||||
scala> queue
|
||||
res2: scala.collection.mutable.Queue[Int] = Queue(4, 5)
|
||||
scala> val set = scala.collection.mutable.Set('a')
|
||||
set: scala.collection.mutable.Set[Char] = Set(a)
|
||||
scala> set += 'b'
|
||||
res3: set.type = Set(b, a)
|
||||
scala> val map = scala.collection.mutable.Map(1 -> "one")
|
||||
map: scala.collection.mutable.Map[Int,java.lang.String] = Map(1 -> one)
|
||||
scala> map += (2 -> "two")
|
||||
res4: map.type = Map(2 -> two, 1 -> one)
|
||||
scala> map
|
||||
res5: scala.collection.mutable.Map[Int,java.lang.String] = Map(2 -> two, 1 -> one)
|
||||
4
Task/Collections/Scheme/collections-2.ss
Normal file
4
Task/Collections/Scheme/collections-2.ss
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(display (list 1 2 3))
|
||||
(newline)
|
||||
(display (list))
|
||||
(newline)
|
||||
2
Task/Collections/Scheme/collections-3.ss
Normal file
2
Task/Collections/Scheme/collections-3.ss
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(1 2 3)
|
||||
()
|
||||
1
Task/Collections/Scheme/collections-4.ss
Normal file
1
Task/Collections/Scheme/collections-4.ss
Normal file
|
|
@ -0,0 +1 @@
|
|||
(cons obj lst)
|
||||
2
Task/Collections/Scheme/collections-5.ss
Normal file
2
Task/Collections/Scheme/collections-5.ss
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(display (cons 0 (list 1 2 3)))
|
||||
(newline)
|
||||
1
Task/Collections/Scheme/collections-6.ss
Normal file
1
Task/Collections/Scheme/collections-6.ss
Normal file
|
|
@ -0,0 +1 @@
|
|||
(0 1 2 3)
|
||||
1
Task/Collections/Scheme/collections-7.ss
Normal file
1
Task/Collections/Scheme/collections-7.ss
Normal file
|
|
@ -0,0 +1 @@
|
|||
(append lst ...)
|
||||
2
Task/Collections/Scheme/collections-8.ss
Normal file
2
Task/Collections/Scheme/collections-8.ss
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(display (append (list 1 2 3) (list 4 5 6)))
|
||||
(newline)
|
||||
1
Task/Collections/Scheme/collections-9.ss
Normal file
1
Task/Collections/Scheme/collections-9.ss
Normal file
|
|
@ -0,0 +1 @@
|
|||
(1 2 3 4 5 6)
|
||||
1
Task/Collections/Scheme/collections.ss
Normal file
1
Task/Collections/Scheme/collections.ss
Normal file
|
|
@ -0,0 +1 @@
|
|||
(list obj ...)
|
||||
32
Task/Collections/Smalltalk/collections.st
Normal file
32
Task/Collections/Smalltalk/collections.st
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
|anOrdered aBag aSet aSorted aSorted2 aDictionary|
|
||||
|
||||
anOrdered := OrderedCollection new.
|
||||
anOrdered add: 1; add: 5; add: 3.
|
||||
anOrdered printNl.
|
||||
|
||||
aBag := Bag new.
|
||||
aBag add: 5; add: 5; add: 5; add: 6.
|
||||
aBag printNl.
|
||||
|
||||
aSet := Set new.
|
||||
aSet add: 10; add: 5; add: 5; add: 6; add: 10.
|
||||
aSet printNl.
|
||||
|
||||
aSorted := SortedCollection new.
|
||||
aSorted add: 10; add: 9; add: 8; add: 5.
|
||||
aSorted printNl.
|
||||
|
||||
"another sorted with custom comparator: let's sort
|
||||
the other collections according to their size (number of
|
||||
elements)"
|
||||
aSorted2 := SortedCollection sortBlock: [ :a :b |
|
||||
(a size) < (b size) ].
|
||||
aSorted2 add: anOrdered; add: aBag; add: aSet; add: aSorted.
|
||||
aSorted2 printNl.
|
||||
|
||||
aDictionary := Dictionary new.
|
||||
aDictionary at: 'OrderedCollection' put: anOrdered;
|
||||
at: 'Bag' put: aBag;
|
||||
at: 'Set' put: aSet;
|
||||
at: 'SortedCollection' put: { aSorted. aSorted2 }.
|
||||
aDictionary printNl.
|
||||
24
Task/Collections/Tcl/collections-2.tcl
Normal file
24
Task/Collections/Tcl/collections-2.tcl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# create an empty array
|
||||
array set h {}
|
||||
# add some pair
|
||||
set h(one) 1
|
||||
set h(two) 2
|
||||
# add more data
|
||||
array set h {three 3 four 4 more {5 6 7 8}}
|
||||
# iterate over it in a couple of ways
|
||||
foreach key [array names h] {puts "$key -> $h($key)"}
|
||||
foreach {key value} [array get h] {puts "$key -> $value"}
|
||||
|
||||
# pass by name
|
||||
proc numkeys_byname {arrayName} {
|
||||
upvar 1 $arrayName arr
|
||||
puts "array $arrayName has [llength [array names arr]] keys"
|
||||
}
|
||||
numkeys_byname h
|
||||
|
||||
# pass serialized
|
||||
proc numkeys_bycopy {l} {
|
||||
array set arr $l
|
||||
puts "array has [llength [array names arr]] keys"
|
||||
}
|
||||
numkeys_bycopy [array get h]
|
||||
9
Task/Collections/Tcl/collections-3.tcl
Normal file
9
Task/Collections/Tcl/collections-3.tcl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# create an empty dictionary
|
||||
set d [dict create]
|
||||
dict set d one 1
|
||||
dict set d two 2
|
||||
# create another
|
||||
set e [dict create three 3 four 4]
|
||||
set f [dict merge $d $e]
|
||||
dict set f nested [dict create five 5 more [list 6 7 8]]
|
||||
puts [dict get $f nested more] ;# ==> 6 7 8
|
||||
12
Task/Collections/Tcl/collections.tcl
Normal file
12
Task/Collections/Tcl/collections.tcl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
set c [list] ;# create an empty list
|
||||
# fill it
|
||||
lappend c 10 11 13
|
||||
set c [linsert $c 2 "twelve goes here"]
|
||||
# iterate over it
|
||||
foreach elem $c {puts $elem}
|
||||
|
||||
# pass to a proc
|
||||
proc show_size {l} {
|
||||
puts [llength $l]
|
||||
}
|
||||
show_size $c
|
||||
Loading…
Add table
Add a link
Reference in a new issue