r/dailyprogrammer • u/Godspiral 3 3 • Dec 11 '15
[2015-12-09] Challenge #244 [Easy]er - Array language (part 3) - J Forks
This challenge does not require doing the previous 2 parts. If you want something harder, the rank conjunction from Wednesday's challenge requires concentration.
Forks
A fork is a function that takes 3 functions that are all "duck defined" to take 2 parameters with 2nd optional or ignorable.
for 3 functions, f(y,x= default): , g(y,x= default): , h(y,x= default): , where the function g is a "genuine" 2 parameter function,
the call Fork(f,g,h) executes the function composition:
g(f(y,x),h(y,x)) (data1,data2)1. Produce the string that makes the function call from string input:
sum divide count(above input are 3 function names to Fork)
2. Native to your favorite language, create an executable function from above string input
or 3. create a function that takes 3 functions as input, and returns a function.
Fork(sum, divide ,count) (array data)should return the mean of that array. Where divide works similarly to add from Monday's challenge.
4. Extend above functions to work for any odd number of function parameters
for 5 parameters, Fork(a, b, c, d, e) is:
b(a, Fork(c,d,e)) NB. should expand this if producing strings. challenge input
(25 functions)
a b c d e f g h i j k l m n o p q r s t u v w x y
9
u/cheers- Dec 11 '15
I think this challenge is not suited for a strongly typed language(Java) and it is definitely not Easy.
I can easily compose functions but only if their return type is a valid type for the second function argument.
For instance a set of functions that accept type E and return type E can be easily composed with this method:
Your challenge input could be done in java only if:
a returns valid type for b, b returns valid type for c etc...
for your input:
3
u/smls Dec 12 '15 edited Dec 12 '15
Perl 6
(Translation of fibonacci__'s Python solution.)
3
u/wizao 1 0 Dec 12 '15 edited Dec 12 '15
I didn't bother implementing the code because the unsafe types make it a pain to accomplish:
But I think it's fun to share how you could use the idea in code.
Forkis Haskell'sliftA2for a function's applicative instance.You'd have to split up the functions based on their parametricity to be able to do something like
2
u/obeoj Dec 11 '15 edited Dec 11 '15
c#
Examples:
3
u/fibonacci__ 1 0 Dec 11 '15 edited Dec 11 '15
Python
Input
Output
3
u/casualfrog Dec 11 '15
JavaScript (feedback welcome, of course!)
Not sure if I understood everything correctly. Here's my take:
Usage:
2
u/quickreply100 Dec 11 '15
I'm not 100% sure I did this right but here's a quick implementation in Javascript (I didn't do the last point)
2
u/kirsybuu 0 1 Dec 14 '15
D Language, both compile-time (fully flexible) and runtime (can't use default params):
Borrowed examples from /u/fibonacci__: