File tree Expand file tree Collapse file tree 3 files changed +57
-32
lines changed Expand file tree Collapse file tree 3 files changed +57
-32
lines changed Original file line number Diff line number Diff line change 1- def swap (a : Array [Char ], i : Int , j : Int ) {
1+ def swap (a : Array [Char ], i : Int , j : Int ): Unit =
22 val saved = a(i)
33 a(i) = a(j)
44 a(j) = saved
5- }
65
7- def generatePermutations (a : Array [Char ], n : Int ) {
8- if ( n == 0 ) {
6+ def generatePermutations (a : Array [Char ], n : Int ): Unit =
7+ if n == 0 then
98 println(String .valueOf(a))
10- } else {
11- for ( i <- 0 until n) {
9+ else
10+ for i <- 0 until n do
1211 generatePermutations(a, n- 1 )
13- swap(a, if (n % 2 == 0 ) 0 else i, n)
14- }
12+ swap(a, if n % 2 == 0 then 0 else i, n)
1513 generatePermutations(a, n- 1 )
16- }
17- }
1814
19- if (args.length != 1 ) {
20- Console .err.println( " Exactly one argument is required " )
21- sys.exit( 1 )
22- }
23- var word = args(0 )
24- generatePermutations(word.toCharArray, word.length- 1 )
15+ @ main def anagrams (args : String * ) =
16+ if args.length != 1 then
17+ Console .err.println( " Exactly one argument is required " )
18+ sys.exit( 1 )
19+ var word = args(0 )
20+ generatePermutations(word.toCharArray, word.length- 1 )
Original file line number Diff line number Diff line change 1- abstract class Animal (name : String ) {
1+ trait Animal (name : String ):
22 def speak = s " $name says $sound"
33 def sound : String
4- }
54
6- class Cow (name : String ) extends Animal (name) {
7- override def sound () = " moooo"
8- }
5+ class Cow (name : String ) extends Animal (name):
6+ def sound = " moooo"
97
10- class Horse (name : String ) extends Animal (name) {
11- override def sound () = " neigh"
12- }
8+ class Horse (name : String ) extends Animal (name):
9+ def sound = " neigh"
1310
14- class Sheep (name : String ) extends Animal (name) {
15- override def sound () = " baaaa"
16- }
11+ class Sheep (name : String ) extends Animal (name):
12+ def sound = " baaaa"
1713
18- var h = new Horse (" CJ" )
19- assert(h.speak == " CJ says neigh" )
20- var c = new Cow (" Bessie" )
21- assert(c.speak == " Bessie says moooo" )
22- assert(new Sheep (" Little Lamb" ).speak == " Little Lamb says baaaa" )
14+ @ main def animals () =
15+ var h = new Horse (" CJ" )
16+ assert(h.speak == " CJ says neigh" )
17+ var c = new Cow (" Bessie" )
18+ assert(c.speak == " Bessie says moooo" )
19+ assert(new Sheep (" Little Lamb" ).speak == " Little Lamb says baaaa" )
Original file line number Diff line number Diff line change 1+ function Assert-MatchTests {
2+ param (
3+ [Parameter (Mandatory = $true , ValueFromPipeline )] $TestResult
4+ )
5+
6+ if ($TestResult ) {
7+ Write-Error " Output does not match expected results."
8+ }
9+ }
10+
11+ $Error.clear ()
12+ scala " $PSScriptRoot \anagrams.scala" -- rats |
13+ Compare-Object (Get-Content " $PSScriptRoot \..\test\rats_heap_expected" ) |
14+ Assert-MatchTests &&
15+ scala " $PSScriptRoot \animals.scala" &&
16+ scala " $PSScriptRoot \clockhands.scala" |
17+ Compare-Object (Get-Content " $PSScriptRoot \..\test\clockhands_expected" ) |
18+ Assert-MatchTests &&
19+ scala " $PSScriptRoot \clockhandsTime.scala" |
20+ Compare-Object (Get-Content " $PSScriptRoot \..\test\clockhands_expected" ) |
21+ Assert-MatchTests &&
22+ scala " $PSScriptRoot \triple.scala" |
23+ Compare-Object (Get-Content " $PSScriptRoot \..\test\triple_expected" ) |
24+ Assert-MatchTests &&
25+ ForEach-Object ' foo'
26+
27+ if ($Error -or ! $? ) {
28+ " *** SCALA TESTS FAILED ***"
29+ }
30+ else {
31+ " SCALA TESTS PASSED"
32+ }
You can’t perform that action at this time.
0 commit comments