function-composition

Function composition of methods, functions, and partially applied functions in Scala

守給你的承諾、 提交于 2019-12-04 16:14:32
问题 Somewhat similar to Stack Overflow question Compose and andThen methods , I've been working through Twitter's Scala School tutorial and quickly ran into the same problem that a commenter had (which was great, because I went to bed thinking my problem was solved). In the tutorial, it defines two methods as such: def addUmm(x: String) = x + " umm" def addAhem(x: String) = x + " ahem" and while in newer versions of Scala, you can't call compose on them as such: addUmm(_).compose(addAhem(_)) ,

Haskell Monad bind operator confusion

萝らか妹 提交于 2019-12-04 07:51:34
问题 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. I know there are a million questions on SO asking to explain Monads, so I'm going to be a little more specific about what's bugging me: I read this excellent article (an introduction in Javascript), and thought that I understood Monads completely.

How to combine two procs into one?

纵饮孤独 提交于 2019-12-04 05:00:24
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 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 a = Proc.new { |x| x + 1 } b = Proc.new { |x| x * 10 } c = Proc.new { |x| b.call(a.call(x)) } you could create a union

Is there a way to chain functions like withCString?

谁都会走 提交于 2019-12-04 00:33:45
Is there a way to chain functions like withCString ? By that I mean any function that looks something like f :: Foo -> (CFoo -> IO a) -> IO a . For example, lets say there is a function cFunc :: CString -> CFoo -> CBar -> IO () Usualy, I would do something like: haskellFunc string foo bar = withCString string $ \ cString -> withCFoo foo $ \ cFoo -> withCBar bar $ \ cBar -> cFunc cString cFoo cBar But i would like to do something like: haskellFunc = (withCString |.| withCFoo |.| withCBar) cFunc with some appropriate composition operator |.| . I'm writing library with a lot of C bindings, and

Graphing n iterations of a function- Python

岁酱吖の 提交于 2019-12-03 21:59:05
I'm studying dynamical systems, particularly the logistic family g(x) = cx(1-x), and I need to iterate this function an arbitrary amount of times to understand its behavior. I have no problem iterating the function given a specific point x_0, but again, I'd like to graph the entire function and its iterations, not just a single point. For plotting a single function, I have this code: import numpy as np import scipy as sp import matplotlib.pyplot as plt def logplot(c, n = 10): dt = .001 x = np.arange(0,1.001,dt) y = c*x*(1-x) plt.plot(x,y) plt.axis([0, 1, 0, c*.25 + (1/10)*c*.25]) plt.show() I

compose function and functional module

橙三吉。 提交于 2019-12-03 16:26:58
问题 Python 3.2 documentation refers to Collin Winter's functional module which contains function compose : The compose() function implements function composition. In other words, it returns a wrapper around the outer and inner callables, such that the return value from inner is fed directly to outer. Unfortunately, this module hasn't been updated since July 2006; I wonder if there's any replacement available. For now, I only need compose function. Is the following original functional.compose

Function Composition VS Function Application

六月ゝ 毕业季﹏ 提交于 2019-12-03 15:50:43
Do anyone can give example of function composition? This is the definition of function composition operator? (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) This shows that it takes two functions and return a function but i remember someone has expressed the logic in english like boy is human -> ali is boy -> ali is human What this logic related to function composition? What is the meaning of strong binding of function application and composition and which one is more strong binding than the other? Please help. Thanks. ( Edit 1: I missed a couple components of your question the

Composing a Java Function and Consumer

走远了吗. 提交于 2019-12-03 12:57:55
问题 What is the best way to functionally compose a java Function and a Consumer ? For example given some Function<Object, String> f and some Consumer<String> c then doing f.andThen(c) would feel natural, however that is not how the interfaces work. The two options I see are either replace Consumer<String> c with Function<String, Void> c or change Consumer<String> c to BiConsumer<Function<Object, String>, String> c and do accept(Function<Object, String> f, Object o) { String thing = f.apply(o); /

composition with dyadic operator?

二次信任 提交于 2019-12-03 12:53:57
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 . (++)), all to no avail, in that the type of (++) does not match the expected type of nub ( [a] ). I could of course define an auxiliary function or a lambda, but I think that probably there is a composition which would be clearer. Hints please! You can write this as ((nub .) .) (++) Example: Prelude Data.List> ((nub .) .) (++) [1,2,3] [3,4,5] [1,2,3,4,5]

Is it a good idea to have a syntax sugar to function composition in Python?

时间秒杀一切 提交于 2019-12-03 12:06:31
问题 Some time ago I looked over Haskell docs and found it's functional composition operator really nice. So I've implemented this tiny decorator: 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