function-composition

Is it possible in C++11 to combine functions into a new function?

痴心易碎 提交于 2021-02-20 18:51:54
问题 This is more a kind of theoretical question. Is it possible in C++11 to combine functions into a new function? For example : auto f = [](int i){return i * 2;}; auto g = [](int i){return i + 10;}; So this works: auto c = f(g(20)); // = 60 But I want an object that stores the combination, like auto c = f(g); std::cout << c(20) << std::endl; //prints 60 Edit: Additionally what i want to create is a function a, which you can give a function b and an int n , and which returns the n'th combination

Repeat a function composition n times in Python like Haskell's repeat

只谈情不闲聊 提交于 2020-12-26 18:51:18
问题 This code does NOT work: def inc(x): return x + 1 def repeat(f, n): if n == 0: return lambda x: x else: return f( repeat(f, n - 1 ) ) inc_10 = repeat(inc, 10) #TypeError: unsupported operand type(s) for +: 'function' and 'int' ''' # Ideally print(inc_10(0)) # 10 ''' How can I write it in a more Pythonic way or in lambda calculus way ? 回答1: You still need to return a function, not the result of calling f , in the recursive case. # repeat :: (a -> a) -> Integer -> a -> a # repeat _ 0 = id #

Repeat a function composition n times in Python like Haskell's repeat

若如初见. 提交于 2020-12-26 18:49:43
问题 This code does NOT work: def inc(x): return x + 1 def repeat(f, n): if n == 0: return lambda x: x else: return f( repeat(f, n - 1 ) ) inc_10 = repeat(inc, 10) #TypeError: unsupported operand type(s) for +: 'function' and 'int' ''' # Ideally print(inc_10(0)) # 10 ''' How can I write it in a more Pythonic way or in lambda calculus way ? 回答1: You still need to return a function, not the result of calling f , in the recursive case. # repeat :: (a -> a) -> Integer -> a -> a # repeat _ 0 = id #

Python function composition (max recursion depth error, scope?)

房东的猫 提交于 2020-01-17 01:10:36
问题 What is wrong with this function? It seems like a scope error (although I thought I had fixed that by placing each callable in the list, instead of using it directly). Error is max recursion depth reached (when calling comp(inv,dbl,inc))... Note: the question is: why is it even recursing, not why it's reaching the max depth... def comp(*funcs): if len(funcs) in (0,1): raise ValueError('need at least two functions to compose') # get most inner function composed = [] print("appending func 1")

Python function composition (max recursion depth error, scope?)

纵然是瞬间 提交于 2020-01-17 01:10:32
问题 What is wrong with this function? It seems like a scope error (although I thought I had fixed that by placing each callable in the list, instead of using it directly). Error is max recursion depth reached (when calling comp(inv,dbl,inc))... Note: the question is: why is it even recursing, not why it's reaching the max depth... def comp(*funcs): if len(funcs) in (0,1): raise ValueError('need at least two functions to compose') # get most inner function composed = [] print("appending func 1")

How to know whether a symbol represents function or macro?

血红的双手。 提交于 2020-01-14 02:28:10
问题 I'm writing a macro for function / macro composition (mixed combinations are possible). Inside of the macro I have to treat symbols which represent functions and those that name macros differently. It is because result function must work with any number of arguments (if 'lowest' function in composition can), and I cannot apply apply to macros. My question: how to determine what a given symbol represents: function or macro? 回答1: Macro: CL-USER 8 > (macro-function 'bar) NIL CL-USER 9 > (macro

How can I use an operator to compose functions in Python?

强颜欢笑 提交于 2020-01-05 03:43:29
问题 It's fairly straightforward to write a function that composes two other functions. (For simplicity, assume they are one parameter each.) def compose(f, g): fg = lambda x: f(g(x)) return fg def add1(x): return x + 1 def add2(x): return x + 2 print(compose(add1, add2)(5)) # => 8 I would like to do composition using an operator, e.g., (add1 . add2)(5) . Is there a way to do that? I tried various decorator formulations, but I couldn't get any of them to work. def composable(f): """ Nothing I

How to functional compose transforms of objects via transducers

江枫思渺然 提交于 2020-01-02 14:32:15
问题 Live code example I'm trying to learn transducers via egghead and I think I got it until we try to compose object transformation. I have an example below that doesn't work const flip = map(([k,v]) => ({[v]: k})); const double = map(([k,v]) => ({[k]: v + v})); seq(flip, {one: 1, two: 2}); /*?*/ {1: 'one', 2: 'two'} seq(double, {one: 1, two: 2}); /*?*/ {'one': 2, 'two: 4} but if I compose it fails: seq(compose(flip, double), {one: 1, two: 2}); /*?*/ {undefined: NaN} seq(compose(double, flip),