Last active
November 29, 2017 19:02
-
-
Save rsp/4255663f8c5a5e58bc9818a6174fe28f to your computer and use it in GitHub Desktop.
inFullMobile Language Wars: Round 1 - Task Summary - See: https://gist.github.com/rsp/d8bdbafa09f24f99eebc8ed60fe205c8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A task for inFullMobile Language Wars: Round 1 | |
TL;DR | |
We need a function f that takes a and returns b: | |
b = f(a) | |
when: | |
a = g => x => g(g(g(x))) | |
then: | |
b = g => x => g(g(x)) | |
More details: | |
By "function" I mean a function, method, subroutine, object, class or whatever makes most sense in the language. | |
Examples below are in old dialect of JavaScript for readability and easy translation to other languages. | |
We don't discuss the solutions before everything is ready to make sure that all solutions are original. | |
Function f() takes a function a() as an argument and returns a function b(), such as: | |
If a() is: | |
function a(x) { | |
return function (y) { | |
return x(x(x(y))); | |
}; | |
} | |
then b() must be equivalent to: | |
function b(x) { | |
return function (y) { | |
return x(x(y)); | |
}; | |
} | |
that is the same as a() but with one less x() invocation. | |
The f() function must be tested to work on the input functions: input1, input2, input3, input4, input5 below. | |
There is a count() function provided below to count the number of x() invocation inside of functions, | |
e.g. for the a() and b() above, count(a) would return 3 and count(b) would return 2. | |
*/ | |
// Sample input functions: | |
// Applies x() 1 time: | |
function input1(x) { | |
return function (y) { | |
return x(y); | |
}; | |
} | |
// Applies x() 2 times: | |
function input2(x) { | |
return function (y) { | |
return x(x(y)); | |
}; | |
} | |
// Applies x() 10 times: | |
function input3(x) { | |
return function (y) { | |
return x(x(x(x(x(x(x(x(x(x(y)))))))))); | |
}; | |
} | |
// Applies x() 100 times: | |
function input4(x) { | |
return input3(input3(x)); | |
} | |
// Applies x() 1000 times: | |
function input5(x) { | |
return input3(input4(x)); | |
} | |
// The count() function for tests: | |
function inc(x) { | |
return x + 1; | |
} | |
function count(f) { | |
return f(inc)(0); | |
} | |
// The f() function implementation: | |
function f(x) { | |
// ... | |
} | |
// Tests of the f() function implementation: | |
count(f(input1)) === count(input1) - 1 // true for all input1, input2, input3, input4, input5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment