Last active
March 10, 2017 07:06
-
-
Save 7-fl/3a2809cfb6ccd5b3b98f1be8c2f75ebf to your computer and use it in GitHub Desktop.
iterate(), twice(), etc.
This file contains hidden or 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
| -module(w3). | |
| -compile(export_all). | |
| -include_lib("eunit/include/eunit.hrl"). | |
| iterate_test() -> | |
| Iter1 = iterate(1), | |
| Iter2 = iterate(2), | |
| Iter3 = iterate(3), | |
| F1 = Iter1(fun(X) -> X+2 end), | |
| 3+2 = F1(3), | |
| F2 = Iter2(fun(X) -> X*2 end), | |
| 3*2*2 = F2(3), | |
| F3 = Iter3(fun(X) -> X-1 end), | |
| 0-1-1-1 = F3(0), | |
| all_tests_passed. | |
| iterate(N) -> | |
| fun(Func) -> | |
| fun(X) -> ( func_n_times(Func, N) )(X) end | |
| end. | |
| %-------------- | |
| func_n_times_test() -> | |
| F = fun(X) -> X+2 end, | |
| G = fun(X) -> X*2 end, | |
| 1+2+2 = (func_n_times(F, 2))(1), | |
| 1*2*2*2 = (func_n_times(G, 3))(1), | |
| all_tests_passed. | |
| func_n_times(Func, N) -> | |
| lists:foldl( | |
| fun(F, Comp) -> | |
| fun(X) -> F(Comp(X)) end | |
| end, | |
| fun(X) -> X end, | |
| lists:duplicate(N, Func) | |
| ). | |
| % Old school tail recursion: | |
| % | |
| %func_n_times(Func, N) -> | |
| % func_n_times(Func, N, []). | |
| % | |
| %func_n_times(_Func, 0, Funcs) -> | |
| % compose(Funcs); | |
| %func_n_times(Func, N, Funcs) -> | |
| % func_n_times(Func, N-1, [Func|Funcs]). | |
| %--------------- | |
| twice_test() -> | |
| F = fun(X) -> X*2 end, | |
| G = fun(X) -> X+10 end, | |
| 1*2*2 = (twice(F))(1), | |
| 0+10+10 = (twice(G))(0), | |
| 1*2*2*2*2 = (twice(twice(F)))(1), | |
| 0+10+10+10+10 = (twice(twice(G)))(0), | |
| Mult = fun(X) -> fun(Y) -> X*Y end end, | |
| Mult3 = Mult(3), | |
| 3*(1*2*2*2*2) = Mult3((twice(twice(F)))(1)), | |
| 3*(0+10+10+10+10) = Mult3((twice(twice(G)))(0)), | |
| 3*(1+10+10+10+10) = Mult3((twice(twice(G)))(1)), | |
| 3*(0*2*2*2*2) = Mult3((twice(twice(F)))(0)), | |
| all_tests_passed. | |
| twice(F) -> | |
| compose([F,F]). | |
| %------------- | |
| compose_test() -> | |
| F = fun(X) -> X+2 end, | |
| G = fun(X) -> X*2 end, | |
| H = fun(X) -> X-2 end, | |
| Comp1 = compose([F, G]), | |
| Comp2 = compose([G, F]), | |
| (3+2)*2 = Comp1(3), | |
| (3*2)+2 = Comp2(3), | |
| (4+2)*2 = Comp1(4), | |
| (4*2)+2 = Comp2(4), | |
| Comp3 = compose([F, G, H]), | |
| Comp4 = compose([H, G, F]), | |
| ((2+2)*2)-2 = Comp3(2), | |
| ((2-2)*2)+2 = Comp4(2), | |
| all_tests_passed. | |
| compose([F|Fs]) -> | |
| lists:foldl( | |
| fun(Func, Comp) -> | |
| fun(X) -> Func(Comp(X) ) end | |
| end, | |
| F, | |
| Fs | |
| ). | |
| %Old school tail recursion: | |
| % | |
| %compose(Fs) -> compose(Fs, fun(X) -> X end). | |
| % | |
| %compose([], CompFunc) -> | |
| % CompFunc; | |
| %compose([F|Fs], Comp) -> | |
| % compose(Fs, fun(X) -> F(Comp(X)) end). | |
| % |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment