How do you pass a function as a parameter in Elixir?
Use &Module.function/arity to pass it, and .(…) to call it. For example: def my_hof(f) f.([1, 2, 3], &(&1 * 2)) end my_hof(&Enum.map/2)
Use &Module.function/arity to pass it, and .(…) to call it. For example: def my_hof(f) f.([1, 2, 3], &(&1 * 2)) end my_hof(&Enum.map/2)
You can prepend your variable name with @: defmodule MyModule do @my_favorite_number 13 end Here are the docs
To me, the if IS the equivalent of a ternary operator as it evaluates to a value which for various other languages it doesn’t. so x = if false, do: 1, else: 2 is basically x = false? 1 : 2 Not sure why Ruby adopted it ( if you are coming from Ruby ) …
There are 5 ways to run only specific tests with Elixir run a single file with mix test path_to_your_tests/your_test_file.exs This will run all test defined in your_test_file.exs run a specific test from a specific test file by adding a colon and the line number of that test for example mix test path_to_your_tests/your_test_file.exs:12 will run the …
No, there is no way to test them via ExUnit. I personally avoid testing private functions because usually you end up testing implementation instead of behaviour and those tests fail as soon as you need to change the code. Instead, I test the expected behaviour via the public functions, breaking them in small, consistent chunks.
See Inspect.Opts for a description of the available options: :limit – limits the number of items that are printed for tuples, bitstrings, maps, lists and any other collection of items. It does not apply to strings nor charlists and defaults to 50. If you don’t want to limit the number of items to a particular …
Just follow the Elixir docs to add an element to a list ( and keep performance in mind =) ): iex> list = [1, 2, 3] iex> [0 | list] # fast [0, 1, 2, 3] iex> list ++ [4] # slow [1, 2, 3, 4] https://hexdocs.pm/elixir/List.html
Enum.to_list/1 is what you’re looking for: iex(3)> Enum.to_list 1..10 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
You can use the IEx.Helpers.recompile/0 function. Recompiles the current Mix application. This helper only works when IEx is started with a Mix project, for example, iex -S mix. Before compiling the code, it will stop the current application, and start it again afterwards. Stopping applications are required so processes in the supervision tree won’t crash …
You can now do mix test path/to/test.exs:13, where 13 is the line of the test.