function-composition

How to combine two procs into one?

╄→尐↘猪︶ㄣ 提交于 2019-12-21 10:52:21
问题 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 working with things like method(:abc).to_proc and :xyz.to_proc 回答1: More sugar, not really recommended in production code class Proc def *(other) ->(*args) { self[*other[*args]] } end end a = ->(x){x+1} b = ->(x){x*10} c = b*a c.call(1) #=> 20 回答2: a =

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

最后都变了- 提交于 2019-12-21 07:58:06
问题 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: > (>>);; val it : (('a -> 'b) -> ('b -> 'c) -> 'a -> 'c) = <fun:it@214-13> > (<<);; val it : (('a -> 'b) -> ('c -> 'a) -> 'c -> 'b) = <fun:it@215-14> What I'd like to do, however, is something like the following: let add a b = a + b let double c = 2*c let

Python function composition

◇◆丶佛笑我妖孽 提交于 2019-12-20 10:44:18
问题 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)) return _compfunc(f) def __rshift__(self, y): f = lambda *args, **kwargs: y(self.func(*args, **kwargs)) return _compfunc(f) def composable(f): return _compfunc(f) @composable def f1(x): return x * 2 @composable def f2(x): return x + 3 @composable def f3(x): return (-1)

Why is function composition in Haskell right associative?

蓝咒 提交于 2019-12-20 09:28:37
问题 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 Haskell (i.e. the juxtaposition of terms, not the $ operation) is left associative in my opinion function composition should also be left associative. After all most people in the world (including myself) are used to reading from left to right.

A function composition operator in Python

丶灬走出姿态 提交于 2019-12-20 05:25:51
问题 In this question I asked about a function composition operator in Python. @Philip Tzou offered the following code, which does the job. import functools class Composable: def __init__(self, func): self.func = func functools.update_wrapper(self, func) def __matmul__(self, other): return lambda *args, **kw: self.func(other.func(*args, **kw)) def __call__(self, *args, **kw): return self.func(*args, **kw) I added the following functions. def __mul__(self, other): return lambda *args, **kw: self

Haskell: type inference and function composition

好久不见. 提交于 2019-12-18 12:56:12
问题 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

Tuple and function composition

天涯浪子 提交于 2019-12-18 06:09:18
问题 Is there a better way to express (\(a, b) -> a < b) with function composition? I feel like I'm missing something and experimenting with curry only confused me more. 回答1: curry is the wrong thing to use here; it turns a function operating on tuples into a curried function. You want the opposite, which is uncurry: uncurry :: (a -> b -> c) -> (a, b) -> c In this case, it's uncurry (<) . (Another useful source for combinators useful in writing functions on tuples is Control.Arrow; since (->) is

Haskell: composing function with two floating arguments fails

好久不见. 提交于 2019-12-17 20:15:57
问题 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

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

◇◆丶佛笑我妖孽 提交于 2019-12-17 04:12:29
问题 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 . h But when you apply something to the end of those functions I write it like this: k = a $ b $ c $ value But the book would write it like this: k = a . b . c $ value Now, to me they look functionally equivalent, they do the exact same thing in my eyes.

Combine 2 list functions into 1?

只愿长相守 提交于 2019-12-12 02:08:46
问题 How would I combine the following 2 functions: replaceNth n newVal (x:xs) | n == 0 = newVal:xs | otherwise = x:replaceNth (n-1) newVal xs replaceMthNth m n v arg = replaceNth m (replaceNth n v (arg !! m)) arg into a single function? Is it possible? 回答1: This is pretty hideous but it does the job: replacemn 0 0 z ((x : xs) : xss) = (z : xs) : xss replacemn 0 n z ((x : xs) : xss) = let (ys : yss) = replacemn 0 (n-1) z (xs : xss) in ((x : ys) : yss) replacemn m n z (xs:xss) = xs : replacemn (m-1