function-composition

Specify function composition through declarative maps in F#

扶醉桌前 提交于 2019-12-01 00:37:30
The Clojure Prismatic/Plumbing library can be used in order to provide a declarative and explicit definition of an application or module functions' graph. In short, it provides a means to specify each function as a node with a label, which is also the output label, the labeled inputs, and an implementation. It uses a custom keyword (fkn) defined in a macro for this purpose. We have to develop a module in F# which performs relatively complex calculations in a hierarchical fashion that could benefit from Prismatic features, namely: A graph can be built easily from function map, just taking

Specify function composition through declarative maps in F#

久未见 提交于 2019-11-30 19:01:50
问题 The Clojure Prismatic/Plumbing library can be used in order to provide a declarative and explicit definition of an application or module functions' graph. In short, it provides a means to specify each function as a node with a label, which is also the output label, the labeled inputs, and an implementation. It uses a custom keyword (fkn) defined in a macro for this purpose. We have to develop a module in F# which performs relatively complex calculations in a hierarchical fashion that could

How do I pass in multiple parameters into a Ramda compose chain?

廉价感情. 提交于 2019-11-30 10:34:35
Here are four functions I am trying to compose into a single endpoint string: const endpoint = str => `${str}` || 'default' const protocol = str => `https://${str}` const params = str => `${str}?sort=desc&part=true&` const query = str => `${str}query={ some:'value', another:'value'}` let finalEndpoint = R.compose(query, params, protocol, endpoint) var result = finalEndpoint('api.content.io') This composition works and returns the result I want which is: https://api.content.io?sort=desc&part=true&query={ some:'value', another:'value'} But notice how I have hard coded the values for params and

Haskell: type inference and function composition

浪子不回头ぞ 提交于 2019-11-30 08:26:28
This question was inspired by this answer to another question, indicating that you can remove every occurrence of an element from a list using a function defined as: removeall = filter . (/=) Working it out with pencil and paper from the types of filter , (/=) and (.) , the function has a type of removeall :: (Eq a) => a -> [a] -> [a] which is exactly what you'd expect based on its contract. However, with GHCi 6.6, I get gchi> :t removeall removeall :: Integer -> [Integer] -> [Integer] unless I specify the type explicitly (in which case it works fine). Why is Haskell inferring such a specific

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

笑着哭i 提交于 2019-11-30 06:39:15
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]) 3 where f = const Nothing g = undefined This hits prelude.Undefined . Which is odd, because I would

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

﹥>﹥吖頭↗ 提交于 2019-11-29 22:42:04
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 matching up with its signature): ((*) . (+)) 1 (\x -> x + 1) 1 But that fails to compile. I'm trying to

Haskell function composition, type of (.)(.) and how it's presented

自闭症网瘾萝莉.ら 提交于 2019-11-29 16:10:09
问题 So i know that: (.) = (f.g) x = f (g x) And it's type is (B->C)->(A->B)->A->C But what about: (.)(.) = _? = _? How this is represented? I thought of: (.)(.) = (f.g)(f.g)x = f(g(f(g x))) // this (.)(.) = (f.g.h)x = f(g(h x)) // or this But as far as i tried to get type of it, it's not correct to what GHCi tells me. So what are both "_?" Also - what does function/operator $ do? 回答1: First off, you're being sloppy with your notation. (.) = (f.g) x = f (g x) -- this isn't true What is true: (.) f

How do I pass in multiple parameters into a Ramda compose chain?

三世轮回 提交于 2019-11-29 15:52:40
问题 Here are four functions I am trying to compose into a single endpoint string: const endpoint = str => `${str}` || 'default' const protocol = str => `https://${str}` const params = str => `${str}?sort=desc&part=true&` const query = str => `${str}query={ some:'value', another:'value'}` let finalEndpoint = R.compose(query, params, protocol, endpoint) var result = finalEndpoint('api.content.io') This composition works and returns the result I want which is: https://api.content.io?sort=desc&part

Typescript recursive function composition

﹥>﹥吖頭↗ 提交于 2019-11-29 12:30:06
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) => Tmp1, (i: Tmp1) => Tmp2, ...Chain<Tmp2, Out>]; The idea is in the draft. This however produces tho

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

十年热恋 提交于 2019-11-29 11:20:15
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. 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 right associative way. If you look at the implementations of and , sum , maximum and similar, you'll see that