Skip to content

Commit d6e3b94

Browse files
committed
New snapshot for Early Release editions.
1 parent af76e0c commit d6e3b94

File tree

471 files changed

+2278
-1575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

471 files changed

+2278
-1575
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## README for the Code Examples ##
44

55
*Dean Wampler*<br/>
6-
*July 24, 2014*
6+
*August 7, 2014*
77

88
This archive contains all the code examples found in [Programming Scala, Second Edition](http://shop.oreilly.com/product/0636920033073.do), with the exception of some trivial code snippets in the text. There are also some examples in this distribution that aren't actually in the book. In the book's text, when an example corresponds to a file in this distribution, the listing begins with a path in a comment with the following format:
99

build.sbt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@ crossScalaVersions := Seq("2.11.2", "2.10.4")
1313
// The XML and parser combinator support are now separate jars.
1414
// I use the if condition to conditionally add these extra dependencies.
1515
libraryDependencies ++= Seq(
16-
"com.typesafe.akka" %% "akka-actor" % "2.3.3",
17-
"org.scalaz" %% "scalaz-core" % "7.0.6",
18-
"org.scalacheck" %% "scalacheck" % "1.11.4" % "test",
19-
"org.scalatest" %% "scalatest" % "2.2.0" % "test",
20-
"org.specs2" %% "specs2" % "2.3.12" % "test",
16+
"org.scala-lang.modules" %% "scala-async" % "0.9.2",
17+
"com.typesafe.akka" %% "akka-actor" % "2.3.4",
18+
"com.typesafe.akka" %% "akka-slf4j" % "2.3.4",
19+
"ch.qos.logback" % "logback-classic" % "1.1.2",
20+
"org.scalaz" %% "scalaz-core" % "7.0.6",
21+
"org.scalacheck" %% "scalacheck" % "1.11.4" % "test",
22+
"org.scalatest" %% "scalatest" % "2.2.0" % "test",
23+
"org.specs2" %% "specs2" % "2.3.12" % "test",
2124
// JUnit is used for some Java interop. examples. A driver for JUnit:
22-
"junit" % "junit-dep" % "4.10" % "test",
23-
"com.novocode" % "junit-interface" % "0.10" % "test"
25+
"junit" % "junit-dep" % "4.10" % "test",
26+
"com.novocode" % "junit-interface" % "0.10" % "test"
2427
) ++ (
2528
if (scalaVersion.value startsWith "2.11")
2629
Seq(
2730
// Could use this to get everything:
28-
// "org.scala-lang.modules" %% "scala-library-all" % "2.11.1")
31+
// "org.scala-lang.modules" %% "scala-library-all" % "2.11.")
2932
"org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.1",
3033
"org.scala-lang.modules" %% "scala-xml" % "1.0.2")
3134
else Seq.empty)

misc/run-akka-input.txt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
h
2+
c 1 one
3+
c 2 two
4+
c 3 three
5+
c 4 four
6+
c 5 five
7+
c 6 six
8+
c 7 seven
9+
c 8 eight
10+
c 9 nine
11+
c 10 ten
12+
c 11 eleven
13+
c 12 twelve
14+
c 13 thirteen
15+
c 14 fourteen
16+
c 15 fifteen
17+
c 16 sixteen
18+
c 17 seventeen
19+
c 18 eighteen
20+
c 19 nineteen
21+
c 20 twenty
22+
dump
23+
dump 2
24+
25+
u 1 ONE
26+
u 2 TWO
27+
u 3 THREE
28+
u 4 FOUR
29+
u 5 FIVE
30+
u 6 SIX
31+
u 7 SEVEN
32+
u 8 EIGHT
33+
u 9 NINE
34+
u 10 TEN
35+
dump
36+
37+
r 1
38+
r 3
39+
r 11
40+
r 20
41+
42+
d 1
43+
d 3
44+
d 11
45+
d 20
46+
dump
47+
48+
r 1
49+
r 3
50+
r 11
51+
r 20
52+
53+
crash 1
54+
dump
55+
56+
r 1
57+
r 2
58+
r 3
59+
r 4
60+
r 5
61+
r 6
62+
r 7
63+
r 8
64+
r 9
65+
r 10
66+
dump

src/main/java/JavaInterop/ScalaFunctions8.javaX

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/main/java/AdvOOP/objects/PersonUser.javaX renamed to src/main/java/progscala2/advoop/objects/PersonUser.javaX

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// src/main/java/AdvOOP/objects/PersonUser.javaX
1+
// src/main/java/progscala2/advoop/objects/PersonUser.javaX
22
// WON'T COMPILE
33

4-
package objects;
4+
package progscala2.advoop.objects;
55

66
public class PersonUserWontCompile {
77
public static void main(String[] args) {

src/main/java/AppDesign/annotations/FilePrinterMain.java renamed to src/main/java/progscala2/appdesign/annotations/FilePrinterMain.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// src/main/java/AppDesign/annotations/FilePrinterMain.java
1+
// src/main/java/progscala2/appdesign/annotations/FilePrinterMain.java
22

3-
package appdesign.annotations;
3+
package progscala2.appdesign.annotations;
44

55
import java.io.*;
66

src/main/java/AppDesign/annotations/Pre.java renamed to src/main/java/progscala2/appdesign/annotations/Pre.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// src/main/java/AppDesign/annotations/Pre.java
1+
// src/main/java/progscala2/appdesign/annotations/Pre.java
22

33
package org.contract4j5.contract;
44
import java.lang.annotation.Documented;

src/main/java/BasicOOP/Person.java renamed to src/main/java/progscala2/basicoop/JPerson.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// src/main/java/BasicOOP/Person.java
1+
// src/main/java/progscala2/basicoop/JPerson.java
22

3-
package BasicOOP;
3+
package progscala2.basicoop;
44

5-
public class Person {
5+
public class JPerson {
66
private String name;
77
private int age;
88

9-
public Person(String name, int age) {
9+
public JPerson(String name, int age) {
1010
this.name = name;
1111
this.age = age;
1212
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// src/main/java/bigdata/CascadingWordCount.javaX <1>
2+
/*
3+
* Adapted from "Cascading for the Impatient", Exercise 2:
4+
* http://docs.cascading.org/impatient/
5+
* Copyright (c) 2007-2013 Concurrent, Inc. All Rights Reserved.
6+
* See that project for additional copyright information.
7+
*/
8+
9+
package impatient;
10+
11+
import java.util.Properties;
12+
13+
import cascading.flow.Flow;
14+
import cascading.flow.FlowDef;
15+
import cascading.flow.hadoop.HadoopFlowConnector;
16+
import cascading.operation.aggregator.Count;
17+
import cascading.operation.regex.RegexSplitGenerator;
18+
import cascading.pipe.Each;
19+
import cascading.pipe.Every;
20+
import cascading.pipe.GroupBy;
21+
import cascading.pipe.Pipe;
22+
import cascading.property.AppProps;
23+
import cascading.scheme.hadoop.TextDelimited;
24+
import cascading.tap.Tap;
25+
import cascading.tap.hadoop.Hfs;
26+
import cascading.tuple.Fields;
27+
28+
public class CascadingWordCount {
29+
public static void main( String[] args ) {
30+
String input = args[0];
31+
String outpu = args[1];
32+
33+
Properties properties = new Properties(); // <2>
34+
AppProps.setApplicationJarClass( properties, Main.class );
35+
HadoopFlowConnector flowConnector = new HadoopFlowConnector( properties );
36+
37+
// Create source and sink taps.
38+
Tap docTap = new Hfs( new TextDelimited( true, "\t" ), input ); // <3>
39+
Tap wcTap = new Hfs( new TextDelimited( true, "\t" ), output );
40+
41+
// Specify a regex operation to split the "document" text lines
42+
// into a token stream.
43+
Fields token = new Fields( "token" ); // <4>
44+
Fields text = new Fields( "text" );
45+
RegexSplitGenerator splitter =
46+
new RegexSplitGenerator( token, "[ \\[\\]\\(\\),.]" );
47+
// Only returns "token"
48+
Pipe docPipe = // <5>
49+
new Each( "token", text, splitter, Fields.RESULTS );
50+
51+
// Determine the word counts.
52+
Pipe wcPipe = new Pipe( "wc", docPipe ); // <6>
53+
wcPipe = new GroupBy( wcPipe, token );
54+
wcPipe = new Every( wcPipe, Fields.ALL, new Count(), Fields.ALL );
55+
56+
// Connect the taps, pipes, etc., into a flow.
57+
FlowDef flowDef = FlowDef.flowDef() // <7>
58+
.setName( "wc" )
59+
.addSource( docPipe, docTap )
60+
.addTailSink( wcPipe, wcTap );
61+
62+
// Run the flow.
63+
Flow wcFlow = flowConnector.connect( flowDef ); // <8>
64+
wcFlow.complete();
65+
}
66+
}
67+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// src/main/java/bigdata/HadoopWordCount.javaX <1>
2+
3+
package bigdata;
4+
import org.apache.hadoop.io.*;
5+
import org.apache.hadoop.mapred.*;
6+
import java.util.StringTokenizer;
7+
8+
public class HadoopWordCount {
9+
10+
public static void main(String[] args) {
11+
String input = args[0];
12+
String output = args[1];
13+
14+
JobClient client = new JobClient();
15+
JobConf conf = new JobConf(WordCount.class);
16+
17+
conf.setJobName("WordCount");
18+
conf.setOutputKeyClass(Text.class);
19+
conf.setOutputValueClass(IntWritable.class);
20+
FileInputFormat.addInputPath(conf, new Path(input));
21+
FileOutputFormat.setOutputPath(conf, new Path(output));
22+
conf.setMapperClass(WordCountMapper.class);
23+
conf.setReducerClass(WordCountReducer.class);
24+
25+
client.setConf(conf);
26+
27+
try {
28+
JobClient.runJob(conf);
29+
} catch (Exception e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
class WordCountMapper extends MapReduceBase
35+
implements Mapper<IntWritable, Text, Text, IntWritable> {
36+
37+
static final IntWritable one = new IntWritable(1);
38+
// Value will be set in a non-thread-safe way!
39+
static final Text word = new Text;
40+
41+
@Override
42+
public void map(IntWritable key, Text valueDocContents,
43+
OutputCollector<Text, IntWritable> output, Reporter reporter) {
44+
String[] tokens = valueDocContents.toString.split("\\s+"); // <2>
45+
for (String wordString: tokens) {
46+
if (wordString.length > 0) {
47+
word.set(wordString.toLowerCase);
48+
output.collect(word, one);
49+
}
50+
}
51+
}
52+
}
53+
54+
class WordCountReduce extends MapReduceBase
55+
implements Reducer<Text, IntWritable, Text, IntWritable> {
56+
57+
public void reduce(Text keyWord, java.util.Iterator<IntWritable> counts,
58+
OutputCollector<Text, IntWritable> output, Reporter reporter) {
59+
int totalCount = 0;
60+
while (counts.hasNext) { // <3>
61+
while (counts.hasNext) {
62+
totalCount += counts.next.get;
63+
}
64+
output.collect(keyWord, new IntWritable(totalCount));
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)