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
11
votes
7 answers

Better Function Composition in Python

I work in Python. Recently, I discovered a wonderful little package called fn. I've been using it for function composition. For example, instead of: baz(bar(foo(x)))) with fn, you can write: (F() >> foo >> bar >> baz)(x) . When I saw this, I…
Charles R
  • 17,989
  • 6
  • 20
  • 18
10
votes
3 answers

What am I missing: is function composition with multiple arguments possible?

I understand the basics of function composition in F#, as, for example, described here. Maybe I am missing something, though. The >> and << operators seem to have been defined with the assumption that each function only takes one argument: >…
Kevin Cantu
  • 807
  • 7
  • 17
10
votes
1 answer

Typescript recursive function composition

I want to create a function chain, which would be an input of a pipe/flow/compose function. Is this possible without the literal expansion of the types to selected depth, as is this usually handled? See lodash's flow. I want to achieve typecheck of…
10
votes
2 answers

Why does function composition require parentheses?

Let's say I want to compose Text.pack with Text.strip. :t (.) produces: (b -> c) -> (a -> b) -> a -> c :t (Text.pack) produces: String -> Text :t (Text.strip) produces: Text -> Text So substituting strip for (b -> c) gives: b = Text c =…
Abraham P
  • 15,029
  • 13
  • 58
  • 126
9
votes
2 answers

Haskell Rewrite Rules and Function Composition

Why does haskell require multiple rewrite rules depending on the function composition technique and length? Is there a way to avoid this? For example, given the following code... {-# RULES "f/f" forall a. f ( f a ) = 4*a #-} f a = 2 * a this…
Toymakerii
  • 1,510
  • 2
  • 12
  • 26
9
votes
6 answers

Pointfree function combination in Python

I have some predicates, e.g.: is_divisible_by_13 = lambda i: i % 13 == 0 is_palindrome = lambda x: str(x) == str(x)[::-1] and want to logically combine them as in: filter(lambda x: is_divisible_by_13(x) and is_palindrome(x), range(1000,10000)) The…
Frank S. Thomas
  • 4,725
  • 2
  • 28
  • 47
9
votes
5 answers

composition with dyadic operator?

I want to do something fairly simple; I am using the operator (++) with Data.Map insertWith, and it works fine, but I want to eliminate duplicates in the value created, so want to compose it with nub. I tried (nub (++)), (nub $ (++)), (nub . (++)),…
guthrie
  • 4,529
  • 4
  • 26
  • 31
9
votes
2 answers

Haskell - How does this average function work?

I found this implementation of the average function: avg :: [Int] -> Int avg = div . sum <*> length How does this work? I looked at the function that was produced as a result of div . sum: (div . sum) :: (Integral a, Foldable t) => t a -> a -> a I…
xilpex
  • 3,097
  • 2
  • 14
  • 45
9
votes
3 answers

Composite functions in ocaml

How can I define a composite function in a functional language, in particular with Ocaml? For example, if I write a function that calculates the negation of the result of another function, that is: not(f(x)) where f(x) returns a boolean. How can I…
kafka
  • 949
  • 12
  • 27
9
votes
2 answers

Railway oriented programming with Async operations

Previously asked similar question but somehow I'm not finding my way out, attempting again with another example. The code as a starting point (a bit trimmed) is available at https://ideone.com/zkQcIU. (it has some issue recognizing…
Developer11
  • 771
  • 5
  • 15
9
votes
1 answer

Serialize Composed Func?

This works fine: Func func1 = s => s + "func"; ViewState["function"] = func1; However, this does not: Func func1 = s => s + "func"; Func func2 = s => func1(s); …
Fishtoaster
  • 1,809
  • 2
  • 20
  • 36
9
votes
1 answer

Python function composition

I've tried to implement function composition with nice syntax and here is what I've got: from functools import partial class _compfunc(partial): def __lshift__(self, y): f = lambda *args, **kwargs: self.func(y(*args, **kwargs)) …
si14
  • 783
  • 6
  • 11
9
votes
5 answers

Composing functions in Java?

I'm writing demo code for an API we've created and I keep running into the same problem where I'm repeating myself, over and over ad nauseum. I am painfully aware that Java is scheduled to have closures added but I don't have access to them now. …
wheaties
  • 35,646
  • 15
  • 94
  • 131
9
votes
4 answers

How to combine two procs into one?

Just wondering if there's a syntax shortcut for taking two procs and joining them so that output of one is passed to the other, equivalent to: a = ->(x) { x + 1 } b = ->(x) { x * 10 } c = ->(x) { b.( a.( x ) ) } This would come in handy when…
Gunchars
  • 9,555
  • 3
  • 28
  • 27
8
votes
2 answers

Composing a chain of 2-argument functions

So I have a list of a functions of two arguments of the type [a -> a -> a] I want to write a function which will take the list and compose them into a chain of functions which takes length+1 arguments composed on the left. For example if I have…
Stephen Diehl
  • 8,271
  • 5
  • 38
  • 56