Mapping a list of functions

Hello all The standard map function applies a single function to a list of arguments. But what if I want to apply a list of functions to a single argument. I can of course write such a function, but I wonder if there is a standard way of doing this, Related to that is the problem, that the function in map may require more than one argument, so I need to apply it partially to some value(s) first. But what if I want to fix its second and third argument and not its first argument? Can flip help? Thanks -- Martin

On Jun 17, 2010, at 12:02 PM, Martin Drautzburg wrote:
The standard map function applies a single function to a list of arguments. But what if I want to apply a list of functions to a single argument. I can of course write such a function, but I wonder if there is a standard way of doing this,
Use Control.Applicative, and the <*> operator. Something like [f, g, h, i, j] <*> pure x (which equals [f x, g x, h x, i x , j x])
Related to that is the problem, that the function in map may require more than one argument, so I need to apply it partially to some value(s) first. But what if I want to fix its second and third argument and not its first argument? Can flip help?
I think the <$> operator can help here. Also, don't forget plain old lambda abstraction to abstract over a free variable.

Martin.Drautzburg:
Hello all
The standard map function applies a single function to a list of arguments. But what if I want to apply a list of functions to a single argument. I can of course write such a function, but I wonder if there is a standard way of doing this,
map ($ 2) [(*2), (+1), (^7)] Cheers, Don

Martin Drautzburg wrote:
Hello all
The standard map function applies a single function to a list of arguments. But what if I want to apply a list of functions to a single argument. I can of course write such a function, but I wonder if there is a standard way of doing this,
The magical incantation you seek is "map ($ x) [fn1, fn2, fn3]". Remember that "$" is a function like any other, and can be curried.

Martin Drautzburg wrote:
The standard map function applies a single function to a list of arguments. But what if I want to apply a list of functions to a single argument.
So your list of arguments is actually a list of functions. But since functions are first-class values, that shouldn't be a problem. You simply have to write a appropriate function to map over that list of functions: map (\f -> f a b c) listOfFunctions The lambda expression (\f -> f a b c) denotes a function which takes a function f, and applies it to some values a, b and c. That's exactly the function you want to map over the list of functions. Alternatively, you might want to look into list comprehensions: [f a b c | f <- listOfFunctions] Enjoy Haskell! Tillmann

listFs = [f1, f2, f3] map ($ x) listFs -- same as [f1 x, f2 x, f3 x] f x y z = ... map (\x -> f x u v) xs On 17 Jun 2010, at 23:02, Martin Drautzburg wrote:
Hello all
The standard map function applies a single function to a list of arguments. But what if I want to apply a list of functions to a single argument. I can of course write such a function, but I wonder if there is a standard way of doing this,
Related to that is the problem, that the function in map may require more than one argument, so I need to apply it partially to some value(s) first. But what if I want to fix its second and third argument and not its first argument? Can flip help?
Thanks
-- Martin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Martin Drautzburg
Hello all
The standard map function applies a single function to a list of arguments. But what if I want to apply a list of functions to a single argument. I can of course write such a function, but I wonder if there is a standard way of doing this,
> (map ($ 3) [(+2), (*4), (/3)])::[Double] [5.0,12.0,1.0]
Related to that is the problem, that the function in map may require more than one argument, so I need to apply it partially to some value(s) first. But what if I want to fix its second and third argument and not its first argument? Can flip help?
The "pointfree" command-line tool can help you find good short forms for
this kind of thing:
$ pointfree '\x -> f x 2 3'
flip (flip f 2) 3
In this case the explicit (pointed) lambda is simpler so I would usually
just use that.
G
--
Gregory Collins

map (\function -> function argument) functions map ($ argument) functions map (\firstArgument -> function firstArgument secondArgument thirdArgument) xs On 17.06.10 22:02, Martin Drautzburg wrote:
Hello all
The standard map function applies a single function to a list of arguments. But what if I want to apply a list of functions to a single argument. I can of course write such a function, but I wonder if there is a standard way of doing this,
Related to that is the problem, that the function in map may require more than one argument, so I need to apply it partially to some value(s) first. But what if I want to fix its second and third argument and not its first argument? Can flip help
-- Best regards, Roman Beslik.

Hello Martin, Thursday, June 17, 2010, 11:02:31 PM, you wrote:
But what if I want to apply a list of functions to a single argument. I can
one more answer is "swing map": http://www.haskell.org/haskellwiki/Pointfree#Swing -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

???
What does exactly swing do ?
2010/6/18 Bulat Ziganshin
Hello Martin,
Thursday, June 17, 2010, 11:02:31 PM, you wrote:
But what if I want to apply a list of functions to a single argument. I can
one more answer is "swing map":
http://www.haskell.org/haskellwiki/Pointfree#Swing
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

swing map :: forall a b. [a -> b] -> a -> [b]
swing any :: forall a. [a -> Bool] -> a -> Bool
swing foldr :: forall a b. b -> a -> [a -> b -> b] -> b
swing zipWith :: forall a b c. [a -> b -> c] -> a -> [b] -> [c]
swing find :: forall a. [a -> Bool] -> a -> Maybe (a -> Bool)
-- applies each of the predicates to the given value, returning the
first predicate which succeeds, if any
swing partition :: forall a. [a -> Bool] -> a -> ([a -> Bool], [a -> Bool])
Essentially, the main use case seems to be transforming HOFs that
operate on a list of values and a single function into HOFs that
operate on a list of functions and a single value.
Cheers.
~Liam
On 19 June 2010 19:30, Limestraël
??? What does exactly swing do ?
2010/6/18 Bulat Ziganshin
Hello Martin,
Thursday, June 17, 2010, 11:02:31 PM, you wrote:
But what if I want to apply a list of functions to a single argument. I can
one more answer is "swing map":
http://www.haskell.org/haskellwiki/Pointfree#Swing
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

I love that. It's great. Definitely going in my .ghci file.
On 20 June 2010 12:28, Liam O'Connor
swing map :: forall a b. [a -> b] -> a -> [b] swing any :: forall a. [a -> Bool] -> a -> Bool swing foldr :: forall a b. b -> a -> [a -> b -> b] -> b swing zipWith :: forall a b c. [a -> b -> c] -> a -> [b] -> [c] swing find :: forall a. [a -> Bool] -> a -> Maybe (a -> Bool) -- applies each of the predicates to the given value, returning the first predicate which succeeds, if any swing partition :: forall a. [a -> Bool] -> a -> ([a -> Bool], [a -> Bool])
Essentially, the main use case seems to be transforming HOFs that operate on a list of values and a single function into HOFs that operate on a list of functions and a single value.
Cheers. ~Liam
On 19 June 2010 19:30, Limestraël
wrote: ??? What does exactly swing do ?
2010/6/18 Bulat Ziganshin
Hello Martin,
Thursday, June 17, 2010, 11:02:31 PM, you wrote:
But what if I want to apply a list of functions to a single argument. I can
one more answer is "swing map":
http://www.haskell.org/haskellwiki/Pointfree#Swing
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (12)
-
Alexander Solla
-
Andrew Coppin
-
Bulat Ziganshin
-
Christopher Done
-
Don Stewart
-
Gregory Collins
-
Liam O'Connor
-
Limestraël
-
Martin Drautzburg
-
Miguel Mitrofanov
-
Roman Beslik
-
Tillmann Rendel