@@ -16,17 +16,17 @@ class QueensSuite extends FunSuite {
16
16
import scala .concurrent .ExecutionContext .Implicits .global
17
17
given Gopher [Future ] = SharedGopherAPI .apply[Future ]()
18
18
19
- type State = Vector [( Int , Int ) ]
19
+ type State = Vector [Int ]
20
20
21
21
extension(queens: State ) {
22
22
23
23
def isUnderAttack (i: Int , j: Int ): Boolean =
24
- queens.exists{ (qi,qj ) =>
24
+ queens.zipWithIndex. exists{ (qj,qi ) =>
25
25
qi == i || qj == j || i- j == qi- qj || i+ j == qi+ qj
26
26
}
27
-
28
- def put (i : Int , j : Int ): State =
29
- queens :+ (i,j)
27
+
28
+ def asPairs : Vector [( Int ,Int )] =
29
+ queens.zipWithIndex.map(_.swap)
30
30
31
31
}
32
32
@@ -38,14 +38,14 @@ class QueensSuite extends FunSuite {
38
38
val i = state.length
39
39
if i < N then
40
40
for { j <- 0 until N if ! state.isUnderAttack(i,j) }
41
- ch.write(state.put(i,j) )
41
+ ch.write(state appended j )
42
42
ch.close()
43
43
}
44
44
ch
45
45
46
46
def solutions (state : State ): ReadChannel [Future ,State ] =
47
47
async[[X ] =>> ReadChannel [Future ,X ]] {
48
- if (state.size < N ) then
48
+ if (state.length < N ) then
49
49
val nextState = await(putQueen(state))
50
50
await(solutions(nextState))
51
51
else
@@ -57,7 +57,7 @@ class QueensSuite extends FunSuite {
57
57
async[Future ] {
58
58
val r = solutions(Vector .empty).take(2 )
59
59
assert(! r.isEmpty)
60
- println(r)
60
+ println(r.map(_.asPairs) )
61
61
}
62
62
}
63
63
0 commit comments