2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,7 +1,13 @@
{{clarified-review}}
Collections are abstractions to represent sets of values.
In statically-typed languages, the values are typically of a common data type.
;Task:
Create a collection, and add a few values to it.
{{Template:See also lists}}
<br><br>

View file

@ -0,0 +1,21 @@
# create a constant array of integers and set its values #
[]INT constant array = ( 1, 2, 3, 4 );
# create an array of integers that can be changed, note the size mst be specified #
# this array has the default lower bound of 1 #
[ 5 ]INT mutable array := ( 9, 8, 7, 6, 5 );
# modify the second element of the mutable array #
mutable array[ 2 ] := -1;
# array sizes are normally fixed when the array is created, however arrays can be #
# declared to be FLEXible, allowing their sizes to change by assigning a new array to them #
# The standard built-in STRING is notionally defined as FLEX[ 1 : 0 ]CHAR in the standard prelude #
# Create a string variable: #
STRING str := "abc";
# assign a longer value to it #
str := "bbc/itv";
# add a few characters to str, +=: adds the text to the beginning, +:= adds it to the end #
"[" +=: str; str +:= "]"; # str now contains "[bbc/itv]" #
# Arrays of any type can be FLEXible: #
# create an array of two integers #
FLEX[ 1 : 2 ]INT fa := ( 0, 0 );
# replace it with a new array of 5 elements #
fa := LOC[ -2 : 2 ]INT;

View file

@ -0,0 +1,37 @@
identification division.
program-id. collections.
data division.
working-storage section.
01 sample-table.
05 sample-record occurs 1 to 3 times depending on the-index.
10 sample-alpha pic x(4).
10 filler pic x value ":".
10 sample-number pic 9(4).
10 filler pic x value space.
77 the-index usage index.
procedure division.
collections-main.
set the-index to 3
move 1234 to sample-number(1)
move "abcd" to sample-alpha(1)
move "test" to sample-alpha(2)
move 6789 to sample-number(3)
move "wxyz" to sample-alpha(3)
display "sample-table : " sample-table
display "sample-number(1): " sample-number(1)
display "sample-record(2): " sample-record(2)
display "sample-number(3): " sample-number(3)
*> abend: out of bounds subscript, -debug turns on bounds check
set the-index down by 1
display "sample-table : " sample-table
display "sample-number(3): " sample-number(3)
goback.
end program collections.

View file

@ -0,0 +1,5 @@
// Creates and initializes a new Array
#var intArray := (1, 2, 3, 4, 5).
#var stringArr := Array new:5.
stringArr@0 := "string".

View file

@ -0,0 +1,5 @@
//Create and initialize ArrayList
#var myAl := ArrayList new += "Hello" += "World" += "!".
//Create and initialize List
#var myList := List new += "Hello" += "World" += "!".

View file

@ -0,0 +1,4 @@
//Create a dictionary
#var dict := Dictionary new.
dict@"Hello" := "World".
dict@"Key" := "Value".

View file

@ -1,7 +1,15 @@
empty_map = Map.new #=> %{}
map = %{:a => 1, 2 => :b} #=> %{2 => :b, :a => 1}
map[:a] #=> 1
map[2] #=> :b
empty_map = Map.new #=> %{}
kwlist = [x: 1, y: 2] # Key Word List
Map.new(kwlist) #=> %{x: 1, y: 2}
Map.new([{1,"A"}, {2,"B"}]) #=> %{1 => "A", 2 => "B"}
map = %{:a => 1, 2 => :b} #=> %{2 => :b, :a => 1}
map[:a] #=> 1
map[2] #=> :b
# If you pass duplicate keys when creating a map, the last one wins:
%{1 => 1, 1 => 2} #=> %{1 => 2}
%{1 => 1, 1 => 2} #=> %{1 => 2}
# When all the keys in a map are atoms, you can use the keyword syntax for convenience:
map = %{:a => 1, :b => 2} #=> %{a: 1, b: 2}
map.a #=> 1
%{map | :a => 2} #=> %{a: 2, b: 2} update only

View file

@ -1,3 +1,10 @@
map = %{:a => 1, :b => 2} #=> %{a: 1, b: 2}
map.a #=> 1
%{map | :a => 2} #=> %{a: 2, b: 2}
empty_set = MapSet.new #=> #MapSet<[]>
set1 = MapSet.new(1..4) #=> #MapSet<[1, 2, 3, 4]>
MapSet.size(set1) #=> 4
MapSet.member?(set1,3) #=> true
MapSet.put(set1,9) #=> #MapSet<[1, 2, 3, 4, 9]>
set2 = MapSet.new([6,4,2,0]) #=> #MapSet<[0, 2, 4, 6]>
MapSet.union(set1,set2) #=> #MapSet<[0, 1, 2, 3, 4, 6]>
MapSet.intersection(set1,set2) #=> #MapSet<[2, 4]>
MapSet.difference(set1,set2) #=> #MapSet<[1, 3]>
MapSet.subset?(set1,set2) #=> false

View file

@ -1,10 +1,9 @@
empty_set = HashSet.new #=> #HashSet<[]>
set1 = Enum.into(1..4,HashSet.new) #=> #HashSet<[2, 3, 4, 1]>
Set.size(set1) #=> 4
Set.member?(set1,3) #=> true
Set.put(set1,9) #=> #HashSet<[2, 3, 4, 1, 9]>
set2 = Enum.into([0,2,4,6],HashSet.new) #=> #HashSet<[0, 2, 6, 4]>
Set.union(set1,set2) #=> #HashSet<[0, 2, 6, 4, 3, 1]>
Set.intersection(set1,set2) #=> #HashSet<[2, 4]>
Set.difference(set1,set2) #=> #HashSet<[3, 1]>
Set.subset?(set1,set2) #=> false
defmodule User do
defstruct name: "john", age: 27
end
john = %User{} #=> %User{age: 27, name: "john"}
john.name #=> "john"
%User{age: age} = john # pattern matching
age #=> 27
meg = %User{name: "meg"} #=> %User{age: 27, name: "meg"}
is_map(meg) #=> true

View file

@ -0,0 +1,2 @@
let a = [1u8,2,3,4,5]; // a is of type [u8; 5];
let b = [0;256] // Equivalent to `let b = [0,0,0,0,0,0... repeat 256 times]`

View file

@ -0,0 +1,3 @@
let array = [1,2,3,4,5];
let slice = &array[0..2]
println!("{:?}", slice);

View file

@ -0,0 +1,6 @@
let mut v = Vec::new();
v.push(1);
v.push(2);
v.push(3);
// Or (mostly) equivalently via a convenient macro in the standard library
let v = vec![1,2,3];

View file

@ -0,0 +1,4 @@
let x = "abc"; // x is of type &str (a borrowed string slice)
let s = String::from(x);
// or alternatively
let s = x.to_owned();