This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -1,3 +1,5 @@
{{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.
{{Template:See also lists}}

View file

@ -0,0 +1,53 @@
import scala.Tuple2;
import scala.collection.concurrent.TrieMap;
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);
myarrlist.$plus$eq(new Integer(42)); // $plus$eq is Scala += operator
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++)
myarrlist2.$plus$eq(i);
// loop through myarrlist to sum each entry
int sum = 0;
for (int i = 0; i < myarrlist2.size(); i++) {
sum += myarrlist2.apply(i);
}
System.out.println("List is: " + myarrlist2 + " with head: "
+ myarrlist2.head() + " sum is: " + sum);
System.out.println("Third element is: " + myarrlist2.apply$mcII$sp(2));
Tuple2<String, String> tuple = new Tuple2<String, String>("US",
"Washington");
System.out.println("Tuple2 is : " + tuple);
ArrayBuffer<Tuple2<String, String>> capList = new ArrayBuffer<Tuple2<String, String>>();
capList.$plus$eq(new Tuple2<String, String>("US", "Washington"));
capList.$plus$eq(new Tuple2<String, String>("France", "Paris"));
System.out.println(capList);
TrieMap<String, String> trieMap = new TrieMap<String, String>();
trieMap.put("US", "Washington");
trieMap.put("France", "Paris");
HashSet<Character> set = new HashSet<Character>();
ArrayBuffer<Tuple2<String, String>> capBuffer = new ArrayBuffer<Tuple2<String, String>>();
trieMap.put("US", "Washington");
System.out.println(trieMap);
}
}

View file

@ -1,2 +1,16 @@
# Array
my @array = 1,2,3;
@array.push: 4,5,6;
# Hash
my %hash = a => 1, b => 2;
%hash<c d> = 3,4;
%hash.push: e => 5, f => 6;
# KeySet
my $s = KeySet.new: <a b c>;
$s = <d e f>;
# KeyBag
my $b = KeyBag.new: <b a k l a v a>;
$b.push: <d e f>;

View file

@ -1,3 +1,11 @@
my %hash = a => 1, b => 2;
%hash<c d> = 3,4;
%hash.push: e => 5, f => 6;
# List
my @list := 1,2,3;
my @newlist := @list, 4,5,6;
# Set
my $set = set <a b c>;
my $newset = $set <d e f>;
# Bag
my $bag = bag <b a k l a v a>;
my $newbag = $bag <b e e f>;

View file

@ -1,2 +1,2 @@
my @list := 1,2,3;
my @newlist := @list, 4,5,6;
my $tail = d => e => f => Nil;
my $new = a => b => c => $tail;

View file

@ -1,2 +1,2 @@
my $set = set <a b c>;
my $newset = $set <d e f>;
my $obj = Something.new: foo => 1, bar => 2;
my $newobj = $obj but role { has $.baz = 3 } # anonymous mixin

View file

@ -13,19 +13,26 @@ 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*/
do j=1 for 10000 /*this method isn't very efficient*/
if pr.j==0 then iterate
primes=primes+1
primes = primes+1
end /*j*/
say '# of primes in list:' primes
/*─────────────────────────────────────────────────────────────────────*/
#primes=0
do j=1 for 10000 /*this method isn't very efficient*/
if pr.j\==0 then #primes = #primes+1
end /*j*/
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 #.*/
say '# of primes in list:' #primes
/*─────────────────────────────────────────────────────────────────────*/
s=0 /*yet another inefficient method. */
do k=1 for 10000 /*this method isn't very efficient*/
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

View file

@ -1,37 +1,58 @@
scala> // Immutable collections do not change the original object
scala> // A List
Windows PowerShell
Copyright (C) 2012 Microsoft Corporation. All rights reserved.
PS C:\Users\FransAdm> scala
Welcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> // Immutable collections do not and cannot change the instantiated object
scala> // Lets start with Lists
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 list2 = List("one", "two") // List with two elements (Strings)
list2: List[String] = List(one, two)
scala> val list3 = 3 :: list2 // prepend 3 to list2, using a special operator
list3: List[Int] = List(3, 1, 2)
scala> // A Set
list3: List[Any] = List(3, one, two)
scala> // The result was a mixture with a Int and Strings, so the common superclass Any is used.
scala> // Let test the Set collection
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)
scala> val set2 = set + 'a' + 'c' + 'c' // try to add another and the same element twice
set2: scala.collection.immutable.Set[Char] = Set(a, c)
scala> // Let's look at the most universal map: TrieMap (Cache-aware lock-free concurrent hash trie)
scala> val capital = collection.concurrent.TrieMap("US" -> "Washington", "France" -> "Paris") // This map is mutable
capital: scala.collection.concurrent.TrieMap[String,String] = TrieMap(US -> Washington, France -> Paris)
scala> capital - "France" // This is only an expression, does not modify the map itself
res0: scala.collection.concurrent.TrieMap[String,String] = TrieMap(US -> Washington)
scala> capital += ("Tokio" -> "Japan") // Adding an element, object is changed - not the val capital
res1: capital.type = TrieMap(US -> Washington, Tokio -> Japan, France -> Paris)
scala> capital // Check what we have sofar
res2: scala.collection.concurrent.TrieMap[String,String] = TrieMap(US -> Washington, Tokio -> Japan, France -> Paris)
scala> val queue = new scala.collection.mutable.Queue[String]
queue: scala.collection.mutable.Queue[String] = Queue()
scala> queue += "first"
res17: queue.type = Queue("first")
scala> queue += "second"
res19: queue.type = Queue("first", "second")
scala>