Skip to content

Instantly share code, notes, and snippets.

View gorkaio's full-sized avatar
👾

Gorka López de Torre gorkaio

👾
View GitHub Profile
-module(composition).
-export([twice/2,compose/2,iterate/1]).
-export([test/0]).
%% Composition
compose(F, G) ->
fun (X) -> F(G(X)) end.
%% Iteration
-module(rps).
-export([play/0, play/1, result/2, tournament/2, auto/3]).
-export([rock/1, echo/1, random/1, cycle/1, lfreq/1, mfreq/1, mad_hatter/1, best_of/1]). % Strategies
-export([test/0]).
-type gesture() :: rock | paper | scissors.
-type result() :: win | lose | draw.
-type strategy() :: fun(([gesture()]) -> gesture()).
-define(STRATEGIES, [fun rock/1, fun echo/1, fun random/1, fun cycle/1, fun lfreq/1, fun mfreq/1, fun mad_hatter/1, fun best_of/1]).
-module(hof).
-export([doubleAll/1,evens/1,product/1,zip/2,zip_with/3]).
-export([test/0]).
%% Using higher-order functions
doubleAll(Xs) -> lists:map(fun(X) -> 2*X end, Xs).
doubleAll_test() ->
[] = doubleAll([]),
-module(billing).
-export([print/1]).
-import(catalog, [find/1]).
-define(WIDTH, 30).
-define(STORE_NAME, "Erlang Stores").
-define(PRECISSION, "~.2f").
print(Cart) ->
Bill = format_bill(Cart),
-module(text).
-export([load/1,format/2,format/3,format_file/2,format_file/3]).
-export([split_words_test/0,format_line_test/0,remove_line_test/0,format_test/0,line_test/0,join_test/0]).
-spec load(string()) -> string() | {error, atom()}.
load(FileName) ->
case file:read_file(FileName) of
{ok, Data} -> Data;
_ -> {error, unable_to_read_file}
end.
-module(index).
-export([get_file_contents/1,show_file_contents/1,index/1,index/2]).
-export([split_words_test/0,filter_words_test/0,normalise_words_test/0,words_test/0,index_line_test/0,index_test/0,add_words_test/0,range_test/0]).
-define(MIN_WORD_LENGTH, 2).
% Used to read a file into a list of lines.
% Example files available in:
% gettysburg-address.txt (short)
% dickens-christmas.txt (long)
-module(lists2).
-export([join/2,concat/1,member/2,sort/1,sort/2,perms/1]).
-export([join_test/0,concat_test/0,member_test/0,sort_test/0,perms_test/0]).
%% Join %%%
join(L1, L2) when is_list(L1), is_list(L2) ->
join_internal(lists:reverse(L1), L2).
join_internal(L1, []) -> lists:reverse(L1);
join_internal(L1, [H|T]) ->
-module(palindrome).
-export([palindrome/1,normalize/1]).
-export([palindrome_test/0,normalize_test/0]).
%% Palindrome %%%
palindrome([]) -> false;
palindrome(S) ->
Sn = normalize(S),
palindrome(Sn,lists:reverse(Sn), false).
-module(nub).
-export([nub/1]).
-export([nub_test/0]).
nub([]) -> [];
nub([_|_] = L) -> nub(L, []).
nub([], Ac) -> lists:reverse(Ac);
nub([H|T], Ac) ->
case lists:member(H, Ac) of
-module(list_functions2).
-export([double/1,evens/1]).
-export([double_test/0,evens_test/0]).
%% double %%%
double([]) -> [];
double([_|_] = L) -> double(L, []).
double([], Ac) -> lists:reverse(Ac);