4

I wish to apply a sequence of functions to an object (each of the functions may return the same or modified object) and get the ultimate result returned by the last function.

Is there an idiomatic Scala way to turn this (pseudocode):

val pipeline = ListMap(("a" -> obj1), ("b" -> obj2), ("c" -> obj3))

into this?

val initial_value = Something("foo", "bar")
val result = obj3.func(obj2.func(obj1.func(initial_value)))

The pipeline is initialized at runtime and contains an undetermined number of "manglers".

I tried with foreach but it requires an intermediate var to store the result, and foldLeft only works on types of ListMap, while the initial value and the result are of type Something.

Thanks

5
  • Is input and output type of each function the same?
    – ghik
    Sep 30, 2015 at 17:48
  • What is the signature of obN.func? (bascally the same as @ghik's comment) Sep 30, 2015 at 17:54
  • @ghik, each function accepts and returns a Something: def func(foo: Something): Something Sep 30, 2015 at 17:55
  • "foldLeft only works on types of ListMap, while the initial value and the result are of type Something." Why is this a problem? You call foldLeft on pipeline, passing inital_value as the initial value, and modify the accumulator in each step by calling the next function from the pipeline Sep 30, 2015 at 17:55
  • as I understand you just need to get rid of keys of your ListMap first pipeline.values.foldLeft(initial_value)((a, f) => f.func(a))
    – dk14
    Sep 30, 2015 at 17:56

4 Answers 4

7

This should do it:

 pipeline.foldLeft(initial_value){case (acc, (k,obj)) => obj.func(acc)}

No idea why pipeline contains pairs, though.

2
  • Thanks, I managed to get foldLeft working now. The pairs in the pipeline are because I will later pass the pipeline itself into another function which may need to access specific processors by their names. Sep 30, 2015 at 18:16
  • I'm not sure, but I don't think ListMap makes any guarantees about the order of elements - it is at liberty to shuffle them every time you produce a updated one. If you wanted something that preserves the insertion order, you may prefer LinkedHashMap. Sep 30, 2015 at 18:51
2

Assuming input and output types are the same, I'd go with a reduceLeft and composition by andThen:

def pipe[A](a: A, funcs: List[A => A]): A = funcs.reduceLeft(_ andThen _)(a)
2
  • 1
    You want reduceLeft. For reduce "The order in which operations are performed on elements is unspecified and may be nondeterministic." And pipeline isn't a list of A=>A so can't be used directly. Sep 30, 2015 at 18:10
  • Upvoted for taking a different approach of composing functions to get a function that does what is needed. Sep 30, 2015 at 18:48
0

I think foldLeft is the right choice:

val pipeline = List("a"-> func1, "b"-> func2, "c"-> func3)
...
val result = pipeline.foldLeft(initial_value) {case (acc,(key,func)) => func(acc)}
1
  • Pipeline isn't a List, but a ListMap. That still supports foldLeft though Sep 30, 2015 at 18:07
0

Get rid of your keys, first:

pipeline.values.foldLeft(initial_value)((a, f) => f.func(a))
6
  • 1
    Does values of a ListMap preserve order? I think it probably does but the scaladoc doesn't say Sep 30, 2015 at 18:09
  • @TheArchetypalPaul Is it really matters? func's from OP's question may be commutative. However, from the sources I can surely say that it preserves order actually. Every Node has next method, which returns previous one, this next is used by iterator, returned by values method, see github.com/scala/scala/blob/v2.11.7/src/library/scala/…
    – dk14
    Sep 30, 2015 at 18:41
  • It's pretty unlikely the functions would be commutative. In fact, they'd have to be identity or inverses of one another for that to work. You need f(g(i)) === g(f(i)) for all g and f in pipeline's functions. With more than two, that means identity only Sep 30, 2015 at 18:45
  • And personally, I'd prefer to go by the contract of the doc/spec and not rely on the current implementation. Although I think the bigger issue may be how pipeline is constructed, not how it's used. Sep 30, 2015 at 18:50
  • @TheArchetypalPaul But doc says nothing about preserving order inside ListMap itself (except reference to List-based structure, which should be intuitively order-preserving), so same for foldLeft. And OP is using exactly this structure. Talking about commutativity, simplest example is f = _ + 1, g = _ + 2, so it's not so unlikely as it seems :)
    – dk14
    Sep 30, 2015 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.