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
4 answers

Use tail recursion to achieve repeated function composition

I need to define a function (repeat-write n f) so that ((repeat-write n f) x) evaluates to (f (f (... (f x) ...))) where there are n applications of the function f. For example: ((repeat-write 4 cdr) ’(1 2 3 4 5 6)) evaluates to ’(5 6) I'm not too…
0
votes
2 answers

Failing composition of functorial and monadic operations

Another newbie question in my struggle to learn idiomatic Haskell: I'm trying to compose a smart constructor out of some validation functions, but I can't get the types to line up. This is the type I want to construct: newtype Tag = Tag { getTag ::…
Ulrich Schuster
  • 1,670
  • 15
  • 24
0
votes
1 answer

JavaScript bind sets the second argument equal to the first one when it should be undefined?

I recently tried to understand function composition in javascript. I understood what currying is and how it works, but I saw this code and I can't really understand it. It's function composition using .reduce(...). This is the code: const compose =…
David
  • 624
  • 1
  • 6
  • 21
0
votes
1 answer

Use compose with several functions and inner function with param

I have the following: { reseller: filterOutNonUsable(dedupe(extractArrVals(data.locks.entries, 'reseller'))) } What is the best way to call all these functions and use a second parameter for the extractArrVals function (see my example) so…
Gutelaunetyp
  • 2,144
  • 4
  • 15
  • 40
0
votes
5 answers

Combine 2 list functions into 1?

How would I combine the following 2 functions: replaceNth n newVal (x:xs) | n == 0 = newVal:xs | otherwise = x:replaceNth (n-1) newVal xs replaceMthNth m n v arg = replaceNth m (replaceNth n v (arg !! m)) arg into a single function? Is it…
maclunian
  • 7,893
  • 10
  • 37
  • 45
0
votes
0 answers

Is there a more concise way to define this array of function values in Python?

I am new to Python. I want to apply various permutations of three functions f, g, and h where juxtaposition means function composition (e.g., fg(x) = f(g(x))) to say, a list x of boolean integers, and put the results in an array. To make it easy…
mathematrucker
  • 166
  • 1
  • 9
0
votes
1 answer

Compose lambda function in C++ using parameter pack

I'm new to C++ 14, I want to compose a variable length of lambda functions together to a single lambda, how should I do that? Below is my current work #include template Ftype compose(const Ftype & fn) { return…
0
votes
1 answer

Error in using composed function in further function composition

I am having trouble understanding why I am unable to use a composed function and compose a new one. For eg: I have two functions f and g and I create a composed function composed1 from these. I tried to combine composed with fourth function lastOne…
Aandal
  • 51
  • 2
  • 11
0
votes
1 answer

How to use intermediate results in vertx future compose?

I am chaining some calls and I need to use the outcome of 1 future in a following call. In the example below, what would be the most elegant way to be able to use res1? call1() .compose(res1 -> call2(res1)) .compose(res2 ->…
Orkun
  • 6,998
  • 8
  • 56
  • 103
0
votes
2 answers

Haskell - fast naming of projection functions

Suppose I want to define a function f in terms of some other predefined function g as follows: f :: Int -> Int -> Int f 2 b = g b f _ _ = 1 That is, I want to define the projection, f(2,_) : Int->Int to be the same as g(_) : Int->Int. Gladly,…
0
votes
3 answers

Ramda: Confused about pipe

I'm learning functional programming in JS and I'm doing it with Ramda. I'm trying to make a function that takes parameters and returns a list. Here is the code: const list = R.unapply(R.identity); list(1, 2, 3); // => [1, 2, 3] Now I tried doing…
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
0
votes
1 answer

What is the type of this haskell double function composition?

t2 = (\x y z-> x.y.x) GHCI shows me this: t2 :: (b1 -> b2) -> (b2 -> b1) -> p -> b1 -> b2 I can't quite grasp it how this type signature comes to be. So far I've figured that the righter-most x is basically a function which takes a b2 and returns a…
Minimax
  • 121
  • 1
  • 5
  • 20
0
votes
1 answer

Validate input with any function in a list

Say I want to validate an input that has to satisfy one of a number of functions. What is the best way to do this in F#? Here’s a little example I came up with. let funcs = [ fun x -> x % 2 = 0 fun x ->…
0
votes
1 answer

Can I use the same function names when using Object.assign() in composition?

Context I created a new object using a composition paradigm in Javascript. const canUpdateScore = (state) => ({ update: (spell) => state.score-- }) const mage = (name) => { let state = { score: 100, } return Object.assign(state,…
Jason Allshorn
  • 1,625
  • 1
  • 18
  • 27
0
votes
1 answer

Function Composition Error

While going through a tutorial on Function Composition, I tried the following: let prefixFun a b = a + "" + b;; let exclaim s = s + "!";; let bigHello = prefixFun >> exclaim;; But the definition for bigHello returns the following type mismatch…
Yelena
  • 423
  • 3
  • 14