Want to learn?

Anonymous functions exercises

Exercises

  1. Create and execute an anonymous functions (both with standard and short & notation) which calculates square area of:

    • square,
    • rectangle,
    • circle (assume that Pi is 3.14).
  2. Rewrite given anonymous functions with & notation:

    • Enum.find([2, 3, 4], fn x -> rem(x, 2) == 1 end)
    • Enum.flat_map_reduce(1..5, 0, fn x, acc -> {[[x]], acc + x} end)
  3. Create an anonymous function which meets the criteria:

    • when you pass first_name it will print "Hello User" (where "User" is first_name in this case),
    • when you pass last_name it will print "Hello Example" (where "Example" is last_name in this case),
    • when you pass first_name and last_name it will print "Hello User Example" (where "User" is first_name and "Example" is last_name in this case),
    • when you don't pass any arguments it should print "Who you are?".
    • you should not use any control flow mechanisms,
    • use different function implementations depending on given arguments,
    • use pattern matching to execute proper function implementation based on different arguments.
  4. Create an anonymous function which takes two arguments: number and an anonymous function. Your function should execute anonymous function (passed as a second argument) with a number (passed as a first argument). Try it out to perform following operations:

    • multiplying by 10,
    • dividing by 10,
    • add 10 to given number,
    • subtract 10 from given number.
  5. Create an anonymous function which takes a number as an argument and returns another function. Execute nested function with another number. Sum of these two numbers should be returned as a result, for example:

      iex> sum.(5).(6)
      11
    
  6. Create an anonymous function which performs the division of numbers. Use guard clause not to allow non-numeric arguments and zero as a second argument (in both cases provide feedback as a returned value).

Check our latest product - it's based on our experience of managing over 50-people strong company. The tool we're missing as a small company and not an enterprise.

humadroid.io is an employee and performance management software. It's an unique tool allowing everyone to be in the loop - by having up to date info about co-workers, time-off, benefits, assets, helping with one-on-ones, being a go-to place for company-wide announcements.

Check out humadroid.io
Top

Contact us

* Required fields

The controller of your personal data provided via this contact form is Prograils sp. z o.o., with a registered seat at Sczanieckiej 9A/10, 60-215 PoznaƄ. Your personal data will be processed in order to respond to your inquiries and for our marketing purposes (e.g. when you ask us for our post-development, maintenance or ad hoc engagements for your app). You have the rights to: access your personal data, rectify or erase your personal data, restrict the processing of your personal data, data portability and to object to the processing of your personal data. Learn more.

Notice

We do not track you online. We use only session cookies and anonymous identifiers for the purposes specified in the cookie policy. No third-party trackers.

I understand
Elo Mordo!Elo Mordo!