Skip to content

Commit 1229bb5

Browse files
committed
more condenced representation
1 parent 1dced8e commit 1229bb5

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

shared/src/test/scala/gopher/monads/Queens.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ class QueensSuite extends FunSuite {
1616
import scala.concurrent.ExecutionContext.Implicits.global
1717
given Gopher[Future] = SharedGopherAPI.apply[Future]()
1818

19-
type State = Vector[(Int,Int)]
19+
type State = Vector[Int]
2020

2121
extension(queens:State) {
2222

2323
def isUnderAttack(i:Int, j:Int): Boolean =
24-
queens.exists{ (qi,qj) =>
24+
queens.zipWithIndex.exists{ (qj,qi) =>
2525
qi == i || qj == j || i-j == qi-qj || i+j == qi+qj
2626
}
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)
3030

3131
}
3232

@@ -38,14 +38,14 @@ class QueensSuite extends FunSuite {
3838
val i = state.length
3939
if i < N then
4040
for{ j <- 0 until N if !state.isUnderAttack(i,j) }
41-
ch.write(state.put(i,j))
41+
ch.write(state appended j)
4242
ch.close()
4343
}
4444
ch
4545

4646
def solutions(state: State): ReadChannel[Future,State] =
4747
async[[X] =>> ReadChannel[Future,X]] {
48-
if(state.size < N) then
48+
if(state.length < N) then
4949
val nextState = await(putQueen(state))
5050
await(solutions(nextState))
5151
else
@@ -57,7 +57,7 @@ class QueensSuite extends FunSuite {
5757
async[Future] {
5858
val r = solutions(Vector.empty).take(2)
5959
assert(!r.isEmpty)
60-
println(r)
60+
println(r.map(_.asPairs))
6161
}
6262
}
6363

0 commit comments

Comments
 (0)