Collecting values with a given strategy

11 views
Skip to first unread message

etorreborre

unread,
Jul 6, 2016, 5:04:07 AM7/6/16
to kiama
Hi,

I am currently trying to traverse a graph representing a set of software components.

Some of them are implementing a "Start" interface so that they can be started (think database connection, actor system, JMX registration,...):

trait Start {

// start a given component
def start: Eval[StartResult]

}

sealed trait StartResult

case class StartOk(message: String) extends StartResult
case class StartFailure(message: String) extends StartResult
case class StartError(message: String, exception: Throwable) extends StartResult

I have implemented the following strategy to start the components in order (starting from the bottom):

/** start components from the bottom up */
def start[G](graph: G): Eval[List[StartResult]] = Eval.later {
val results: ListBuffer[StartResult] = new ListBuffer[StartResult]

val startStrategy =
somebu(strategy[Any] {
case s: Start =>
s.start.value match {
case s: StartOk => results.append(s); None
case other => results.append(other); Option(s)
}
})

rewrite(startStrategy)(graph)

results.toList
}

This strategy is using an external mutable variable to collect the start results. 

I have the impression that I should be using something like "collect" for this kind of traversal but collect does a top down traversal and wouldn't stop collecting on a failure.
Is there another way to do a "collect"  and parameterize it with a given strategy?

Thanks.

Eric.

Tony Sloane

unread,
Jul 6, 2016, 7:57:22 PM7/6/16
to ki...@googlegroups.com
Hi Eric,

> On 6 Jul 2016, at 7:04 PM, etorreborre <etorr...@gmail.com> wrote:
>
> I have the impression that I should be using something like "collect" for this kind of traversal but collect does a top down traversal and wouldn't stop collecting on a failure.
> Is there another way to do a “collect" and parameterize it with a given strategy?

There is no simple way to do this at present. Nearing the top of our todo list is a task to generalise what are usually called type-unifying strategies, i.e., ones that do some sort of traversal and return a single result at the end. The current collect is one of those but as you point out it’s tied to a particular top down traversal. Eventually we will have an expressive combinator language for these type-unifying strategies as we currently do for the others (called type preserving).

So, at the moment doing it as you’ve done is the only way, but stay tuned. I expect to have more time for Kiama enhancements in the second half of the year.

cheers,
Tony

etorreborre

unread,
Jul 7, 2016, 2:04:18 AM7/7/16
to kiama
I'm glad to know that there is some kind of general "fold" feature on the way. If we can reuse the existing traversal strategies it will be very powerful.

I'll stay tuned!

cheers,

Eric.
Reply all
Reply to author
Forward
0 new messages