Skip to content

Commit

Permalink
BestFirstSearch: if failed, retry no optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Meltzer committed Feb 22, 2020
1 parent 0876f01 commit 3d4fb7b
Showing 1 changed file with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ object AdjunctUsage extends App {

// traverse the list with our stateful computation, producing a list
// of booleans for "was this a repeat of the previous"
val res1: List[Boolean] = Traverse[List].traverseS(nonRepeating)(checkForRepeats).eval(None)
val res2: List[Boolean] = Traverse[List].traverseS(repeating)(checkForRepeats).eval(None)
val res1: List[Boolean] =
Traverse[List].traverseS(nonRepeating)(checkForRepeats).eval(None)
val res2: List[Boolean] =
Traverse[List].traverseS(repeating)(checkForRepeats).eval(None)

// when we collapse the lists of booleans, we expect the non-repeating list to all be false
assert(Tag.unwrap(res1.foldMap(Tags.Disjunction(_))) === false)
Expand Down Expand Up @@ -77,7 +79,6 @@ object AdjunctUsage extends App {

assert(sumOfInts === 10)


// the Id functors get in the way of type inference
// TODO: see if we can get rid of this explicitness
// TODO: also see if we can Trampoline these as they are going to
Expand All @@ -87,32 +88,43 @@ object AdjunctUsage extends App {
// computations: look for repeats and sum the ints, could be
// composed together, so that they happen on a single pass through a
// Traversable,
type ROIRIWW[A] = Reader[Option[Int], // read the previous value for computing repeats
Reader[Int, // read the accumulated sum
Writer[Int,// write the new sum
Writer[Option[Int],A]]]] // write the next value for computing repeats
type ROIRIWW[A] = Reader[
Option[Int], // read the previous value for computing repeats
Reader[
Int, // read the accumulated sum
Writer[
Int, // write the new sum
Writer[Option[Int], A]
]
]
] // write the next value for computing repeats

// now we can combine our two stateful computations
val checkForRepeatsAdjAndSum2: Int ROIRIWW[Boolean] = { next
checkForRepeatsAdj(next).map(w sum(next).map(_.map(_ w)))
}

// but this can be done more generically for any two of these Reader/Writer adjunctions
def run2RWState[A,S1,S2,B,C,R](rws1: A RWState[S1,B], rws2: A RWState[S2,C], f: (B,C) R) = { a: A
def run2RWState[A, S1, S2, B, C, R](
rws1: A RWState[S1, B],
rws2: A RWState[S2, C],
f: (B, C) R) = { a: A
rws1(a).map(b rws2(a).map(_.map(c Writer(b.run._1, f(b.run._2, c)))))
}

// with the above function we can combine the two stateful
// computations with a function that throws away the Unit from sum.
val checkForRepeatsAdjAndSum: Int ROIRIWW[Boolean] = run2RWState(checkForRepeatsAdj,sum,(a:Boolean,_:Any) a)
val checkForRepeatsAdjAndSum: Int ROIRIWW[Boolean] =
run2RWState(checkForRepeatsAdj, sum, (a: Boolean, _: Any) a)

// since the adjunctions compose, we can run both stateful
// computations with a single traverse of the list. This
// composability is something you won't find so easily with the
// state monad by itself.
val bothAtOnce = {
// yay for type scala's inference :(
implicit val adjMonad = adjOptionInt.compose[Writer[Int,?],Reader[Int,?]](adjInt).monad
implicit val adjMonad =
adjOptionInt.compose[Writer[Int, ?], Reader[Int, ?]](adjInt).monad
nonRepeating.traverseU(checkForRepeatsAdjAndSum).run(None).run(0).run
}

Expand Down

0 comments on commit 3d4fb7b

Please sign in to comment.