From 4e2e2c9fe98af2dd4c20cc96a55ce839b0eb6538 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 5 Oct 2021 18:39:24 -0300 Subject: [PATCH] Exercism: Accumulate --- erlang/accumulate/.exercism/config.json | 30 ++++++++++++ erlang/accumulate/.exercism/metadata.json | 1 + erlang/accumulate/HELP.md | 36 +++++++++++++++ erlang/accumulate/README.md | 51 +++++++++++++++++++++ erlang/accumulate/rebar.config | 30 ++++++++++++ erlang/accumulate/src/accumulate.app.src | 9 ++++ erlang/accumulate/src/accumulate.erl | 8 ++++ erlang/accumulate/test/accumulate_tests.erl | 30 ++++++++++++ 8 files changed, 195 insertions(+) create mode 100644 erlang/accumulate/.exercism/config.json create mode 100644 erlang/accumulate/.exercism/metadata.json create mode 100644 erlang/accumulate/HELP.md create mode 100644 erlang/accumulate/README.md create mode 100644 erlang/accumulate/rebar.config create mode 100644 erlang/accumulate/src/accumulate.app.src create mode 100644 erlang/accumulate/src/accumulate.erl create mode 100644 erlang/accumulate/test/accumulate_tests.erl diff --git a/erlang/accumulate/.exercism/config.json b/erlang/accumulate/.exercism/config.json new file mode 100644 index 0000000..b5de5b0 --- /dev/null +++ b/erlang/accumulate/.exercism/config.json @@ -0,0 +1,30 @@ +{ + "blurb": "Implement the `accumulate` operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection.", + "authors": [], + "contributors": [ + "adolfopa", + "ErikSchierboom", + "etrepum", + "iHiD", + "JordanAdams", + "juhlig", + "kytrinyx", + "magthe", + "NobbZ", + "tmcgilchrist", + "xymbol" + ], + "files": { + "solution": [ + "src/accumulate.erl" + ], + "test": [ + "test/accumulate_tests.erl" + ], + "example": [ + ".meta/example.erl" + ] + }, + "source": "Conversation with James Edward Gray II", + "source_url": "https://twitter.com/jeg2" +} diff --git a/erlang/accumulate/.exercism/metadata.json b/erlang/accumulate/.exercism/metadata.json new file mode 100644 index 0000000..f3ac98d --- /dev/null +++ b/erlang/accumulate/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"erlang","exercise":"accumulate","id":"c9819e2454fa472d881c797983106708","url":"https://exercism.org/tracks/erlang/exercises/accumulate","handle":"JBiason","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/erlang/accumulate/HELP.md b/erlang/accumulate/HELP.md new file mode 100644 index 0000000..6408539 --- /dev/null +++ b/erlang/accumulate/HELP.md @@ -0,0 +1,36 @@ +# Help + +## Running the tests + +You can run the tests by running the following command from the exercise directory. + +```bash +$ rebar3 eunit +``` + +## Submitting your solution + +You can submit your solution using the `exercism submit src/accumulate.erl` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Erlang track's documentation](https://exercism.org/docs/tracks/erlang) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can use one of the following resources: + +- [Exercism related BEAM support channel on gitter](https://gitter.im/exercism/xerlang) +- [Erlang Documentation](http://www.erlang.org/doc.html) +- [Learn You Some Erlang for Great Good](http://learnyousomeerlang.com) +- [StackOverflow](http://stackoverflow.com/) \ No newline at end of file diff --git a/erlang/accumulate/README.md b/erlang/accumulate/README.md new file mode 100644 index 0000000..543a18c --- /dev/null +++ b/erlang/accumulate/README.md @@ -0,0 +1,51 @@ +# Accumulate + +Welcome to Accumulate on Exercism's Erlang Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Implement the `accumulate` operation, which, given a collection and an +operation to perform on each element of the collection, returns a new +collection containing the result of applying that operation to each element of +the input collection. + +Given the collection of numbers: + +- 1, 2, 3, 4, 5 + +And the operation: + +- square a number (`x => x * x`) + +Your code should be able to produce the collection of squares: + +- 1, 4, 9, 16, 25 + +Check out the test suite to see the expected function signature. + +## Restrictions + +Keep your hands off that collect/map/fmap/whatchamacallit functionality +provided by your standard library! +Solve this one yourself using other basic tools instead. + +## Source + +### Contributed to by + +- @adolfopa +- @ErikSchierboom +- @etrepum +- @iHiD +- @JordanAdams +- @juhlig +- @kytrinyx +- @magthe +- @NobbZ +- @tmcgilchrist +- @xymbol + +### Based on + +Conversation with James Edward Gray II - https://twitter.com/jeg2 \ No newline at end of file diff --git a/erlang/accumulate/rebar.config b/erlang/accumulate/rebar.config new file mode 100644 index 0000000..db5d907 --- /dev/null +++ b/erlang/accumulate/rebar.config @@ -0,0 +1,30 @@ +%% Erlang compiler options +{erl_opts, [debug_info, warnings_as_errors]}. + +{deps, [{erl_exercism, "0.1.2"}]}. + +{dialyzer, [ + {warnings, [underspecs, no_return]}, + {get_warnings, true}, + {plt_apps, top_level_deps}, % top_level_deps | all_deps + {plt_extra_apps, []}, + {plt_location, local}, % local | "/my/file/name" + {plt_prefix, "rebar3"}, + {base_plt_apps, [stdlib, kernel, crypto]}, + {base_plt_location, global}, % global | "/my/file/name" + {base_plt_prefix, "rebar3"} +]}. + +%% eunit:test(Tests) +{eunit_tests, []}. +%% Options for eunit:test(Tests, Opts) +{eunit_opts, [verbose]}. + +%% == xref == + +{xref_warnings, true}. + +%% xref checks to run +{xref_checks, [undefined_function_calls, undefined_functions, + locals_not_used, exports_not_used, + deprecated_function_calls, deprecated_functions]}. diff --git a/erlang/accumulate/src/accumulate.app.src b/erlang/accumulate/src/accumulate.app.src new file mode 100644 index 0000000..e756c3e --- /dev/null +++ b/erlang/accumulate/src/accumulate.app.src @@ -0,0 +1,9 @@ +{application, accumulate, + [{description, "exercism.io - accumulate"}, + {vsn, "0.0.0"}, + {modules, []}, + {registered, []}, + {applications, [kernel, + stdlib]}, + {env, []} + ]}. diff --git a/erlang/accumulate/src/accumulate.erl b/erlang/accumulate/src/accumulate.erl new file mode 100644 index 0000000..abccdf3 --- /dev/null +++ b/erlang/accumulate/src/accumulate.erl @@ -0,0 +1,8 @@ +-module(accumulate). + +-export([accumulate/2]). + +accumulate(Fn, [H|T]) -> + [Fn(H) | accumulate(Fn, T)]; +accumulate(_Fn, []) -> + []. diff --git a/erlang/accumulate/test/accumulate_tests.erl b/erlang/accumulate/test/accumulate_tests.erl new file mode 100644 index 0000000..5443eb8 --- /dev/null +++ b/erlang/accumulate/test/accumulate_tests.erl @@ -0,0 +1,30 @@ +-module(accumulate_tests). + +-include_lib("erl_exercism/include/exercism.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +accumulate_empty_list_test() -> + Fn = fun() -> ok end, + Ls = [], + ?assertEqual([], accumulate:accumulate(Fn, Ls)). + +accumulate_squares_test() -> + Fn = fun(Number) -> Number * Number end, + Ls = [1, 2, 3], + ?assertEqual([1, 4, 9], accumulate:accumulate(Fn, Ls)). + +accumulate_upcases_test() -> + Fn = fun(Word) -> string:to_upper(Word) end, + Ls = string:tokens("hello world", " "), + ?assertEqual(["HELLO", "WORLD"], accumulate:accumulate(Fn, Ls)). + +accumulate_reversed_strings_test() -> + Fn = fun(Word) -> lists:reverse(Word) end, + Ls = string:tokens("the quick brown fox etc", " "), + ?assertEqual(["eht", "kciuq", "nworb", "xof", "cte"], accumulate:accumulate(Fn, Ls)). + +accumulate_recursively_test() -> + Chars = string:tokens("a b c", " "), + Nums = string:tokens("1 2 3", " "), + Fn = fun(Char) -> [Char ++ Num || Num <- Nums] end, + ?assertEqual([["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]], accumulate:accumulate(Fn, Chars)).