Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,6 @@
{{clarified-review}}Collections are abstractions to represent sets of values. In statically-typed languages, the values are typically of a common data type.
{{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.

View file

@ -1,2 +1,4 @@
---
category:
- Data Structures
note: Basic language learning

View file

@ -4,7 +4,7 @@ import scala.collection.immutable.HashSet;
import scala.collection.mutable.ArrayBuffer;
public class Collections {
public static void main(String[] args) {
ArrayBuffer<Integer> myarrlist = new ArrayBuffer<Integer>();
ArrayBuffer<Integer> myarrlist2 = new ArrayBuffer<Integer>(20);
@ -13,9 +13,6 @@ public class Collections {
myarrlist.$plus$eq(13); // to add an element.
myarrlist.$plus$eq(-1);
myarrlist2.$plus$plus$eq(myarrlist);// //$plus$plus$eq is Scala ++= operator
myarrlist2.$plus$plus$eq(myarrlist);
myarrlist2 = (ArrayBuffer<Integer>) myarrlist2.$minus(-1);
for (int i = 0; i < 10; i++)

View file

@ -0,0 +1,5 @@
L1 := [3, 4, 5, 6];
L1 := [3, 4, 5, 6]
L2 := [7, 8, 9];
L2 := [7, 8, 9]

View file

@ -0,0 +1,2 @@
[op(L1), op(L2)]
[3, 4, 5, 6, 7, 8, 9]

View file

@ -0,0 +1,2 @@
A1 := Array([3, 4, 5, 6]);
A1 := [3, 4, 5, 6]

View file

@ -0,0 +1,2 @@
ArrayTools:-Append(A1, 7);
A1 := [3, 4, 5, 6, 7]

View file

@ -1,5 +1,4 @@
# creating an empty array and adding values
a = [] #=> []
a[0] = 1 #=> [1]
a[3] = "abc" #=> [1, nil, nil, "abc"]

View file

@ -1,5 +1,4 @@
# creating an empty hash
h = {} #=> {}
h["a"] = 1 #=> {"a"=>1}
h["test"] = 2.4 #=> {"a"=>1, "test"=>2.4}

View file

@ -0,0 +1,21 @@
require 'set'
# different ways of creating a set
p s1 = Set[1, 2, 3, 4] #=> #<Set: {1, 2, 3, 4}>
p s2 = [8, 6, 4, 2].to_set #=> #<Set: {8, 6, 4, 2}>
p s3 = Set.new(1..4) {|x| x*2} #=> #<Set: {2, 4, 6, 8}>
# Union
p s1 | s2 #=> #<Set: {1, 2, 3, 4, 8, 6}>
# Intersection
p s1 & s2 #=> #<Set: {4, 2}>
# Difference
p s1 - s2 #=> #<Set: {1, 3}>
p s1 ^ s2 #=> #<Set: {8, 6, 1, 3}>
p s2 == s3 #=> true
p s1.add(5) #=> #<Set: {1, 2, 3, 4, 5}>
p s1 << 0 #=> #<Set: {1, 2, 3, 4, 5, 0}>
p s1.delete(3) #=> #<Set: {1, 2, 4, 5, 0}>

View file

@ -0,0 +1,24 @@
require 'matrix'
# creating a matrix
p m0 = Matrix.zero(3) #=> Matrix[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
p m1 = Matrix.identity(3) #=> Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
p m2 = Matrix[[11, 12], [21, 22]]
#=> Matrix[[11, 12], [21, 22]]
p m3 = Matrix.build(3) {|row, col| row - col}
#=> Matrix[[0, -1, -2], [1, 0, -1], [2, 1, 0]]
p m2[0,0] #=> 11
p m1 * 5 #=> Matrix[[5, 0, 0], [0, 5, 0], [0, 0, 5]]
p m1 + m3 #=> Matrix[[1, -1, -2], [1, 1, -1], [2, 1, 1]]
p m1 * m3 #=> Matrix[[0, -1, -2], [1, 0, -1], [2, 1, 0]]
# creating a Vector
p v1 = Vector[1,3,5] #=> Vector[1, 3, 5]
p v2 = Vector[0,1,2] #=> Vector[0, 1, 2]
p v1[1] #=> 3
p v1 * 2 #=> Vector[2, 6, 10]
p v1 + v2 #=> Vector[1, 4, 7]
p m1 * v1 #=> Vector[1, 3, 5]
p m3 * v1 #=> Vector[-13, -4, 5]

View file

@ -0,0 +1,23 @@
require 'ostruct'
# creating a OpenStruct
ab = OpenStruct.new
p ab #=> #<OpenStruct>
ab.foo = 25
p ab.foo #=> 25
ab[:bar] = 2
p ab["bar"] #=> 2
p ab #=> #<OpenStruct foo=25, bar=2>
ab.delete_field("foo")
p ab.foo #=> nil
p ab #=> #<OpenStruct bar=2>
p son = OpenStruct.new({ :name => "Thomas", :age => 3 })
#=> #<OpenStruct name="Thomas", age=3>
p son.name #=> "Thomas"
p son[:age] #=> 3
son.age += 1
p son.age #=> 4
son.items = ["candy","toy"]
p son.items #=> ["candy","toy"]
p son #=> #<OpenStruct name="Thomas", age=4, items=["candy", "toy"]

View file

@ -0,0 +1,30 @@
import collection.concurrent.TrieMap
// super concurrent mutable hashmap
val map = TrieMap("Amsterdam" -> "Netherlands",
"New York" -> "USA",
"Heemstede" -> "Netherlands")
map("Laussanne") = "Switzerland" // 2 Ways of updating
map += ("Tokio" -> "Japan")
assert(map("New York") == "USA")
assert(!map.isDefinedAt("Gent")) // isDefinedAt is false
assert(map.isDefinedAt("Laussanne")) // true
val hash = new TrieMap[Int, Int]
hash(1) = 2
hash += (1 -> 2) // same as hash(1) = 2
hash += (3 -> 4, 5 -> 6, 44 -> 99)
hash(44) // 99
hash.contains(33) // false
hash.isDefinedAt(33) // same as contains
hash.contains(44) // true
// iterate over key/value
// hash.foreach { case (key, val) => println( "key " + e._1 + " value " + e._2) } // e is a 2 element Tuple
// same with for syntax
for ((k, v) <- hash) println("key " + k + " value " + v)
// // items in map where the key is greater than 3
map.filter { k => k._1 > 3 } // Map(5 -> 6, 44 -> 99)
// // same with for syntax
for ((k, v) <- map; if k > 3) yield (k, v)

View file

@ -1,2 +1 @@
(1 2 3)
()
(cons obj lst)

View file

@ -1 +1,2 @@
(cons obj lst)
(display (cons 0 (list 1 2 3)))
(newline)

View file

@ -1,2 +1 @@
(display (cons 0 (list 1 2 3)))
(newline)
(append lst ...)

View file

@ -1 +1,2 @@
(0 1 2 3)
(display (append (list 1 2 3) (list 4 5 6)))
(newline)

View file

@ -0,0 +1,9 @@
a_index=(one two three) # create an array with a few elements
a_index+=(four five) # append some elements
a_index[9]=ten # add a specific index
for elem in "${a_index[@]}"; do # interate over the elements
echo "$elem"
done
for idx in "${!a_index[@]}"; do # interate over the array indices
printf "%d\t%s\n" $idx "${a_index[idx]}"
done

View file

@ -0,0 +1,9 @@
declare -A a_assoc=([one]=1 [two]=2 [three]=3) # create an array with a few elements
a_assoc+=([four]=4 [five]=5) # add some elements
a_assoc[ten]=10
for value in "${a_assoc[@]}"; do # interate over the values
echo "$value"
done
for key in "${!a_assoc[@]}"; do # interate over the array indices
printf "%s\t%s\n" "$key" "${a_assoc[$key]}"
done

View file

@ -0,0 +1 @@
declare -A

View file

@ -0,0 +1 @@
typeset -A