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
0
votes
1 answer

Function composition vs decorators, in a memoized recursive Fibonacci sequence generator

I'm practicing memoization to improve recursive functions, so I wrote this memoized Fibonacci generator: memo = {} def memo_fibo(n): if n not in memo: if n < 2: memo[n] = n else: memo[n] = memo_fibo(n - 2)…
0
votes
1 answer

How do composition and currying interact?

While doing exercises of chapter 3 of the Purescript by Example book was puzzled by this: The exercise is to write an isInBook :: String -> String -> AddressBook -> Boolean findEntryByName :: String -> String -> AddressBook -> Maybe…
Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72
0
votes
1 answer

Complex function composition in 1 line | Python 3.7

function composition of single argument functions is a simple 1 liner. how would I (if it's possible) write the following composition in 1 line? def fn(x: int) -> str: def inner(a, b): return strjoin(a, g(b)) return…
0
votes
0 answers

When nesting function calls like f(g(x)), which one is called first?

If I want to call multiple functions in JavaScript, which function will run first? Like in this example: sum(range(1, 10)); Does the sum function run first or does the range function run first? I am not sure which one runs first.
user1234Es
  • 11
  • 1
0
votes
1 answer

Understanding pipe function in JavaScript (functional programming)

I'm working through question 11 on bfe.dev's JavaScript coding exercises. The question asks to chain multiple basic math functions together via composition. Here's some sample code that passes the provided test cases. function pipe(funcs) { let…
0
votes
1 answer

Composing a Java BiFunction and Consumer

Similar to this question Composing a Java Function and Consumer. What is the best way to functionally compose a java BiFunction and a Consumer? For example given some BiFunction f and some Consumer c then doing f.andThen(c)…
0
votes
1 answer

Easy-to-read way of function composition in Javascript

I'm a huge fan of functional programming. I strive to use point free notation when I can. However, I often don't understand when point free notation is appropriate or when it is overkill. I typical delema that I run into is when I have a three or…
0
votes
2 answers

Python function composition (max recursion depth error, scope?)

What is wrong with this function? It seems like a scope error (although I thought I had fixed that by placing each callable in the list, instead of using it directly). Error is max recursion depth reached (when calling comp(inv,dbl,inc))... Note:…
0
votes
2 answers

Compose functions using generators

I am trying to make composition using generators. function A(x){return 1+x} function B(y){return 2*y} let val=tryCompose([A,B](1); //output should be : A(B(1)) How should the generator function should look like
0
votes
3 answers

Nested function pointers or function composition

I am working on C implementations of calculus operations (such as derivatives, integrals, etc...). As an example, here's the template definition of my derivative function: double derivative(double (*f)(double), double x); Let's say I want to…
Skander J.
  • 33
  • 7
0
votes
1 answer

Why does this composition function apply functions in the wrong order when passed as a reference?

Problem The problem is that a composition function sometimes applies its functions in the wrong order but only when it is passed as a reference (e.g., mapValues(compose(appendB, appendA))(wordNumberMap)). Whereas if the composition function is…
0
votes
1 answer

A Scheme function to compose other functions

I wish to create a function that can compose two functions into one. Given g and f, it should create a function h provides the output of f as input to g. Here was my result: (define (compose g f) (lambda (.args) (g (apply f (if (atom?…
0
votes
0 answers

haskell style generalized function composition

we have some different types Prelude> type T1 = Int Prelude> type T2 = Int Prelude> type T3 = Int Prelude> type T4 = Int ... Prelude> type X1 = Int Prelude> type X2 = Int Prelude> type X3 = Int Prelude> type X4 = Int and some functions Prelude> let…
Evg
  • 2,978
  • 5
  • 43
  • 58
0
votes
2 answers

Ramda - andThen, otherwise, andFinally?

Is there a 'finally' implementation in Ramda, for doing functional composition, and invoking a function regardless of the outcome of a promise? I want to to something like this: compose( finally(() => console.log("The promise resolved or…