5

When we call the same function on a list of things, we call that "map". What do we call it when we call a list of functions on the same data? I don't mean pipe - not feeding the output of each function in turn into the next function - but simply iterating over a list of functions, passing each the same input?

  • 5
    In my day, we used to call that "calling a list of functions with the same input." – Blrfl Jan 4 '17 at 19:15
  • I think calling "collection" of different methods with same signature(input) is what event and eventhandlers does. When you raise an event all eventhandlers executed with same input parameters. I think "delegating" is ok word. – Fabio Jan 4 '17 at 19:56
  • 1
    Using Flynn's taxonomy, I would suggest MISD (multiple instruction, single data). – mouviciel Jan 5 '17 at 9:34
  • This is a fairly easy example of the Visitor pattern -- maybe you'll find your verbiage researching that. – Bryan Boettcher Jan 5 '17 at 15:38
13

I do not know of any term for your operation, but it can be seen as a kind of mapping, so maybe you do not need a special name for it. I will illustrate this in Haskell, but it is easy to do the same in other languages, e.g. Python, Clojure, Javascript, etc.

In Haskell map has type:

map :: (a -> b) -> [a] -> [b]

For example, you can map length over a list strings:

xs = ["Good", "morning", "world"] :: [String]

> map length xs
[4,7,5]

Now suppose you have a list of functions, e.g.

fs = [(+ 1), (* 5), (* 2)] :: [Int -> Int]

How can you apply each function to the same number, say, 1? You can use the function:

($) :: (a -> b) -> a -> b

This function takes another function and applies it to an argument. If you partially apply it to the number 1, you get:

($ 1) :: Num a => (a -> b) -> b

This is a function that given a function from a numeric type to any type b, applies it to 1. So, now we can map ($ 1) over our list of functions:

> map ($ 1) fs
[2,5,2]

So, in general, given an input x :: a and a list of functions fs :: [a -> b] you can apply all the functions to x with map ($ x) fs, which gives you a result of type [b].

  • 1
    Excellent. I suspected it was just another variation on map, but couldn't articulate an answer well enough to prove it. – Robert Harvey Jan 4 '17 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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