E-penis measuring

Implement a function composition function. Given a, b, c are functions, write a function compose, such that
f = 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?

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.

Well done, you fucked up.

usage would be
f = compose
f(a, b, c, x)

What about infinite arguments?
compose(a, b, c, ...x, y, z)

i did?

As defined in OP, f should be accepting just one argument, x, not four.
compose should return a function - your compose returns value.

function compose() {
var args=arguments;

return function(x){
for (var i = args.length-1; i >=0; i--) {
x=args[i](x);
}

return x;
}
}

def compose(x, *args):
tmp = x
for f in args[::-1]:
tmp = f(tmp)
return tmp

my $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);

And again you wrote compose that returns value instead of returning a function.

F-. See me after class.

STOP DOING SOME DIPSHIT'S HOMEWORK

i'm aware of that. i can't into doing this in python.

You should work on that. Python has proper closures so there's no way function composition function would be impossible.

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

Yes it would
lambda *args: //something


is valid syntax. That being said:

def compose(f, *fs):
return lambda *args: reduce(lambda a, f: f(a), fs, f(*args))

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.

It does
from functools import reduce

You can do it with a simple loop just like you did in .

Compose should be returning a function that only accepts one argument. What are you doing?

B-but this guy said...

compose a b c x = a (b (c (x)))

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)))))

Ok then
def compose(f, *fs):
return lambda arg: reduce(lambda a, f: f(a), fs, f(arg))

One thing Haskell is actually good at, huh

Well, I don't know python so I can't say for sure, but it looks like it's the right answer.

Why don't you use $ instead of ()? It's too readable, the pleb might understand.

Do they even teach Haskell anywhere?

Of course, on Sup Forums

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))))