Created
March 23, 2017 03:46
-
-
Save shakerlxxv/f5cb908fa87c6c7ef98535c4620d929a to your computer and use it in GitHub Desktop.
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
%%============================================================================== | |
%% Univ. Kent Functional Programing with Erlang | |
%% Week 3: Part 5 - Higher-order functions in practice | |
%% | |
%% Description: | |
%% work with higher order functions 'map','filter', and 'reduce' | |
%% | |
%% Author: Brian L. Shaver | |
%% Date : 2017-03-22 | |
%%============================================================================== | |
-module(ex3_5). | |
-include_lib("eunit/include/eunit.hrl"). | |
-export([doubleAll/1,evens/1,product/1,zip/2,zip_with/3,zip_with2/3,zip2/2]). | |
%------------------------------------------------------------------------------- | |
% doubleAll([]) -> []; | |
% doubleAll([X|Xs]) -> | |
% [ 2*X | doubleAll(Xs) ]. | |
doubleAll(L) -> | |
lists:map(fun(X) -> 2*X end,L). | |
%+++++++++++++++++++++++++++++++++++++++ | |
% unit tests | |
%+++++++++++++++++++++++++++++++++++++++ | |
doubleAll_test_() -> | |
[ | |
?_assertEqual([],doubleAll([])), | |
?_assertEqual([2,4],doubleAll([1,2])) | |
]. | |
%------------------------------------------------------------------------------- | |
% evens([]) -> []; | |
% evens([X|Xs]) when X rem 2 == 0 -> | |
% [X | evens(Xs) ]; | |
% evens([_|Xs]) -> | |
% evens(Xs). | |
evens(L) -> | |
IsEven = fun(X) -> X rem 2 == 0 end, | |
lists:filter(IsEven,L). | |
%+++++++++++++++++++++++++++++++++++++++ | |
% unit tests | |
%+++++++++++++++++++++++++++++++++++++++ | |
evens_test_() -> | |
[ | |
?_assertEqual([],evens([])), | |
?_assertEqual([],evens([1,3])), | |
?_assertEqual([2,4],evens([1,2,3,4])) | |
]. | |
%------------------------------------------------------------------------------- | |
% product([]) -> 1; | |
% product([X|Xs]) -> X * product(Xs). | |
product(L) -> | |
lists:foldr(fun(X,Acc) -> Acc * X end, 1, L). | |
%+++++++++++++++++++++++++++++++++++++++ | |
% unit tests | |
%+++++++++++++++++++++++++++++++++++++++ | |
product_test_() -> | |
[ | |
?_assertEqual(1,product([])), | |
?_assertEqual(6,product([1,2,3])) | |
]. | |
%------------------------------------------------------------------------------- | |
% zip/2 first attempt (without library) | |
zip([],_L) -> | |
[]; | |
zip(_L,[]) -> | |
[]; | |
zip([X|Xs],[Y|Ys]) -> | |
[{X, Y} | zip(Xs,Ys)]. | |
%+++++++++++++++++++++++++++++++++++++++ | |
% unit tests | |
%+++++++++++++++++++++++++++++++++++++++ | |
zip_test_() -> | |
[ | |
?_assertEqual([],zip([],[1,2,3])), | |
?_assertEqual([],zip([1,2,3],[])), | |
?_assertEqual([{1,2},{3,4}],zip([1,3],[2,4,6])) | |
]. | |
%------------------------------------------------------------------------------- | |
% zip_with/3 first attempt | |
zip_with(_Function,[],_L) -> | |
[]; | |
zip_with(_Function,_L,[]) -> | |
[]; | |
zip_with(Function,[X|Xs],[Y|Ys]) -> | |
[Function(X,Y)|zip_with(Function,Xs,Ys)]. | |
%+++++++++++++++++++++++++++++++++++++++ | |
% unit tests | |
%+++++++++++++++++++++++++++++++++++++++ | |
zip_with_test_() -> | |
F = fun(X,Y) -> X+Y end, | |
[ | |
?_assertEqual([],zip_with(F,[],[1,2])), | |
?_assertEqual([],zip_with(F,[1,2],[])), | |
?_assertEqual([2,4],zip_with(F,[1,2],[1,2])) | |
]. | |
%------------------------------------------------------------------------------- | |
% zip_with/3 2nd attempt using lists:map/2 and zip/2 | |
zip_with2(Function,L,R) -> | |
F = fun({X,Y}) -> Function(X,Y) end, | |
lists:map(F,zip(L,R)). | |
%+++++++++++++++++++++++++++++++++++++++ | |
% unit tests | |
%+++++++++++++++++++++++++++++++++++++++ | |
zip_with2_test_() -> | |
F = fun(X,Y) -> X+Y end, | |
[ | |
?_assertEqual([],zip_with2(F,[],[1,2])), | |
?_assertEqual([],zip_with2(F,[1,2],[])), | |
?_assertEqual([2,4],zip_with2(F,[1,2],[1,2])) | |
]. | |
%------------------------------------------------------------------------------- | |
% zip/2 2nd attempt rewrite using zip_with/3 | |
zip2(L,R) -> | |
zip_with(fun (X,Y) -> {X,Y} end, L, R). | |
%+++++++++++++++++++++++++++++++++++++++ | |
% unit tests | |
%+++++++++++++++++++++++++++++++++++++++ | |
zip2_test_() -> | |
[ | |
?_assertEqual([],zip2([],[1,2,3])), | |
?_assertEqual([],zip2([1,2,3],[])), | |
?_assertEqual([{1,2},{3,4}],zip2([1,3],[2,4,6])) | |
]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment