Non-Programmer Welcome- Sum, Average and Other Data Set Fun With Kotlin

Eric N
Eric N
Jul 8, 2020 · 3 min read

Given a list of Employees, find the average and sum of salary for each department.

You don’t have to be a Kotlin-programmer or any programmer to complete the following 4 tasks, just a curious mind and a love for numbers will do

Each Employee has the following properties:

  • Name
  • Department
  • Salary
  • Title

Does that sound familiar? I’m sure it does to all of us who have been through the 101 tutorial for Excel and SQL.

Buckled up as today we are going to learn how to achieve the same tasks with Kotlin aka. one of the most well-rounded and loved programming language of our era!

Preparation

Our data set is here (salary, name, department and title are random)

The code to generate the .csv data is here. You can easily modify it to generate more than 12 employees for example. The best part is you can run and have fun instantly without installing anything!

Task 0 — Total sum

Get the total salary amount of the whole list

Outputs

Task 1 — Group member counts

Count the number of employees in each department

Outputs

Task 2 — Groups’ sums

Get the list of departments and the total salary amount for each of them

Outputs

Behind the scene, .fold() uses .aggregate() so alternatively, the same result can be achieved by:

Task 3 — Groups’ averages

Get the list of departments and the average salary for each of them

outputs

If the current average of K employees is kAverage, the new average when process the K+1th employee with salary kPlusOneSalary will be

kPlusOneAverage = kAverage + ((kAverage — kPlusOneSalary)/K+1)

There’s a bug currently. Can you spot it?

Live Action

All of the above tasks can be found here, feel free to run, modify and run again for a maximum amount of fun :)

Ask me questions and let me know once and I might give you a certificate ;)

The following sections are intended for programmers only

Performance

All of the aforementioned functions have O(N) time and space (fold() and aggregate() use a Map of “group name/id” to group/sub-list of items) complexity. There’s no performance magic behind them. They are just convenient and fun to use :)

If you want to process millions of records or more, your data might not fit inside the memory. You’d need to process the values in a stream, one at a time.

Next part

Sum and average for large collections of data with RxJava and perhaps Kotlin Sequence

Further reading

The Startup

Get smarter at building your thing. Join The Startup’s +785K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Eric N

Written by

Eric N

Hiring Android engineer shorturl.at/bivJO Clean coder, walker & biker. Hater of inefficiency

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Eric N

Written by

Eric N

Hiring Android engineer shorturl.at/bivJO Clean coder, walker & biker. Hater of inefficiency

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store