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
8
votes
2 answers

How to compose a zero-argument function?

What I want to achieve is the 2 functions (one of them is no arg function) are composed into one. Here is an example to give you idea what I am doing: object Test extends App { val zeroArgFunc = () => 10 val intArgFunc = (i: Int) => s"hello…
egordoe
  • 918
  • 1
  • 5
  • 12
8
votes
5 answers

Expressive way compose generators in Python

I really like Python generators. In particular, I find that they are just the right tool for connecting to Rest endpoints - my client code only has to iterate on the generator that is connected the the endpoint. However, I am finding one area where…
chladni
  • 119
  • 7
8
votes
2 answers

Why does my function composition implemented by reduce returns a closure?

I want to derive a composition function for n-functions from reduce/fold, but it doesn't work as expected: $id = function ($x) { return $x; }; $comp = function ($f) { return function ($g) use ($f) { return function ($x) use ($f, $g) { …
user6445533
8
votes
2 answers

Why is function composition in F# so much slower, by 60%, than piping?

Admittedly, I am unsure whether I am correctly comparing apples with apples or apples with pears here. But I'm particularly surprised about the bigness of the difference, where a slighter difference, if any, would be expected. Piping can often be…
Abel
  • 56,041
  • 24
  • 146
  • 247
8
votes
2 answers

Why does function composition compose from right to left in Javascript?

Function composition composes from right to left: const comp = f => g => x => f(g(x)); const inc = x => x + 1; const dec = x => x - 1; const sqr = x => x * x; let seq = comp(dec)(comp(sqr)(inc)); seq(2); // 8 seq(2) is transformed to…
8
votes
2 answers

Haskell composition with two parameters

I'm trying to understand functional programming through Haskell and I'm having so much trouble dealing with function composition. Actually I have these two functions : add:: Integer -> Integer -> Integer add x y = x + y sub:: Integer -> Integer ->…
8
votes
1 answer

Why does the dot compose from right to left in Haskell?

If we have two functions, f and g, then in Haskell h = f . g is equivalent to h x = f(g x). I.e. the functions are applied from right to left to the input. Is there any fundamental reason why it goes from right to left, and not from left to right?…
Tiddo
  • 6,331
  • 6
  • 52
  • 85
8
votes
2 answers

Difference between function composition operator (.) and fmap (<$>)

Currently reading through this article (which is pretty brilliant btw) and have a pretty simple question: If I combine two functions like (+3) and (+2) with <$>, it seems to give me a new function that adds 5 to whatever is passed to it. If I do the…
Brendan
  • 511
  • 1
  • 3
  • 11
8
votes
2 answers

Haskell notation for composing two functions f and g where g takes multiple arguments

Often I find I want to compose two functions f and g, but g takes multiple arguments. Does Haskell provide a set of operators for that (I know I could write it myself, but it seems fairly common and I don't want to duplicate an operator that…
dspyz
  • 5,280
  • 2
  • 25
  • 63
8
votes
2 answers

Haskell: Why is ((.).(.)) f g equal to f . g x?

Could you please explain the meaning of the expression ((.).(.))? As far as I know (.) has the type (b -> c) -> (a -> b) -> a -> c.
8
votes
3 answers

foldl . foldr function composition - Haskell

So, I'm really frying my brain trying do understand the foldl.foldr composition. Here is a example: (foldl.foldr) (+) 1 [[1,2,3],[4,5,6]] The result is 22, but what's really happening here? To me it looks like this is what is happening: foldl (+) 1…
dehq
  • 449
  • 5
  • 12
8
votes
2 answers

efficient list append/prepend through function composition

Some months ago I read somewhere of an efficient approach for appending and prepending lists to other lists in O(1) by representing them with function compositions that, once evaluated, build in O(n) the resulting list. Unfortunately I cannot…
Riccardo T.
  • 8,907
  • 5
  • 38
  • 78
7
votes
1 answer

Haskell: difference in performance from different function composition?

The following code: import Control.Exception import Data.List updateAverage :: (Fractional t) => (t, t) -> t -> (t, t) updateAverage (old_value, old_counter) x = let new_counter = old_counter + 1 in assert(new_counter /= 0) …
quant_dev
  • 6,181
  • 1
  • 34
  • 57
7
votes
1 answer

(How) Can you curry compose monadic functions?

I have the following functions: f: a -> m[b] g: (b,c) -> m[d] h: (a,c) -> m[d] How can h be expressed as a composition of f and g? Using do/for notation we can implement h easily like so: h: (a,c) => { for { b <- f(a) d <- g(b,c) } yield…
PhD
  • 11,202
  • 14
  • 64
  • 112
7
votes
3 answers

A function composition operator in Python

In this question I asked about a function composition operator in Python. @Philip Tzou offered the following code, which does the job. import functools class Composable: def __init__(self, func): self.func = func …
RussAbbott
  • 2,660
  • 4
  • 24
  • 37