spire icon indicating copy to clipboard operation
spire copied to clipboard

Proposal: `andThen` for Polynomial?

Open windymelt opened this issue 1 year ago • 2 comments

Current Spire has compose for Polynomial[A]:

(f.compose(g).compose(h))(x) // => process h, then g, then f; that's result

But it has no andThen method for Polynomial.

In business context, People prefer more intuitive syntax ---- "Discount, then apply another discount, then add tax".

So implementing andThen alike function is reasonable choice for devs including newcomers.

Please tell me your opinions!

windymelt avatar Jun 09 '24 17:06 windymelt

Thanks for the idea! The compose method for Polynomial relates to this mathematical concept.

https://en.wikipedia.org/wiki/Polynomial#Composition

What is the mathematical formulation for andThen?

armanbilge avatar Aug 25 '24 22:08 armanbilge

It is just the opposite (and the standard semantics in Scala: https://www.scala-lang.org/api/current/scala/PartialFunction.html#andThen-f71).

basicaly (f.compose(g).compose(h))(x) is (h.andThen(g).andThen(f))(x). Such "pipeline" composition is often used in functional programming (Haskell, ocaml, etc). It is easier to read.

wasowski avatar May 25 '25 09:05 wasowski