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

Function composition with the Writer monad?

I'm studying the Writer monad and have the following: myFunction :: Int -> Int -> Writer String Int myFunction e1 e2 | e1 > e2 = do tell ("E1 greater") return (e1) | otherwise = do tell ("E2 greater") return…
Babra Cunningham
  • 2,949
  • 1
  • 23
  • 50
0
votes
1 answer

Why does this function composition ( with tail recursion ) in ES6 returned undefined but the accumulator returns the correct result?

I'm attempting to write a compose function which can accept an arbitrary number of functions, store them in a closure, and apply each of those functions to an initial value. The actual application of the function composition executes in a tail call…
bresson
  • 831
  • 11
  • 20
0
votes
1 answer

Composing function with higher arity in Ramda

I'm eagerly trying to learn functional programming in js, but I am having a hard time. I feel like I am constantly doing stuff like this, which just doesn't feel right at all: export const getParamFromUrl = R.curry((name, url) => R.compose( …
0
votes
1 answer

Function composition in Haskell to calculate Power

i am trying to write a power function in haskell that calculates f to the power of n, where f is a function itself, using function composition. This is what I have so far: let pow 0 f = (\x -> x) pow n f = f . (pow (n-1) f) in 2 ((\x -> x+1)…
0
votes
1 answer

Haskell how to get sum of all positive members using function composition?

I have to write a function that takes all positive numbers from Integer list, adds to each 10 and then sums all of them until it reaches the maxNum. I've already wrote something that kinda works : ff :: Integer -> [Integer] -> Integer ff maxNum =…
Nikas Žalias
  • 1,594
  • 1
  • 23
  • 51
0
votes
2 answers

Passing arguments while running lodash flow asynchronously

Given the code below, how can I pass id to the applySaveAsync function? var then = _.curry(function (f, thenable) { return thenable.then(f); }); var validateAsync = _.flow( function () { return…
0
votes
1 answer

Mathematical relations between functions

I'm going to use an exemple to ask my question, lets suppose we have a function G composed of 3 functions f1, f2 and f3, like this picture : the function G = f3(f2(f1(input))) or g = f3 º f2 º f1 an other example when G is composed of 4 functions…
0
votes
1 answer

Function composition in boolean operators

I don't have much FP experience and I think I'm just missing a key insight from someone more versed. I'm writing a small, embedded, functional, strictly- and invariantly-typed language. I'll be using Haskell-like syntax in this question. The…
0
votes
2 answers

Is there a way to write this Javascript function without listing the arguments?

I'm trying to write a function that compares two items using another function, then checks if the result is greater than some other value also provided to the function. I can write it like this: const compareDifference = function(t1, t2, threshold)…
0
votes
3 answers

Function composition in Fortran

I want to create a function composition in Fortran. The idea is if I have f(x) and g(x), I want to find f(g(x)). It is simple in syntax in python Better Function Composition in Python. Here is a sample Fortran code: module test10 implicit…
0
votes
0 answers

Capture original argument from function using andThen

I have a function: f: T => Option[T] I want to use this function to compose another function in which I would capture the original argument of type T from f, when the result returned from it is None g: T => T The best way that I have found to to…
0
votes
0 answers

Compose function with two arguments

I ran into something that threw me off today. I was trying to define a function equivalent to Data.Text.commonPrefixes that throws away everything but the prefix. The following works: commonPrefix :: Text -> Text -> Maybe Text commonPrefix a b =…
Sean Clark Hess
  • 15,859
  • 12
  • 52
  • 100
0
votes
1 answer

Puzzling function composition

How can I make sense of function composition in the following expression? map . foldr (.) id :: [b -> b] -> [b] -> [b]
0
votes
1 answer

Error while composing two functions

I try to compose two function with type specifying. foo :: Num a => a -> a foo a = a + 2 bar :: Num a => a -> a bar a = a * 2 fooBarCompose :: (Num a, Num b, Num c) => (a -> b) -> (c -> a) -> c -> b fooBarCompose f g = f . g My module compiles,…
barbara
  • 3,111
  • 6
  • 30
  • 58
0
votes
2 answers

How can I represent the function chain using compose?

I am using the highlandjs library to read files and add an end card to their contents before displaying them in the console: const readFile = highland.wrapCallback(fs.readFile); const addEndCard = x => x + '\nx---THE…
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183