Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,9 @@
|
|||
// Cannot / Do not need to instantiate the algorithm implementation (e.g, HashMap).
|
||||
Map<String, String> strMap = new Map<String, String>();
|
||||
strMap.put('a', 'aval');
|
||||
strMap.put('b', 'bval');
|
||||
|
||||
System.assert( strMap.containsKey('a') );
|
||||
System.assertEquals( 'bval', strMap.get('b') );
|
||||
// String keys are case-sensitive
|
||||
System.assert( !strMap.containsKey('A') );
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
Map<String, String> strMap = new Map<String, String>{
|
||||
'a' => 'aval',
|
||||
'b' => 'bval'
|
||||
};
|
||||
|
||||
System.assert( strMap.containsKey('a') );
|
||||
System.assertEquals( 'bval', strMap.get('b') );
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
import ceylon.collection {
|
||||
|
||||
ArrayList,
|
||||
HashMap,
|
||||
naturalOrderTreeMap
|
||||
}
|
||||
|
||||
shared void run() {
|
||||
|
||||
// the easiest way is to use the map function to create
|
||||
// an immutable map
|
||||
value myMap = map {
|
||||
"foo" -> 5,
|
||||
"bar" -> 10,
|
||||
"baz" -> 15,
|
||||
"foo" -> 6 // by default the first "foo" will remain
|
||||
};
|
||||
|
||||
// or you can use the HashMap constructor to create
|
||||
// a mutable one
|
||||
value myOtherMap = HashMap {
|
||||
"foo"->"bar"
|
||||
};
|
||||
myOtherMap.put("baz", "baxx");
|
||||
|
||||
// there's also a sorted red/black tree map
|
||||
value myTreeMap = naturalOrderTreeMap {
|
||||
1 -> "won",
|
||||
2 -> "too",
|
||||
4 -> "fore"
|
||||
};
|
||||
for(num->homophone in myTreeMap) {
|
||||
print("``num`` is ``homophone``");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
(lib 'hash) ;; needs hash.lib
|
||||
(define H (make-hash)) ;; new hash table
|
||||
;; keys may be symbols, numbers, strings ..
|
||||
;; values may be any lisp object
|
||||
(hash-set H 'simon 'antoniette)
|
||||
→ antoniette
|
||||
(hash-set H 'antoinette 'albert)
|
||||
→ albert
|
||||
(hash-set H "Elvis" 42)
|
||||
→ 42
|
||||
(hash-ref H 'Elvis)
|
||||
→ #f ;; not found. Elvis is not "Elvis"
|
||||
(hash-ref H "Elvis")
|
||||
→ 42
|
||||
(hash-ref H 'simon)
|
||||
→ antoniette
|
||||
(hash-count H)
|
||||
→ 3
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
arr := { => }
|
||||
arr[ 10 ] := "Val_10"
|
||||
arr[ "foo" ] := "foovalue"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
arr := hb_Hash( 10, "Val_10", "foo", "foovalue" )
|
||||
// or
|
||||
arr := { 10 => "Val_10", "foo" => "foovalue" }
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(let* ((my-dict (: dict new))
|
||||
(my-dict (: dict store 'key-1 '"value 1" my-dict))
|
||||
(my-dict (: dict store 'key-2 '"value 2" my-dict)))
|
||||
(: io format '"size: ~p~n" (list (: dict size my-dict)))
|
||||
(: io format '"some data: ~p~n" (list (: dict fetch 'key-1 my-dict))))
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
// In Lasso associative arrays are called maps
|
||||
|
||||
// Define an empty map
|
||||
local(mymap = map)
|
||||
|
||||
// Define a map with content
|
||||
local(mymap = map(
|
||||
'one' = 'Monday',
|
||||
'2' = 'Tuesday',
|
||||
3 = 'Wednesday'
|
||||
))
|
||||
|
||||
// add elements to an existing map
|
||||
#mymap -> insert('fourth' = 'Thursday')
|
||||
|
||||
// retrieve a value from a map
|
||||
#mymap -> find('2') // Tuesday
|
||||
'<br />'
|
||||
#mymap -> find(3) // Wednesday, found by the key not the position
|
||||
'<br />'
|
||||
|
||||
// Get all keys from a map
|
||||
#mymap -> keys // staticarray(2, fourth, one, 3)
|
||||
'<br />'
|
||||
|
||||
// Iterate thru a map and get values
|
||||
with v in #mymap do {^
|
||||
#v
|
||||
'<br />'
|
||||
^}
|
||||
// Tuesday<br />Thursday<br />Monday<br />Wednesday<br />
|
||||
|
||||
// Perform actions on each value of a map
|
||||
#mymap -> foreach => {
|
||||
#1 -> uppercase
|
||||
#1 -> reverse
|
||||
}
|
||||
#mymap // map(2 = YADSEUT, fourth = YADSRUHT, one = YADNOM, 3 = YADSENDEW)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
props = [#key1: "value1", #key2: "value2"]
|
||||
|
||||
put props[#key2]
|
||||
-- "value2"
|
||||
put props["key2"]
|
||||
-- "value2"
|
||||
put props.key2
|
||||
-- "value2"
|
||||
put props.getProp(#key2)
|
||||
-- "value2"
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
command assocArray
|
||||
local tArray
|
||||
put "value 1" into tArray["key 1"]
|
||||
put 123 into tArray["key numbers"]
|
||||
put "a,b,c" into tArray["abc"]
|
||||
|
||||
put "number of elements:" && the number of elements of tArray & return & \
|
||||
"length of item 3:" && the length of tArray["abc"] & return & \
|
||||
"keys:" && the keys of tArray
|
||||
end assocArray
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
number of elements: 3
|
||||
length of item 3: 5
|
||||
keys: key numbers
|
||||
abc
|
||||
key 1
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import tables
|
||||
|
||||
var
|
||||
hash = initTable[string, int]() # empty hash table
|
||||
hash2 = {"key1": 1, "key2": 2}.toTable # hash table with two keys
|
||||
hash3 = [("key1", 1), ("key2", 2)].toTable # hash table from tuple array
|
||||
hash4 = @[("key1", 1), ("key2", 2)].toTable # hash table from tuple seq
|
||||
value = hash2["key1"]
|
||||
|
||||
hash["spam"] = 1
|
||||
hash["eggs"] = 2
|
||||
hash.add("foo", 3)
|
||||
|
||||
echo "hash has ", hash.len, " elements"
|
||||
echo "hash has key foo? ", hash.hasKey("foo")
|
||||
echo "hash has key bar? ", hash.hasKey("bar")
|
||||
|
||||
echo "iterate pairs:" # iterating over (key, value) pairs
|
||||
for key, value in hash:
|
||||
echo key, ": ", value
|
||||
|
||||
echo "iterate keys:" # iterating over keys
|
||||
for key in hash.keys:
|
||||
echo key
|
||||
|
||||
echo "iterate values:" # iterating over values
|
||||
for key in hash.values:
|
||||
echo key
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
setd("one",1)
|
||||
setd(2,"duo")
|
||||
setd({3,4},{5,"six"})
|
||||
?getd("one") -- shows 1
|
||||
?getd({3,4}) -- shows {5,"six"}
|
||||
?getd(2) -- shows "duo"
|
||||
deld(2)
|
||||
?getd(2) -- shows 0
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
mydictionary = (red=0xff0000, green=0x00ff00, blue=0x0000ff)
|
||||
|
||||
redblue = "purple"
|
||||
mydictionary put(redblue, 0xff00ff)
|
||||
|
||||
255 == mydictionary("blue")
|
||||
65280 == mydictionary("green")
|
||||
16711935 == mydictionary("purple")
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
var hash = Hash.new(
|
||||
key1 => 'value1',
|
||||
key2 => 'value2',
|
||||
);
|
||||
|
||||
# Add a new key-value pair
|
||||
hash{:key3} = 'value3';
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
// make an empty map
|
||||
var a = [String: Int]()
|
||||
// or
|
||||
var b: [String: Int] = [:]
|
||||
|
||||
// make an empty map with an initial capacity
|
||||
var c = [String: Int](minimumCapacity: 42)
|
||||
|
||||
// set a value
|
||||
c["foo"] = 3
|
||||
|
||||
// make a map with a literal
|
||||
var d = ["foo": 2, "bar": 42, "baz": -1]
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
LOCAL loCol As Collection, k, n, o
|
||||
CLEAR
|
||||
*!* Example using strings
|
||||
loCol = NEWOBJECT("Collection")
|
||||
loCol.Add("Apples", "A")
|
||||
loCol.Add("Oranges", "O")
|
||||
loCol.Add("Pears", "P")
|
||||
n = loCol.Count
|
||||
? "Items:", n
|
||||
*!* Loop through the collection
|
||||
k = 1
|
||||
FOR EACH o IN loCol FOXOBJECT
|
||||
? o, loCol.GetKey(k)
|
||||
k = k + 1
|
||||
ENDFOR
|
||||
*!* Get an item by its key
|
||||
? loCol("O")
|
||||
?
|
||||
*!* Example using objects
|
||||
LOCAL loFruits As Collection
|
||||
loFruits = NEWOBJECT("Collection")
|
||||
loFruits.Add(CREATEOBJECT("fruit", "Apples"), "A")
|
||||
loFruits.Add(CREATEOBJECT("fruit", "Oranges"), "O")
|
||||
loFruits.Add(CREATEOBJECT("fruit", "Pears"), "P")
|
||||
*!* Loop through the collection
|
||||
k = 1
|
||||
FOR EACH o IN loFruits FOXOBJECT
|
||||
? o.Name, loFruits.GetKey(k)
|
||||
k = k + 1
|
||||
ENDFOR
|
||||
*!* Get an item name by its key
|
||||
? loFruits("P").Name
|
||||
|
||||
|
||||
DEFINE CLASS fruit As Custom
|
||||
PROCEDURE Init(tcName As String)
|
||||
THIS.Name = tcName
|
||||
ENDPROC
|
||||
ENDDEFINE
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
h <- (table 'a 1 'b 2)
|
||||
h 'a
|
||||
=> 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
(define starlings (make-table))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(table-set! starlings "Common starling" "Sturnus vulgaris")
|
||||
(table-set! starlings "Abbot's starling" "Poeoptera femoralis")
|
||||
(table-set! starlings "Cape starling" "Lamprotornis nitens")
|
||||
|
|
@ -0,0 +1 @@
|
|||
(table-ref starlings "Cape starling")
|
||||
|
|
@ -0,0 +1 @@
|
|||
(map-over-table-entries starlings (lambda (x y) (print (string-append x " (Linnaean name " y ")"))))
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
# An empty object:
|
||||
{}
|
||||
|
||||
# Its type:
|
||||
{} | type
|
||||
# "object"
|
||||
|
||||
# An object literal:
|
||||
{"a": 97, "b" : 98}
|
||||
|
||||
# Programmatic object construction:
|
||||
reduce ("a", "b", "c", "d") as $c ({}; . + { ($c) : ($c|explode[.0])} )
|
||||
# {"a":97,"c":99,"b":98,"d":100}
|
||||
|
||||
# Same as above:
|
||||
reduce range (97;101) as $i ({}; . + { ([$i]|implode) : $i })
|
||||
|
||||
# Addition of a key/value pair by assignment:
|
||||
{}["A"] = 65 # in this case, the object being added to is {}
|
||||
|
||||
# Alteration of the value of an existing key:
|
||||
{"A": 65}["A"] = "AA"
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
def collisionless:
|
||||
if type == "object" then with_entries(.value = (.value|collisionless))|tostring
|
||||
elif type == "array" then map(collisionless)|tostring
|
||||
else (type[0:1] + tostring)
|
||||
end;
|
||||
|
||||
# WARNING: addKey(key;value) will erase any previous value associated with key
|
||||
def addKey(key;value):
|
||||
if type == "object" then . + { (key|collisionless): value }
|
||||
else {} | addKey(key;value)
|
||||
end;
|
||||
|
||||
def getKey(key): .[key|collisionless];
|
||||
|
||||
def removeKey(key): delpaths( [ [key|collisionless] ] );
|
||||
|
|
@ -0,0 +1 @@
|
|||
{} | addKey(1;"one") | addKey(2; "two") | removeKey(1) | getKey(2)
|
||||
|
|
@ -0,0 +1 @@
|
|||
"two"
|
||||
Loading…
Add table
Add a link
Reference in a new issue