functional programming - How to run a sequence of operations (functions) in Haskell? -
let's have list of values want apply sequence of operations until final result:
[0, 1, 2] firstoperation xs = map (+1) xs secondoperation xs = filter xs thirdoperation xs = sum xs although sure there other better ways handle this, 1 know define function calls these functions nested 1 inside another:
runalloperations xs = thirdoperation (secondoperation (firstoperation xs)) but both ugly , raises problem if have 10 operations, turns bit of code maintanance nightmare.
what correct way of implementing of kind here? keep in mind example gave above oversimplification of facing on current project.
if can make list of operations, can fold composition operator on list:
foldr (.) id fns then can apply result of initial values.
though might need apply final reduction step separately.
Comments
Post a Comment