function-composition

Folding, function composition, monads, and laziness, oh my?

感情迁移 提交于 2019-11-29 05:53:37
问题 I am puzzled. I can write this: import Control.Monad main = print $ head $ (foldr (.) id [f, g]) [3] where f = (1:) g = undefined and the output is 1 . That makes sense, because it reduces to: main = print $ head $ ((1:) . undefined . id) [3] main = print $ head $ (1:) ((undefined . id) [3]) main = print $ head $ 1 : ((undefined . id) [3]) main = print $ 1 But if I use a vaguely similar monadic technique, it doesn't work the same: import Control.Monad main = print $ (foldr (<=<) return [f, g]

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

蓝咒 提交于 2019-11-28 21:16:49
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) s = 10 / (b - a) Pratik Deoghare f(g(x)) is in mathematics : f ∘ g (x) in haskell : ( f . g ) (x) Alex Jenter It means function composition. See this question . Note also the f.g.h x is not equivalent to (f.g.h) x , because it is interpreted as f.g.(h x) which won't typecheck unless (h x) returns a function. This is where the $ operator can come in handy: f.g.h $ x turns x from being a parameter to h

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

时光总嘲笑我的痴心妄想 提交于 2019-11-28 18:26:46
问题 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 -> a I'm struggling to understand this type signature. I would have expected to be able to do things like: ((*) . (+)) 1 2 :: Num a => a -> a = (* (+ 1 2)) What is the meaning of (*) . (+)'s type signature? I tried playing with it by something like (just

Haskell: composing function with two floating arguments fails

爷,独闯天下 提交于 2019-11-28 11:28:50
I am trying to compose a function of type (Floating a) => a -> a -> a with a function of type (Floating a) => a -> a to obtain a function of type (Floating a) => a -> a -> a . I have the following code: test1 :: (Floating a) => a -> a -> a test1 x y = x test2 :: (Floating a) => a -> a test2 x = x testBoth :: (Floating a) => a -> a -> a testBoth = test2 . test1 --testBoth x y = test2 (test1 x y) However, when I compile it in GHCI, I get the following error: /path/test.hs:8:11: Could not deduce (Floating (a -> a)) from the context (Floating a) arising from a use of `test2' at /path/test.hs:8:11

Variadic compose function?

纵饮孤独 提交于 2019-11-28 09:11:29
I'm trying to write a variadic function composition function. Which is basically the (.) except that the second argument function is variadic. This should allow expressions like: map even . zipWith (+) or just map even . zipWith Currently what I've reached works if I add IncoherentInstances and requires a non-polymorphic instance for the first argument function. {-# LANGUAGE FlexibleInstances, OverlappingInstances, MultiParamTypeClasses, FunctionalDependencies, UndecidableInstances, KindSignatures #-} class Comp a b c d | c -> d where comp :: (a -> b) -> c -> d instance Comp a b (a :: *) (b ::

Typescript recursive function composition

旧街凉风 提交于 2019-11-28 05:59:10
问题 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 the data flow in the chain. - Argument of a function is result of the previous one - First argument is a template parameter - Last return is a template parameter type Chain<In, Out, Tmp1 = any, Tmp2 = any> = [] | [(arg: In) => Out] | [(arg: In) =>

Most idiomatic implementation of `[a -> a] -> (a -> a)`

旧城冷巷雨未停 提交于 2019-11-28 04:46:20
问题 If I have a list of functions, each of the type a -> a for some type, what is the most shortest, elegant and idiomatic way to combine them; preferably without adding extra dependencies? Some variants include foo (x:xs) = x . (foo xs) foo [] = id and foo = foldr (.) id and foo = appEndo . mconcat . map Endo but for some reason I’m expecting to find something nicer. 回答1: I'd say you're not going to beat comp = foldr (.) id Why? Well we have a list of things and we're trying to reduce it in a

How to reconcile Javascript with currying and function composition

喜欢而已 提交于 2019-11-28 04:39:41
问题 I love currying but there are a couple of reasons why a lof of Javascript devs reject this technique: aesthetic concerns about the typical curry pattern: f(x) (y) (z) concerns about performance penalties due to the increased number of function calls concerns about debugging issues because of the many nested anonymous functions concerns about readability of point-free style (currying in connection with composition) Is there an approach that can mitigate these concerns so that my coworkers don

How to multiply functions in python?

我怕爱的太早我们不能终老 提交于 2019-11-27 20:26:25
def sub3(n): return n - 3 def square(n): return n * n It's dead 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, when wanting to use the composition as a key , it's kind of lame: >>> sorted(my_list, key=lambda n: square(sub3(n))) [3, 2, 4, 1, 5, 0, 6, 7, 8, 9] This should really just be sorted(my_list, key=square*sub3) , because heck, function __mul__ isn't used for anything else anyway: >>> square * sub3 TypeError: unsupported operand type(s) for *: 'function' and 'function'

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

故事扮演 提交于 2019-11-27 13:40:19
问题 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) s = 10 / (b - a) 回答1: f(g(x)) is in mathematics : f ∘ g (x) in haskell : ( f . g ) (x) 回答2: It means function composition. See this question. Note also the f.g.h x is not equivalent to (f.g.h) x , because it is interpreted as f.g.(h x) which won't typecheck unless (h x) returns a function. This is