Implement a function composition function. Given a, b, c are functions, write a function compose, such thatf = compose(a, b, c)
f(x) = a(b(c(x)))
No cheating, if your language already has a function composition operator/function, write your own.
var compose = (f, ...fs) => (...args) => fs.reduce((a, f) => f(a), f(...args))
In js I can almost copy your example and get it working. Is that cheating?
>>55093281
Liar
def compose(a, b, c, x):
a(b(c(x)))
Pretty simple.function compose(a,b,c){
return function(x){ return a(b(c(x))); }
}
Usage:function a(x){ return x*10; }
function b(x){ return x-5; }
function c(x){ return -x; }
console.log(a(b(c(5))));
function compose(a,b,c){
return function(x){ return a(b(c(x))); }
}
var f=compose(a, b, c)
console.log(f(5));
Outputs -100 twice.
>>55093305
Well done, you fucked up.
>>55093305
usage would bef = compose
f(a, b, c, x)
>>55093305
>>55093323
What about infinite arguments?
compose(a, b, c, ...x, y, z)
>>55093323
i did?
>>55093352
>>55093370
As defined in OP, f should be accepting just one argument, x, not four.
compose should return a function - your compose returns value.
>>55093354function compose() {
var args=arguments;
return function(x){
for (var i = args.length-1; i >=0; i--) {
x=args[i](x);
}
return x;
}
}
>>55093354def compose(x, *args):
tmp = x
for f in args[::-1]:
tmp = f(tmp)
return tmp
>>55093247my $a = sub { $_[0] + 1 };
my $b = sub { $_[0] + 2 };
my $c = sub { $_[0] + 3 };
sub compose {
my ($a, $b, $c) = @_;
$a->($b->($c->(0)));
}
print compose($a, $b, $c);
>>55093417
And again you wrote compose that returns value instead of returning a function.
>>55093435
F-. See me after class.
STOP DOING SOME DIPSHIT'S HOMEWORK
>>55093444
i'm aware of that. i can't into doing this in python.
>>55093543
You should work on that. Python has proper closures so there's no way function composition function would be impossible.
>>55093543
i mean you could do it with a lambda:def compose(a, b, c):
return lambda x: a(b(c(x)))
but that wouldn't let you input indefinite arguments
>>55093616
Yes it wouldlambda *args: //something
is valid syntax. That being said:def compose(f, *fs):
return lambda *args: reduce(lambda a, f: f(a), fs, f(*args))
>>55093752
yeah, python 3 doesn't have reduce for readability reasons (which is bollocks if you ask me, but alright)
thanks for the enlightenment. i was going to do something tail recursive next, but now i've lost interest.
>>55093870
It doesfrom functools import reduce
>>55093870
You can do it with a simple loop just like you did in >>55093417.
>>55093752
Compose should be returning a function that only accepts one argument. What are you doing?
>>55093928
B-but this guy >>55093354 said...
>>55093247compose a b c x = a (b (c (x)))
>>55093942
Compose accepts any number of arguments, and returns a function that only accepts one.
f = compose(a, b, c, d, e)
f(x) = a(b(c(d(e(x)))))
>>55093962
Ok thendef compose(f, *fs):
return lambda arg: reduce(lambda a, f: f(a), fs, f(arg))
>>55093949
One thing Haskell is actually good at, huh
>>55093969
Well, I don't know python so I can't say for sure, but it looks like it's the right answer.
>>55093949
Why don't you use $ instead of ()? It's too readable, the pleb might understand.
>>55094012
Do they even teach Haskell anywhere?
>>55094031
Of course, on /g/
I'm sure you can do this with fewer arity overloads, but this was just what popped into my head:(defn compose
([f] f)
([f g]
(fn [& args]
(f (apply g args))))
([f g & fs]
(reduce compose (list* f g fs))))
def compose(a,b, c):
return lambda *args: a(b(c(*args))
just that?