Questions tagged [function-composition]

Applying one function to the result of another is known as function composition: `(f.g)(x) = f(g(x))`

Applying one function to the result of another is known as function composition: (f.g)(x) = f(g(x)).

See also .

420 questions
15
votes
2 answers

Haskell: type inference and function composition

This question was inspired by this answer to another question, indicating that you can remove every occurrence of an element from a list using a function defined as: removeall = filter . (/=) Working it out with pencil and paper from the types of…
Pillsy
  • 9,781
  • 1
  • 43
  • 70
14
votes
1 answer

Variadic compose function?

I'm trying to write a variadic function composition function. Which is basically the (.) except that the second argument function is variadic. This should allow expressions like: map even . zipWith (+) or just map even . zipWith Currently what…
is7s
  • 3,500
  • 1
  • 20
  • 41
14
votes
1 answer

compose function and functional module

Python 3.2 documentation refers to Collin Winter's functional module which contains function compose: The compose() function implements function composition. In other words, it returns a wrapper around the outer and inner callables, such that…
max
  • 49,282
  • 56
  • 208
  • 355
14
votes
1 answer

Is there an operator for function composition in Julia?

Say I have two functions: f(x) = x^2 g(x) = x + 2 Their composition is the function h(x) = f(g(x)) Is there an operator for function composition in Julia? For example, if * was an operator for function composition (which it isn't), we could…
a06e
  • 18,594
  • 33
  • 93
  • 169
13
votes
2 answers

Function composition in Haskell with tuple arguments

Sometimes I have two functions of the form: f :: a -> (b1,b2) h :: b1 -> b2 -> c and I need the composition g. I solve this by changing h to h': h' :: (b1,b2) -> c Can you please show me (if possible) a function m, so that: (h . m . f) == (h' .…
Jogusa
  • 5,530
  • 5
  • 24
  • 23
13
votes
3 answers

Composing a Java Function and Consumer

What is the best way to functionally compose a java Function and a Consumer? For example given some Function f and some Consumer c then doing f.andThen(c) would feel natural, however that is not how the interfaces work. The…
kag0
  • 5,624
  • 7
  • 34
  • 67
13
votes
3 answers

Functional composition with multi-valued functions in haskell?

I was wondering if it was possible to do functional composition with functions that take more than one argument. I want to be able to do something like this x = (+3).(*) setting x equal to a function that adds three to the product of two numbers.
MYV
  • 4,294
  • 6
  • 28
  • 24
12
votes
2 answers

Expressing long chain of compositions in Haskell

(unimportant background info / motivation) I was implementing a different version of nub, inspired by the Yesod book's discouragement of using it. map head . group . sort is more efficient than a call to nub. However, in our case, order is…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
12
votes
5 answers

How can I write a recursive compose function in R?

I would like to create a function, "compose" in R which will compose an arbitrary number of functions given as arguments. So far, I have accomplished this by defining a function "of" that composes two arguments and then Reducing this: of <-…
Alex Gian
  • 482
  • 4
  • 10
12
votes
3 answers

Is there a way to chain functions like withCString?

Is there a way to chain functions like withCString? By that I mean any function that looks something like f :: Foo -> (CFoo -> IO a) -> IO a. For example, lets say there is a function cFunc :: CString -> CFoo -> CBar -> IO () Usualy, I would do…
11
votes
2 answers

Combining fragments of Haskell Code to get the bigger picture

This is the code that I came upon somewhere but want to know how this works: findIndices :: (a -> Bool) -> [a] -> [Int] findIndices _ [] = [] findIndices pred xs = map fst (filter (pred . snd) (zip [0..] xs)) Output: findIndices (== 0)…
11
votes
5 answers

Composition of a hierarchy of functions

Is there a canonical way to express a function that is a composition of a rooted tree of functions? Here is a concrete example of what I mean by "composition of a tree of functions." Take a rooted tree whose nodes are labelled by functions, like…
egnha
  • 1,157
  • 14
  • 22
11
votes
3 answers

Chaining methods left to right in Haskell (as opposed to right to left)

I come from Scala. So I frequently do stuff like: println((1 to 10).filter(_ < 3).map(x => x*x)) In Haskell, after I discovered I can get rid of all the nested parenthesis using $ and ., I recently found myself writing: putStrLn . show . map (**2)…
sscarduzio
  • 5,938
  • 5
  • 42
  • 54
11
votes
1 answer

How Haskell's "composing lenses using function composition" with that weird order of arguments could be implemented?

I've been reading A wreq tutorial: A lens provides a way to focus on a portion of a Haskell value. For example, the Response type has a responseStatus lens, which focuses on the status information returned by the server. ghci> r ^.…
11
votes
4 answers

What is ((+) . (+)) in Haskell?

In ghci, :t ((+).(+)) > ((+).(+)) :: (Num (a -> a), Num a) => a -> (a -> a) -> a -> a but what is this thing? Can any one give me an example of the use of this please? How can one composite 2 functions that take 2 parameters each? for example, how…
Larry
  • 858
  • 10
  • 16
1 2
3
27 28