[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
E-penis measuring
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 35
Thread images: 2
File: composition-of-function-image.png (6 KB, 298x227) Image search: [Google]
composition-of-function-image.png
6 KB, 298x227
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?
>>
File: Screenshot_1.png (6 KB, 1207x66) Image search: [Google]
Screenshot_1.png
6 KB, 1207x66
>>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 be
f = 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.
>>
>>55093354
function compose() {
var args=arguments;

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

return x;
}
}
>>
>>55093354
def compose(x, *args):
tmp = x
for f in args[::-1]:
tmp = f(tmp)
return tmp
>>
>>55093247
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);
>>
>>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 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))
>>
>>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 does
from 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...
>>
>>55093247
compose 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 then
def 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?
Thread replies: 35
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.