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
798
votes
13 answers

What is the difference between . (dot) and $ (dollar sign)?

What is the difference between the dot (.) and the dollar sign ($)? As I understand it, they are both syntactic sugar for not needing to use parentheses.
Rabarberski
  • 23,854
  • 21
  • 74
  • 96
141
votes
7 answers

Haskell function composition (.) and function application ($) idioms: correct use

I have been reading Real World Haskell, and I am nearing the end, but a matter of style has been niggling at me to do with the (.) and ($) operators. When you write a function that is a composition of other functions you write it like: f = g .…
Robert Massaioli
  • 13,379
  • 7
  • 57
  • 73
103
votes
6 answers

Dot Operator in Haskell: need more explanation

I'm trying to understand what the dot operator is doing in this Haskell code: sumEuler = sum . (map euler) . mkList The entire source code is below. My understanding The dot operator is taking the two functions sum and the result of map euler and…
cbrulak
  • 15,436
  • 20
  • 61
  • 101
60
votes
6 answers

Haskell function composition operator of type (c→d) → (a→b→c) → (a→b→d)

Ordinary function composition is of the type (.) :: (b -> c) -> (a -> b) -> a -> c I figure this should generalize to types like: (.) :: (c -> d) -> (a -> b -> c) -> a -> b -> d A concrete example: calculating difference-squared. We could write…
jameshfisher
  • 34,029
  • 31
  • 121
  • 167
57
votes
3 answers

runST and function composition

Why does this typecheck: runST $ return $ True While the following does not: runST . return $ True GHCI complains: Couldn't match expected type `forall s. ST s c0' with actual type `m0 a0' Expected type: a0 -> forall s. ST s c0 …
Grzegorz Chrupała
  • 3,053
  • 17
  • 24
53
votes
2 answers

function composition with reverse syntax

If I want to apply f first and g second, I have to write: g . f Is there another standard syntax that would allow me to write the functions in the reverse order? f g I know I can just invent my own syntax: compose f g x = g (f x) and…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
52
votes
2 answers

What does (f .) . g mean in Haskell?

I have seen a lot of functions being defined according to the pattern (f .) . g. For example: countWhere = (length .) . filter duplicate = (concat .) . replicate concatMap = (concat .) . map What does this mean?
45
votes
5 answers

What is happening when I compose * with + in Haskell?

I'm trying to understand the result of (*) . (+) in Haskell. I know that the composition operator is just the standard composition of mathematical functions- so (f . g) = f (g x) But: (*) . (+) :: (Num (a -> a), Num a) => a -> (a -> a) -> a ->…
user2666425
  • 1,671
  • 1
  • 15
  • 21
42
votes
16 answers

Composing functions in python

I have an array of functions and I'm trying to produce one function which consists of the composition of the elements in my array. My approach is: def compose(list): if len(list) == 1: return lambda x:list[0](x) list.reverse() …
40
votes
3 answers

Haskell Monad bind operator confusion

Okay, so I am not a Haskell programmer, but I am absolutely intrigued by a lot of the ideas behind Haskell and am looking into learning it. But I'm stuck at square one: I can't seem to wrap my head around Monads, which seem to be fairly fundamental.…
Ord
  • 5,693
  • 5
  • 28
  • 42
34
votes
1 answer

Why is function composition in Haskell right associative?

Mathematically the function composition operation is associative. Hence: f . (g . h) = (f . g) . h Thus the function composition operation may be defined to be either left associative or right associative. Since normal function application in…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
33
votes
6 answers

Elegant way to combine multiple filtering functions in Haskell

Given the following filtering functions as unary predicates, f1 :: Int -> Bool f1 x = x > 30 f2 :: Int -> Bool f2 x = x < 60 f3 :: Int -> Bool f3 x = x `mod` 3 == 0 I'd like to filter a list of integers through all of them. Currently I'm doing…
Jivan
  • 21,522
  • 15
  • 80
  • 131
32
votes
4 answers

How to multiply functions in python?

def sub3(n): return n - 3 def square(n): return n * n It's easy to compose functions in Python: >>> my_list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [square(sub3(n)) for n in my_list] [9, 4, 1, 0, 1, 4, 9, 16, 25, 36] Unfortunately, to use the…
wim
  • 338,267
  • 99
  • 616
  • 750
28
votes
1 answer

Understanding `andThen`

I encountered andThen, but did not properly understand it. To look at it further, I read the Function1.andThen docs def andThen[A](g: (R) ⇒ A): (T1) ⇒ A mm is a MultiMap instance. scala> mm res29:…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
26
votes
6 answers

What does a fullstop or period or dot (.) mean in Haskell?

I really wish that Google was better at searching for syntax: decades :: (RealFrac a) => a -> a -> [a] -> Array Int Int decades a b = hist (0,9) . map decade where decade x = floor ((x - a) * s) …
Casebash
  • 114,675
  • 90
  • 247
  • 350
1
2 3
27 28