Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
5
Task/Collections/00-META.yaml
Normal file
5
Task/Collections/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Data Structures
|
||||
from: http://rosettacode.org/wiki/Collections
|
||||
note: Basic language learning
|
||||
12
Task/Collections/00-TASK.txt
Normal file
12
Task/Collections/00-TASK.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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>
|
||||
|
||||
33
Task/Collections/68000-Assembly/collections.68000
Normal file
33
Task/Collections/68000-Assembly/collections.68000
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
dc.l TrapString_Bus
|
||||
dc.l TrapString_Addr
|
||||
dc.l TrapString_Illegal
|
||||
dc.l TrapString_Div0
|
||||
dc.l TrapString_chk
|
||||
dc.l TrapString_v
|
||||
dc.l TrapString_priv
|
||||
dc.l TrapString_trace
|
||||
|
||||
TrapString_Bus:
|
||||
dc.b "Bus error",255
|
||||
even
|
||||
TrapString_Addr:
|
||||
dc.b "Address error",255
|
||||
even
|
||||
TrapString_Illegal:
|
||||
dc.b "Illegal Instruction",255
|
||||
even
|
||||
TrapString_Div0:
|
||||
dc.b "Divide By Zero",255
|
||||
even
|
||||
TrapString_chk:
|
||||
dc.b "CHK Failure",255
|
||||
even
|
||||
TrapString_v:
|
||||
dc.b "Signed Overflow",255
|
||||
even
|
||||
TrapString_priv:
|
||||
dc.b "Privilege Violation",255
|
||||
even
|
||||
TrapString_trace:
|
||||
dc.b "Tracing",255
|
||||
even
|
||||
18
Task/Collections/ABAP/collections.abap
Normal file
18
Task/Collections/ABAP/collections.abap
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
REPORT z_test_rosetta_collection.
|
||||
|
||||
CLASS lcl_collection DEFINITION CREATE PUBLIC.
|
||||
|
||||
PUBLIC SECTION.
|
||||
METHODS: start.
|
||||
ENDCLASS.
|
||||
|
||||
CLASS lcl_collection IMPLEMENTATION.
|
||||
METHOD start.
|
||||
DATA(itab) = VALUE int4_table( ( 1 ) ( 2 ) ( 3 ) ).
|
||||
|
||||
cl_demo_output=>display( itab ).
|
||||
ENDMETHOD.
|
||||
ENDCLASS.
|
||||
|
||||
START-OF-SELECTION.
|
||||
NEW lcl_collection( )->start( ).
|
||||
21
Task/Collections/ALGOL-68/collections.alg
Normal file
21
Task/Collections/ALGOL-68/collections.alg
Normal 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;
|
||||
1
Task/Collections/AWK/collections-1.awk
Normal file
1
Task/Collections/AWK/collections-1.awk
Normal file
|
|
@ -0,0 +1 @@
|
|||
a[0]="hello"
|
||||
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]
|
||||
11
Task/Collections/Ada/collections-1.ada
Normal file
11
Task/Collections/Ada/collections-1.ada
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
procedure Array_Collection is
|
||||
|
||||
A : array (-3 .. -1) of Integer := (1, 2, 3);
|
||||
|
||||
begin
|
||||
|
||||
A (-3) := 3;
|
||||
A (-2) := 2;
|
||||
A (-1) := 1;
|
||||
|
||||
end Array_Collection;
|
||||
12
Task/Collections/Ada/collections-2.ada
Normal file
12
Task/Collections/Ada/collections-2.ada
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
procedure Array_Collection is
|
||||
|
||||
type Array_Type is array (1 .. 3) of Integer;
|
||||
A : Array_Type := (1, 2, 3);
|
||||
|
||||
begin
|
||||
|
||||
A (1) := 3;
|
||||
A (2) := 2;
|
||||
A (3) := 1;
|
||||
|
||||
end Array_Collection;
|
||||
13
Task/Collections/Ada/collections-3.ada
Normal file
13
Task/Collections/Ada/collections-3.ada
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
procedure Array_Collection is
|
||||
|
||||
type Array_Type is array (positive range <>) of Integer; -- may be indexed with any positive
|
||||
-- Integer value
|
||||
A : Array_Type(1 .. 3); -- creates an array of three integers, indexed from 1 to 3
|
||||
|
||||
begin
|
||||
|
||||
A (1) := 3;
|
||||
A (2) := 2;
|
||||
A (3) := 1;
|
||||
|
||||
end Array_Collection;
|
||||
17
Task/Collections/Ada/collections-4.ada
Normal file
17
Task/Collections/Ada/collections-4.ada
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
with Ada.Containers.Doubly_Linked_Lists;
|
||||
use Ada.Containers;
|
||||
|
||||
procedure Doubly_Linked_List is
|
||||
|
||||
package DL_List_Pkg is new Doubly_Linked_Lists (Integer);
|
||||
use DL_List_Pkg;
|
||||
|
||||
DL_List : List;
|
||||
|
||||
begin
|
||||
|
||||
DL_List.Append (1);
|
||||
DL_List.Append (2);
|
||||
DL_List.Append (3);
|
||||
|
||||
end Doubly_Linked_List;
|
||||
17
Task/Collections/Ada/collections-5.ada
Normal file
17
Task/Collections/Ada/collections-5.ada
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
with Ada.Containers.Vectors;
|
||||
use Ada.Containers;
|
||||
|
||||
procedure Vector_Example is
|
||||
|
||||
package Vector_Pkg is new Vectors (Natural, Integer);
|
||||
use Vector_Pkg;
|
||||
|
||||
V : Vector;
|
||||
|
||||
begin
|
||||
|
||||
V.Append (1);
|
||||
V.Append (2);
|
||||
V.Append (3);
|
||||
|
||||
end Vector_Example;
|
||||
1
Task/Collections/Aime/collections-1.aime
Normal file
1
Task/Collections/Aime/collections-1.aime
Normal file
|
|
@ -0,0 +1 @@
|
|||
list l;
|
||||
3
Task/Collections/Aime/collections-2.aime
Normal file
3
Task/Collections/Aime/collections-2.aime
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
l_p_integer(l, 0, 7);
|
||||
l_push(l, "a string");
|
||||
l_append(l, 2.5);
|
||||
4
Task/Collections/Aime/collections-3.aime
Normal file
4
Task/Collections/Aime/collections-3.aime
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
l_query(l, 2)
|
||||
l_head(l)
|
||||
l_q_text(l, 1)
|
||||
l[3]
|
||||
1
Task/Collections/Aime/collections-4.aime
Normal file
1
Task/Collections/Aime/collections-4.aime
Normal file
|
|
@ -0,0 +1 @@
|
|||
record r;
|
||||
3
Task/Collections/Aime/collections-5.aime
Normal file
3
Task/Collections/Aime/collections-5.aime
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
r_p_integer(r, "key1", 7);
|
||||
r_put(r, "key2", "a string");
|
||||
r["key3"] = .25;
|
||||
3
Task/Collections/Aime/collections-6.aime
Normal file
3
Task/Collections/Aime/collections-6.aime
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
r_query(r, "key1")
|
||||
r_tail(r)
|
||||
r["key2"]
|
||||
4
Task/Collections/Apex/collections-1.apex
Normal file
4
Task/Collections/Apex/collections-1.apex
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
// Create an empty list of String
|
||||
List<String> my_list = new List<String>();
|
||||
// Create a nested list
|
||||
List<List<Set<Integer>>> my_list_2 = new List<List<Set<Integer>>>();
|
||||
6
Task/Collections/Apex/collections-2.apex
Normal file
6
Task/Collections/Apex/collections-2.apex
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
List<Integer> myList = new List<Integer>(); // Define a new list
|
||||
myList.add(47); // Adds a second element of value 47 to the end
|
||||
// of the list
|
||||
Integer i = myList.get(0); // Retrieves the element at index 0
|
||||
myList.set(0, 1); // Adds the integer 1 to the list at index 0
|
||||
myList.clear(); // Removes all elements from the list
|
||||
3
Task/Collections/Apex/collections-3.apex
Normal file
3
Task/Collections/Apex/collections-3.apex
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
String[] colors = new List<String>();
|
||||
List<String> colors = new String[1];
|
||||
colors[0] = 'Green';
|
||||
3
Task/Collections/Apex/collections-4.apex
Normal file
3
Task/Collections/Apex/collections-4.apex
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Set<String> s1 = new Set<String>{'a', 'b + c'}; // Defines a new set with two elements
|
||||
Set<String> s2 = new Set<String>(s1); // Defines a new set that contains the
|
||||
// elements of the set created in the previous step
|
||||
4
Task/Collections/Apex/collections-5.apex
Normal file
4
Task/Collections/Apex/collections-5.apex
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Set<Integer> s = new Set<Integer>(); // Define a new set
|
||||
s.add(1); // Add an element to the set
|
||||
System.assert(s.contains(1)); // Assert that the set contains an element
|
||||
s.remove(1); // Remove the element from the set
|
||||
3
Task/Collections/Apex/collections-6.apex
Normal file
3
Task/Collections/Apex/collections-6.apex
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Map<String, String> country_currencies = new Map<String, String>();
|
||||
Map<ID, Set<String>> m = new Map<ID, Set<String>>();
|
||||
Map<String, String> MyStrings = new Map<String, String>{'a' => 'b', 'c' => 'd'.toUpperCase()};
|
||||
7
Task/Collections/Apex/collections-7.apex
Normal file
7
Task/Collections/Apex/collections-7.apex
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Map<Integer, String> m = new Map<Integer, String>(); // Define a new map
|
||||
m.put(1, 'First entry'); // Insert a new key-value pair in the map
|
||||
m.put(2, 'Second entry'); // Insert a new key-value pair in the map
|
||||
System.assert(m.containsKey(1)); // Assert that the map contains a key
|
||||
String value = m.get(2); // Retrieve a value, given a particular key
|
||||
System.assertEquals('Second entry', value);
|
||||
Set<Integer> s = m.keySet(); // Return a set that contains all of the keys in the map
|
||||
8
Task/Collections/Arturo/collections-1.arturo
Normal file
8
Task/Collections/Arturo/collections-1.arturo
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
; initialize array
|
||||
arr: ["one" 2 "three" "four"]
|
||||
|
||||
; add an element to the array
|
||||
arr: arr ++ 5
|
||||
|
||||
; print it
|
||||
print arr
|
||||
13
Task/Collections/Arturo/collections-2.arturo
Normal file
13
Task/Collections/Arturo/collections-2.arturo
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
; initialize dictionary
|
||||
dict: #[
|
||||
name: "john"
|
||||
surname: "doe"
|
||||
age: 34
|
||||
preferredFood: ["fruit" "pizza"]
|
||||
]
|
||||
|
||||
; add an element to the dictionary
|
||||
dict\country: "Spain"
|
||||
|
||||
; print it
|
||||
print dict
|
||||
4
Task/Collections/AutoHotkey/collections-1.ahk
Normal file
4
Task/Collections/AutoHotkey/collections-1.ahk
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
myCol := Object()
|
||||
mycol.mykey := "my value!"
|
||||
mycol["mykey"] := "new val!"
|
||||
MsgBox % mycol.mykey ; new val
|
||||
3
Task/Collections/AutoHotkey/collections-2.ahk
Normal file
3
Task/Collections/AutoHotkey/collections-2.ahk
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Loop 3
|
||||
array%A_Index% := A_Index * 9
|
||||
MsgBox % array1 " " array2 " " array3 ; 9 18 27
|
||||
4
Task/Collections/AutoHotkey/collections-3.ahk
Normal file
4
Task/Collections/AutoHotkey/collections-3.ahk
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
VarSetCapacity(Rect, 16) ; A RECT is a struct consisting of four 32-bit integers (i.e. 4*4=16).
|
||||
DllCall("GetWindowRect", UInt, WinExist(), UInt, &Rect) ; WinExist() returns an HWND.
|
||||
MsgBox % "Left " . NumGet(Rect, 0, true) . " Top " . NumGet(Rect, 4, true)
|
||||
. " Right " . NumGet(Rect, 8, true) . " Bottom " . NumGet(Rect, 12, true)
|
||||
8
Task/Collections/Axe/collections.axe
Normal file
8
Task/Collections/Axe/collections.axe
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
1→{L₁}
|
||||
2→{L₁+1}
|
||||
3→{L₁+2}
|
||||
4→{L₁+3}
|
||||
Disp {L₁}►Dec,i
|
||||
Disp {L₁+1}►Dec,i
|
||||
Disp {L₁+2}►Dec,i
|
||||
Disp {L₁+3}►Dec,i
|
||||
3
Task/Collections/BBC-BASIC/collections-1.basic
Normal file
3
Task/Collections/BBC-BASIC/collections-1.basic
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
DIM text$(1)
|
||||
text$(0) = "Hello "
|
||||
text$(1) = "world!"
|
||||
5
Task/Collections/BBC-BASIC/collections-2.basic
Normal file
5
Task/Collections/BBC-BASIC/collections-2.basic
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
DIM collection{(1) name$, year%}
|
||||
collection{(0)}.name$ = "Richard"
|
||||
collection{(0)}.year% = 1952
|
||||
collection{(1)}.name$ = "Sue"
|
||||
collection{(1)}.year% = 1950
|
||||
24
Task/Collections/BBC-BASIC/collections-3.basic
Normal file
24
Task/Collections/BBC-BASIC/collections-3.basic
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
DIM node{name$, year%, link%}
|
||||
list% = 0
|
||||
PROCadd(list%, node{}, "Richard", 1952)
|
||||
PROCadd(list%, node{}, "Sue", 1950)
|
||||
PROClist(list%, node{})
|
||||
END
|
||||
|
||||
DEF PROCadd(RETURN l%, c{}, n$, y%)
|
||||
LOCAL p%
|
||||
DIM p% DIM(c{})-1
|
||||
!(^c{}+4) = p%
|
||||
c.name$ = n$
|
||||
c.year% = y%
|
||||
c.link% = l%
|
||||
l% = p%
|
||||
ENDPROC
|
||||
|
||||
DEF PROClist(l%, c{})
|
||||
WHILE l%
|
||||
!(^c{}+4) = l%
|
||||
PRINT c.name$, c.year%
|
||||
l% = c.link%
|
||||
ENDWHILE
|
||||
ENDPROC
|
||||
8
Task/Collections/C++/collections-1.cpp
Normal file
8
Task/Collections/C++/collections-1.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
int a[5]; // array of 5 ints (since int is POD, the members are not initialized)
|
||||
a[0] = 1; // indexes start at 0
|
||||
|
||||
int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; // arrays can be initialized on creation
|
||||
|
||||
#include <string>
|
||||
std::string strings[4]; // std::string is no POD, therefore all array members are default-initialized
|
||||
// (for std::string this means initialized with empty strings)
|
||||
5
Task/Collections/C++/collections-2.cpp
Normal file
5
Task/Collections/C++/collections-2.cpp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#include <vector>
|
||||
|
||||
std::vector<int> v; // empty vector
|
||||
v.push_back(5); // insert a 5 at the end
|
||||
v.insert(v.begin(), 7); // insert a 7 at the beginning
|
||||
6
Task/Collections/C++/collections-3.cpp
Normal file
6
Task/Collections/C++/collections-3.cpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include <deque>
|
||||
|
||||
std::deque<int> d; // empty deque
|
||||
d.push_back(5); // insert a 5 at the end
|
||||
d.push_front(7); // insert a 7 at the beginning
|
||||
d.insert(v.begin()+1, 6); // insert a 6 in the middle
|
||||
8
Task/Collections/C++/collections-4.cpp
Normal file
8
Task/Collections/C++/collections-4.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <list>
|
||||
|
||||
std::list<int> l; // empty list
|
||||
l.push_back(5); // insert a 5 at the end
|
||||
l.push_front(7); // insert a 7 at the beginning
|
||||
std::list::iterator i = l.begin();
|
||||
++l;
|
||||
l.insert(i, 6); // insert a 6 in the middle
|
||||
6
Task/Collections/C++/collections-5.cpp
Normal file
6
Task/Collections/C++/collections-5.cpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include <set>
|
||||
|
||||
std::set<int> s; // empty set
|
||||
s.insert(5); // insert a 5
|
||||
s.insert(7); // insert a 7 (automatically placed after the 5)
|
||||
s.insert(5); // try to insert another 5 (will not change the set)
|
||||
6
Task/Collections/C++/collections-6.cpp
Normal file
6
Task/Collections/C++/collections-6.cpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include <multiset>
|
||||
|
||||
std::multiset<int> m; // empty multiset
|
||||
m.insert(5); // insert a 5
|
||||
m.insert(7); // insert a 7 (automatically placed after the 5)
|
||||
m.insert(5); // insert a second 5 (now m contains two 5s, followed by one 7)
|
||||
10
Task/Collections/C-sharp/collections-1.cs
Normal file
10
Task/Collections/C-sharp/collections-1.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
// Creates and initializes a new integer Array
|
||||
int[] intArray = new int[5] { 1, 2, 3, 4, 5 };
|
||||
//same as
|
||||
int[] intArray = new int[]{ 1, 2, 3, 4, 5 };
|
||||
//same as
|
||||
int[] intArray = { 1, 2, 3, 4, 5 };
|
||||
|
||||
//Arrays are zero-based
|
||||
string[] stringArr = new string[5];
|
||||
stringArr[0] = "string";
|
||||
8
Task/Collections/C-sharp/collections-2.cs
Normal file
8
Task/Collections/C-sharp/collections-2.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
//Create and initialize ArrayList
|
||||
ArrayList myAl = new ArrayList { "Hello", "World", "!" };
|
||||
|
||||
//Create ArrayList and add some values
|
||||
ArrayList myAL = new ArrayList();
|
||||
myAL.Add("Hello");
|
||||
myAL.Add("World");
|
||||
myAL.Add("!");
|
||||
8
Task/Collections/C-sharp/collections-3.cs
Normal file
8
Task/Collections/C-sharp/collections-3.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
//Create and initialize List
|
||||
List<string> myList = new List<string> { "Hello", "World", "!" };
|
||||
|
||||
//Create List and add some values
|
||||
List<string> myList2 = new List<string>();
|
||||
myList2.Add("Hello");
|
||||
myList2.Add("World");
|
||||
myList2.Add("!");
|
||||
7
Task/Collections/C-sharp/collections-4.cs
Normal file
7
Task/Collections/C-sharp/collections-4.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
//Create an initialize Hashtable
|
||||
Hashtable myHt = new Hashtable() { { "Hello", "World" }, { "Key", "Value" } };
|
||||
|
||||
//Create Hashtable and add some Key-Value pairs.
|
||||
Hashtable myHt2 = new Hashtable();
|
||||
myHt2.Add("Hello", "World");
|
||||
myHt2.Add("Key", "Value");
|
||||
6
Task/Collections/C-sharp/collections-5.cs
Normal file
6
Task/Collections/C-sharp/collections-5.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
//Create an initialize Dictionary
|
||||
Dictionary<string, string> dict = new Dictionary<string, string>() { { "Hello", "World" }, { "Key", "Value" } };
|
||||
//Create Dictionary and add some Key-Value pairs.
|
||||
Dictionary<string, string> dict2 = new Dictionary<string, string>();
|
||||
dict2.Add("Hello", "World");
|
||||
dict2.Add("Key", "Value");
|
||||
11
Task/Collections/C/collections-1.c
Normal file
11
Task/Collections/C/collections-1.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); */
|
||||
} /* } */
|
||||
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); */
|
||||
} /* } */
|
||||
37
Task/Collections/COBOL/collections.cobol
Normal file
37
Task/Collections/COBOL/collections.cobol
Normal 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.
|
||||
4
Task/Collections/Clojure/collections-1.clj
Normal file
4
Task/Collections/Clojure/collections-1.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
|
||||
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])
|
||||
41
Task/Collections/Common-Lisp/collections-1.lisp
Normal file
41
Task/Collections/Common-Lisp/collections-1.lisp
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
CL-USER> (let ((list '())
|
||||
(hash-table (make-hash-table)))
|
||||
(push 1 list)
|
||||
(push 2 list)
|
||||
(push 3 list)
|
||||
(format t "~S~%" (reverse list))
|
||||
(setf (gethash 'foo hash-table) 42)
|
||||
(setf (gethash 'bar hash-table) 69)
|
||||
(maphash (lambda (key value)
|
||||
(format t "~S => ~S~%" key value))
|
||||
hash-table)
|
||||
;; or print the hash-table in readable form
|
||||
;; (inplementation-dependent)
|
||||
(write hash-table :readably t)
|
||||
;; or describe it
|
||||
(describe hash-table)
|
||||
;; describe the list as well
|
||||
(describe list))
|
||||
;; FORMAT on a list
|
||||
(1 2 3)
|
||||
;; FORMAT on a hash-table
|
||||
FOO => 42
|
||||
BAR => 69
|
||||
;; WRITE :readably t on a hash-table
|
||||
#.(SB-IMPL::%STUFF-HASH-TABLE
|
||||
(MAKE-HASH-TABLE :TEST 'EQL :SIZE '16 :REHASH-SIZE '1.5
|
||||
:REHASH-THRESHOLD '1.0 :WEAKNESS 'NIL)
|
||||
'((BAR . 69) (FOO . 42)))
|
||||
;; DESCRIBE on a hash-table
|
||||
#<HASH-TABLE :TEST EQL :COUNT 2 {1002B6F391}>
|
||||
[hash-table]
|
||||
|
||||
Occupancy: 0.1
|
||||
Rehash-threshold: 1.0
|
||||
Rehash-size: 1.5
|
||||
Size: 16
|
||||
Synchronized: no
|
||||
;; DESCRIBE on a list
|
||||
(3 2 1)
|
||||
[list]
|
||||
; No value
|
||||
36
Task/Collections/Common-Lisp/collections-2.lisp
Normal file
36
Task/Collections/Common-Lisp/collections-2.lisp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
;;; Obtained from Usenet,
|
||||
;;; Message-ID: <b3b1cc90-2e2b-43c3-b7d9-785ae29870e7@e23g2000prf.googlegroups.com>
|
||||
;;; Posting by Kaz Kylheku, February 28, 2008.
|
||||
|
||||
(eval-when (:compile-toplevel :load-toplevel :execute)
|
||||
(defun bisect-list (list &optional (minimum-length 0))
|
||||
(do ((double-skipper (cddr list) (cddr double-skipper))
|
||||
(single-skipper list (cdr single-skipper))
|
||||
(length 2 (+ length (if (cdr double-skipper) 2 1))))
|
||||
((null double-skipper)
|
||||
(cond
|
||||
((< length minimum-length)
|
||||
(values list nil))
|
||||
((consp single-skipper)
|
||||
(multiple-value-prog1
|
||||
(values list (cdr single-skipper))
|
||||
(setf (cdr single-skipper) nil)))
|
||||
(t (values list nil))))))
|
||||
|
||||
(defun pop-deque-helper (facing-piece other-piece)
|
||||
(if (null facing-piece)
|
||||
(multiple-value-bind (head tail) (bisect-list other-piece 10)
|
||||
(let ((remaining (if tail head))
|
||||
(moved (nreverse (or tail head))))
|
||||
(values (first moved) (rest moved) remaining)))
|
||||
(values (first facing-piece) (rest facing-piece) other-piece))))
|
||||
|
||||
(defmacro pop-deque (facing-piece other-piece)
|
||||
(let ((result (gensym))
|
||||
(new-facing (gensym))
|
||||
(new-other (gensym)))
|
||||
`(multiple-value-bind (,result ,new-facing ,new-other)
|
||||
(pop-deque-helper ,facing-piece ,other-piece)
|
||||
(psetf ,facing-piece ,new-facing
|
||||
,other-piece ,new-other)
|
||||
,result)))
|
||||
3
Task/Collections/D/collections-1.d
Normal file
3
Task/Collections/D/collections-1.d
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
int[3] array;
|
||||
array[0] = 5;
|
||||
// array.length = 4; // compile-time error
|
||||
6
Task/Collections/D/collections-2.d
Normal file
6
Task/Collections/D/collections-2.d
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
int[] array;
|
||||
array ~= 5; // append 5
|
||||
array.length = 3;
|
||||
array[3] = 17; // runtime error: out of bounds. check removed in release mode.
|
||||
array = [2, 17, 3];
|
||||
writefln(array.sort); // 2, 3, 17
|
||||
8
Task/Collections/D/collections-3.d
Normal file
8
Task/Collections/D/collections-3.d
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
int[int] array;
|
||||
// array ~= 5; // it doesn't work that way!
|
||||
array[5] = 17;
|
||||
array[6] = 20;
|
||||
// prints "[5, 6]" -> "[17, 20]" - although the order is not specified.
|
||||
writefln(array.keys, " -> ", array.values);
|
||||
assert(5 in array); // returns a pointer, by the way
|
||||
if (auto ptr = 6 in array) writefln(*ptr); // 20
|
||||
30
Task/Collections/Delphi/collections-1.delphi
Normal file
30
Task/Collections/Delphi/collections-1.delphi
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// Creates and initializes a new integer Array
|
||||
var
|
||||
// Dynamics arrays can be initialized, if it's global variable in declaration scope
|
||||
intArray: TArray<Integer> = [1, 2, 3, 4, 5];
|
||||
intArray2: array of Integer = [1, 2, 3, 4, 5];
|
||||
|
||||
//Cann't initialize statics arrays in declaration scope
|
||||
intArray3: array [0..4]of Integer;
|
||||
intArray4: array [10..14]of Integer;
|
||||
|
||||
procedure
|
||||
var
|
||||
// Any arrays can't be initialized, if it's local variable in declaration scope
|
||||
intArray5: TArray<Integer>;
|
||||
begin
|
||||
// Dynamics arrays can be full assigned in routine scope
|
||||
intArray := [1,2,3];
|
||||
intArray2 := [1,2,3];
|
||||
|
||||
// Dynamics arrays zero-based
|
||||
intArray[0] := 1;
|
||||
|
||||
// Dynamics arrays must set size, if it not was initialized before
|
||||
SetLength(intArray,5);
|
||||
|
||||
// Inline dynamics arrays can be created and initialized routine scope
|
||||
// only for version after 10.3 Tokyo
|
||||
var intArray6 := [1, 2, 3];
|
||||
var intArray7: TArray<Integer> := [1, 2, 3];
|
||||
end;
|
||||
28
Task/Collections/Delphi/collections-2.delphi
Normal file
28
Task/Collections/Delphi/collections-2.delphi
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
var
|
||||
// TLists can't be initialized or created in declaration scope
|
||||
List1, List2:TList<Integer>;
|
||||
begin
|
||||
List1 := TList<Integer>.Create;
|
||||
List1.Add(1);
|
||||
list1.AddRange([2, 3]);
|
||||
List1.free;
|
||||
|
||||
// TList can be initialized using a class derivative from TEnumerable, like it self
|
||||
List1 := TList<Integer>.Create;
|
||||
list1.AddRange([1,2, 3]);
|
||||
|
||||
List2 := TList<Integer>.Create(list1);
|
||||
Writeln(List2[2]); // 3
|
||||
List1.free;
|
||||
List2.free;
|
||||
|
||||
|
||||
// Inline TList can be created in routine scope
|
||||
// only for version after 10.3 Tokyo
|
||||
var List3:= TList<Integer>.Create;
|
||||
List3.Add(2);
|
||||
List3.free;
|
||||
|
||||
var List4: TList<Integer>:= TList<Integer>.Create;
|
||||
List4.free;
|
||||
end;
|
||||
18
Task/Collections/Delphi/collections-3.delphi
Normal file
18
Task/Collections/Delphi/collections-3.delphi
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
var
|
||||
// TDictionary can't be initialized or created in declaration scope
|
||||
Dic1: TDictionary<string, Integer>;
|
||||
begin
|
||||
Dic1 := TDictionary<string, Integer>.Create;
|
||||
Dic1.Add('one',1);
|
||||
Dic1.free;
|
||||
|
||||
// Inline TDictionary can be created in routine scope
|
||||
// only for version after 10.3 Tokyo
|
||||
var Dic2:= TDictionary<string, Integer>.Create;
|
||||
Dic2.Add('one',1);
|
||||
Dic2.free;
|
||||
|
||||
var Dic3: TDictionary<string, Integer>:= TDictionary<string, Integer>.Create.Create;
|
||||
Dic3.Add('one',1);
|
||||
Dic3.free;
|
||||
end;
|
||||
24
Task/Collections/Delphi/collections-4.delphi
Normal file
24
Task/Collections/Delphi/collections-4.delphi
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
var
|
||||
Queue1, Queue2: TQueue<Integer>;
|
||||
List1:TList<Integer>;
|
||||
begin
|
||||
Queue1 := TQueue<Integer>.Create;
|
||||
Queue1.Enqueue(1);
|
||||
Queue1.Enqueue(2);
|
||||
Writeln(Queue1.Dequeue); // 1
|
||||
Writeln(Queue1.Dequeue); // 2
|
||||
Queue1.free;
|
||||
|
||||
// TQueue can be initialized using a class derivative from TEnumerable, like TList<T>
|
||||
List1 := TList<Integer>.Create;
|
||||
List1.Add(3);
|
||||
Queue2:= TQueue<Integer>.Create(List1);
|
||||
Writeln(Queue2.Dequeue); // 3
|
||||
List1.free;
|
||||
Queue2.free;
|
||||
|
||||
// Inline TQueue can be created in routine scope
|
||||
// only for version after 10.3 Tokyo
|
||||
var Queue3 := TQueue<Integer>.Create;
|
||||
Queue3.free;
|
||||
end;
|
||||
24
Task/Collections/Delphi/collections-5.delphi
Normal file
24
Task/Collections/Delphi/collections-5.delphi
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
var
|
||||
Stack1, Stack2: TStack<Integer>;
|
||||
List1:TList<Integer>;
|
||||
begin
|
||||
Stack1:= TStack<Integer>.Create;
|
||||
Stack1.Push(1);
|
||||
Stack1.Push(2);
|
||||
Writeln(Stack1.Pop); // 2
|
||||
Writeln(Stack1.Pop); // 1
|
||||
Stack1.free;
|
||||
|
||||
// TStack can be initialized using a class derivative from TEnumerable, like TList<T>
|
||||
List1 := TList<Integer>.Create;
|
||||
List1.Add(3);
|
||||
Stack2:= TStack<Integer>.Create(List1);
|
||||
Writeln(Stack2.Pop); // 3
|
||||
List1.free;
|
||||
Stack2.free;
|
||||
|
||||
// Inline TStack can be created in routine scope
|
||||
// only for version after 10.3 Tokyo
|
||||
var Stack3:= TStack<Integer>.Create;
|
||||
Stack3.free;
|
||||
end;
|
||||
27
Task/Collections/Delphi/collections-6.delphi
Normal file
27
Task/Collections/Delphi/collections-6.delphi
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
var
|
||||
Str1:String; // default WideString
|
||||
Str2:WideString;
|
||||
Str3:UnicodeString;
|
||||
Str4:AnsiString;
|
||||
Str5: PChar; //PWideChar is the same
|
||||
Str6: PAnsiChar;
|
||||
|
||||
// Strings can be initialized, if it's global variable in declaration scope
|
||||
Str4: string = 'orange';
|
||||
begin
|
||||
Str1 := 'apple';
|
||||
|
||||
// WideString and AnsiString can be converted implicitly, but in some times can lost information about char
|
||||
Str4 := Str1;
|
||||
|
||||
// PChar is a poiter to string (WideString), must be converted using type cast
|
||||
Str5 := Pchar(Str1);
|
||||
|
||||
// PChar not must type cast to convert back string
|
||||
Str2 := Str5;
|
||||
|
||||
//In any string, index start in 1 and end on length of string
|
||||
Writeln(Str1[1]); // 'a'
|
||||
Writeln(Str1[5]); // 'e'
|
||||
Writeln(Str1[length(str1)]); // the same above
|
||||
end;
|
||||
27
Task/Collections/Diego/collections-1.diego
Normal file
27
Task/Collections/Diego/collections-1.diego
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use_namespace(rosettacode)_me();
|
||||
|
||||
// Real world collections
|
||||
with_route(wp1-wp2)_scalar(wp1wp2Distance)_distan({m},32); // simple distance scalar
|
||||
|
||||
// Three dimensional vector (displacement - orientation (via quaternion) - energy consumption)
|
||||
with_route(from-wp1-to-wp2)_vector(wp1Towp2Displacement)_distan({m},32)_orientatout(0.98,0.174i,0.044j,0.087k)_ergconsump({kJ},35.483);
|
||||
|
||||
with_robot(alif)_sensor(frontCamera)_type(camera_3d);
|
||||
with_robot(alif)_abilit(frontalVision)_of[frontCamera]; // ability (1-dimensional)
|
||||
with_robot(alif)_sensor(rearCamera)_type(camera_3d);
|
||||
with_robot(alif)_spec(vision)_of([frontCamera],[rearCamera]); // specification (2-dimensional)
|
||||
|
||||
// Inventory of wheels of robot 'alif'
|
||||
with_robot(alif)_invent(wheels)_compon(frontLeftWheel, frontRightWheel, rearLeftWheel, rearRightWheel)_type(wheel);
|
||||
|
||||
// Non-fungible token (blockchained) of robot photo (default ledger)
|
||||
with_robot(beh)_camera(frontCamera)_snap(74827222-32232-22)
|
||||
add_nft()_[]_chain(block);
|
||||
;
|
||||
|
||||
// Non-fungible token (crosschained) of robot photo (default ledger)
|
||||
with_robot(beh)_camera(frontCamera)_snap(74827222-32232-42)
|
||||
add_nft()_[]_chain(cross);
|
||||
;
|
||||
|
||||
reset_ns[];
|
||||
27
Task/Collections/Diego/collections-2.diego
Normal file
27
Task/Collections/Diego/collections-2.diego
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use_namespace(rosettacode)_me();
|
||||
|
||||
// Abstract world collections
|
||||
add_var({int},wp1wp2Distance)_value(32); // variable
|
||||
add_stack({int},intStack)_values(1,2,3,6,7,9); // stack
|
||||
add_queue({double},intStack)_values(0.03,0.04,0.05,0.06,0.07,0.08); // queue
|
||||
add_ary({str},citrusFruit)_values(lime,orange,lemon); // array
|
||||
add_matrix()_subs(4,4)_identity(); // simple identity matrix
|
||||
|
||||
// rotation matrix
|
||||
add_matrix(wp1Towp2DisplacementRotationMatrix)_row(0.981,-0.155,0.116)_row(0.186,0.924,-0.333)_row(-0.055,0.349,0.936);
|
||||
|
||||
add_clump()_subs(5,3,2,1); // simple clump (zero'ed)
|
||||
|
||||
// list (of robots) from abstract robot objects
|
||||
add_list({robot},robots)_values([alif],[bah],[tah],[thah],[jim]);
|
||||
|
||||
// list (of robots) from SQL query
|
||||
add_list({robot},robots)_sql(SELECT id, name, type FROM tbl_robot)_as(dba_admin);
|
||||
|
||||
// dictionary
|
||||
add_dict(tanzanianBanknoteWidths)_keys(500,1000,2000,5000,10000)_values(120,125,130,135,140);
|
||||
|
||||
// hash of two images loaded from URIs
|
||||
add_hash()_ary()_values()_img()_load(/img_027454322.jpg)_img()_load(/img_027454323.jpg);
|
||||
|
||||
reset_ns[];
|
||||
27
Task/Collections/E/collections.e
Normal file
27
Task/Collections/E/collections.e
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
? def constList := [1,2,3,4,5]
|
||||
# value: [1, 2, 3, 4, 5]
|
||||
|
||||
? constList.with(6)
|
||||
# value: [1, 2, 3, 4, 5, 6]
|
||||
|
||||
? def flexList := constList.diverge()
|
||||
# value: [1, 2, 3, 4, 5].diverge()
|
||||
|
||||
? flexList.push(6)
|
||||
? flexList
|
||||
# value: [1, 2, 3, 4, 5, 6].diverge()
|
||||
|
||||
? constList
|
||||
# value: [1, 2, 3, 4, 5]
|
||||
|
||||
? def constMap := [1 => 2, 3 => 4]
|
||||
# value: [1 => 2, 3 => 4]
|
||||
|
||||
? constMap[1]
|
||||
# value: 2
|
||||
|
||||
? def constSet := [1, 2, 3, 2].asSet()
|
||||
# value: [1, 2, 3].asSet()
|
||||
|
||||
? constSet.contains(3)
|
||||
# value: true
|
||||
8
Task/Collections/EasyLang/collections.easy
Normal file
8
Task/Collections/EasyLang/collections.easy
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
array[] &= 1
|
||||
array[] &= 2
|
||||
array[] &= 3
|
||||
arrayArray[][] &= [ 1 2 ]
|
||||
arrayArray[][] &= [ 3 4 ]
|
||||
arrayArray[][] &= [ 5 6 ]
|
||||
print array[]
|
||||
print arrayArray[][]
|
||||
9
Task/Collections/EchoLisp/collections.l
Normal file
9
Task/Collections/EchoLisp/collections.l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(define my-collection ' ( 🌱 ☀️ ☔️ ))
|
||||
(set! my-collection (cons '🎥 my-collection))
|
||||
(set! my-collection (cons '🐧 my-collection))
|
||||
my-collection
|
||||
→ (🐧 🎥 🌱 ☀️ ☔️)
|
||||
|
||||
;; save it
|
||||
(local-put 'my-collection)
|
||||
→ my-collection
|
||||
6
Task/Collections/Elena/collections-1.elena
Normal file
6
Task/Collections/Elena/collections-1.elena
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// Weak array
|
||||
var stringArr := Array.allocate(5);
|
||||
stringArr[0] := "string";
|
||||
|
||||
// initialized array
|
||||
var intArray := new int[]{1, 2, 3, 4, 5};
|
||||
5
Task/Collections/Elena/collections-2.elena
Normal file
5
Task/Collections/Elena/collections-2.elena
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
//Create and initialize ArrayList
|
||||
var myAl := new system'collections'ArrayList().append:"Hello".append:"World".append:"!";
|
||||
|
||||
//Create and initialize List
|
||||
var myList := new system'collections'List().append:"Hello".append:"World".append:"!";
|
||||
4
Task/Collections/Elena/collections-3.elena
Normal file
4
Task/Collections/Elena/collections-3.elena
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
//Create a dictionary
|
||||
var dict := system'collections'Dictionary.new();
|
||||
dict["Hello"] := "World";
|
||||
dict["Key"] := "Value";
|
||||
9
Task/Collections/Elixir/collections-1.elixir
Normal file
9
Task/Collections/Elixir/collections-1.elixir
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
empty_list = []
|
||||
list = [1,2,3,4,5]
|
||||
length(list) #=> 5
|
||||
[0 | list] #=> [0,1,2,3,4,5]
|
||||
hd(list) #=> 1
|
||||
tl(list) #=> [2,3,4,5]
|
||||
Enum.at(list,3) #=> 4
|
||||
list ++ [6,7] #=> [1,2,3,4,5,6,7]
|
||||
list -- [4,2] #=> [1,3,5]
|
||||
5
Task/Collections/Elixir/collections-2.elixir
Normal file
5
Task/Collections/Elixir/collections-2.elixir
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
empty_tuple = {} #=> {}
|
||||
tuple = {0,1,2,3,4} #=> {0, 1, 2, 3, 4}
|
||||
tuple_size(tuple) #=> 5
|
||||
elem(tuple, 2) #=> 2
|
||||
put_elem(tuple,3,:atom) #=> {0, 1, 2, :atom, 4}
|
||||
4
Task/Collections/Elixir/collections-3.elixir
Normal file
4
Task/Collections/Elixir/collections-3.elixir
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
list = [{:a,1},{:b,2}] #=> [a: 1, b: 2]
|
||||
list == [a: 1, b: 2] #=> true
|
||||
list[:a] #=> 1
|
||||
list ++ [c: 3, a: 5] #=> [a: 1, b: 2, c: 3, a: 5]
|
||||
15
Task/Collections/Elixir/collections-4.elixir
Normal file
15
Task/Collections/Elixir/collections-4.elixir
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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}
|
||||
|
||||
# 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
|
||||
10
Task/Collections/Elixir/collections-5.elixir
Normal file
10
Task/Collections/Elixir/collections-5.elixir
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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
|
||||
9
Task/Collections/Elixir/collections-6.elixir
Normal file
9
Task/Collections/Elixir/collections-6.elixir
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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
|
||||
69
Task/Collections/Factor/collections.factor
Normal file
69
Task/Collections/Factor/collections.factor
Normal 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!
|
||||
8
Task/Collections/Fancy/collections-1.fancy
Normal file
8
Task/Collections/Fancy/collections-1.fancy
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 # => []
|
||||
9
Task/Collections/Fancy/collections-2.fancy
Normal file
9
Task/Collections/Fancy/collections-2.fancy
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 # => <[]>
|
||||
7
Task/Collections/Forth/collections-1.fth
Normal file
7
Task/Collections/Forth/collections-1.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
|
||||
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
|
||||
3
Task/Collections/Fortran/collections-1.f
Normal file
3
Task/Collections/Fortran/collections-1.f
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
REAL A(36) !Declares a one-dimensional array A(1), A(2), ... A(36)
|
||||
A(1) = 1 !Assigns a value to the first element.
|
||||
A(2) = 3*A(1) + 5 !The second element gets 8.
|
||||
7
Task/Collections/Fortran/collections-2.f
Normal file
7
Task/Collections/Fortran/collections-2.f
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
TYPE(MIXED) !Name the "type".
|
||||
INTEGER COUNTER !Its content is listed.
|
||||
REAL WEIGHT,DEPTH
|
||||
CHARACTER*28 MARKNAME
|
||||
COMPLEX PATH(6) !The mixed collection includes an array.
|
||||
END TYPE MIXED
|
||||
TYPE(MIXED) TEMP,A(6) !Declare some items of that type.
|
||||
17
Task/Collections/FreeBASIC/collections.basic
Normal file
17
Task/Collections/FreeBASIC/collections.basic
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
'create fixed size array of integers
|
||||
Dim a(1 To 5) As Integer = {1, 2, 3, 4, 5}
|
||||
Print a(2), a(4)
|
||||
|
||||
'create empty dynamic array of doubles
|
||||
Dim b() As Double
|
||||
' add two elements by first redimensioning the array to hold this number of elements
|
||||
Redim b(0 To 1)
|
||||
b(0) = 3.5 : b(1) = 7.1
|
||||
Print b(0), b(1)
|
||||
|
||||
'create 2 dimensional fixed size array of bytes
|
||||
Dim c(1 To 2, 1 To 2) As Byte = {{1, 2}, {3, 4}}
|
||||
Print c(1, 1), c(2,2)
|
||||
Sleep
|
||||
48
Task/Collections/FutureBasic/collections.basic
Normal file
48
Task/Collections/FutureBasic/collections.basic
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
void local fn Array
|
||||
CFArrayRef array = @[@"Alpha",@"Bravo",@"Charlie",@"Delta"]
|
||||
NSLog(@"Array:%@\n",array)
|
||||
end fn
|
||||
|
||||
void local fn Dictionary
|
||||
CFDictionaryRef dict = @{@"Key1":@"Value1",@"Key2":@"Value2",@"Key3":@"Value3"}
|
||||
NSLog(@"Dictionary:%@\n",dict)
|
||||
end fn
|
||||
|
||||
void local fn Set
|
||||
CFSetRef set = fn SetWithArray( @[@"Echo",@"Echo",@"FutureBasic",@"Golf",@"Hotel",@"India"] )
|
||||
NSLog(@"Set:%@\n",set)
|
||||
end fn
|
||||
|
||||
void local fn IndexPath
|
||||
long indexes(3)
|
||||
indexes(0) = 1 : indexes(1) = 4 : indexes(2) = 3 : indexes(3) = 2
|
||||
IndexPathRef indexPath = fn IndexPathWithIndexes( @indexes(0), 4 )
|
||||
NSLog(@"IndexPath:%@\n",indexPath)
|
||||
end fn
|
||||
|
||||
void local fn IndexSet
|
||||
IndexSetRef indexSet = fn IndexSetWithIndexesInRange( fn CFRangeMake( 12, 5 ) )
|
||||
NSLog(@"IndexSet:%@\n",indexSet)
|
||||
end fn
|
||||
|
||||
void local fn CountedSet
|
||||
CountedSetRef countedSet = fn CountedSetWithArray( @[@"Juliet",@"Lima",@"Mike",@"Lima",@"Kilo",@"Lima",@"Juliet",@"Mike",@"Lima"] )
|
||||
NSLog(@"CountedSet:%@\n",countedSet)
|
||||
end fn
|
||||
|
||||
void local fn OrderedSet
|
||||
OrderedSetRef orderedSet = fn OrderedSetWithObjects( @"November", @"Oscar", @"Papa", NULL )
|
||||
NSLog(@"OrderedSet:%@\n",orderedSet)
|
||||
end fn
|
||||
|
||||
fn Array
|
||||
fn Dictionary
|
||||
fn Set
|
||||
fn IndexPath
|
||||
fn IndexSet
|
||||
fn CountedSet
|
||||
fn OrderedSet
|
||||
|
||||
HandleEvents
|
||||
10
Task/Collections/Gambas/collections.gambas
Normal file
10
Task/Collections/Gambas/collections.gambas
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
Public Sub Main()
|
||||
Dim siCount As Short
|
||||
Dim cCollection As Collection = ["0": "zero", "1": "one", "2": "two", "3": "three", "4": "four",
|
||||
"5": "five", "6": "six", "7": "seven", "8": "eight", "9": "nine"]
|
||||
|
||||
For siCount = 0 To 9
|
||||
Print cCollection[Str(siCount)]
|
||||
Next
|
||||
|
||||
End
|
||||
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)
|
||||
}
|
||||
21
Task/Collections/Groovy/collections-1.groovy
Normal file
21
Task/Collections/Groovy/collections-1.groovy
Normal 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])
|
||||
24
Task/Collections/Groovy/collections-2.groovy
Normal file
24
Task/Collections/Groovy/collections-2.groovy
Normal 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])
|
||||
16
Task/Collections/Groovy/collections-3.groovy
Normal file
16
Task/Collections/Groovy/collections-3.groovy
Normal 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])
|
||||
1
Task/Collections/Haskell/collections-1.hs
Normal file
1
Task/Collections/Haskell/collections-1.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
[1, 2, 3, 4, 5]
|
||||
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]]
|
||||
19
Task/Collections/Haskell/collections-5.hs
Normal file
19
Task/Collections/Haskell/collections-5.hs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import Data.Array (Array, listArray, Ix, (!))
|
||||
|
||||
triples :: Array Int (Char, String, String)
|
||||
triples =
|
||||
listArray (0, 11) $
|
||||
zip3
|
||||
"鼠牛虎兔龍蛇馬羊猴鸡狗豬" -- 生肖 shengxiao – symbolic animals
|
||||
(words "shǔ niú hǔ tù lóng shé mǎ yáng hóu jī gǒu zhū")
|
||||
(words "rat ox tiger rabbit dragon snake horse goat monkey rooster dog pig")
|
||||
|
||||
indexedItem
|
||||
:: Ix i
|
||||
=> Array i (Char, String, String) -> i -> String
|
||||
indexedItem a n =
|
||||
let (c, w, w1) = a ! n
|
||||
in c : unwords ["\t", w, w1]
|
||||
|
||||
main :: IO ()
|
||||
main = (putStrLn . unlines) $ indexedItem triples <$> [2, 4, 6]
|
||||
20
Task/Collections/Haskell/collections-6.hs
Normal file
20
Task/Collections/Haskell/collections-6.hs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import qualified Data.Map as M
|
||||
import Data.Maybe (isJust)
|
||||
|
||||
mapSample :: M.Map String Int
|
||||
mapSample =
|
||||
M.fromList
|
||||
[ ("alpha", 1)
|
||||
, ("beta", 2)
|
||||
, ("gamma", 3)
|
||||
, ("delta", 4)
|
||||
, ("epsilon", 5)
|
||||
, ("zeta", 6)
|
||||
]
|
||||
|
||||
maybeValue :: String -> Maybe Int
|
||||
maybeValue = flip M.lookup mapSample
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
print $ sequence $ filter isJust (maybeValue <$> ["beta", "delta", "zeta"])
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue