Count how many times numbers from a list occur in a list of tupled intervals in Scala

Count how many times numbers from a list occur in a list of tupled intervals in Scala

Say I have a list of tuples:

Say I have a list of tuples:

 val ranges= List((1,4), (5,8), (9,10))

and a list of numbers

val nums = List(2,2,3,7,8,9)

I want to make a map from tuple in ranges to how many times a given number from nums fall into the interval of that tuple.

Output:

Map ((1,4) -> 3, (5,8) -> 2, (9,10) -> 1)

What is the best way to go about it in Scala

I have been trying to use for loops and keeping a counter but am falling short. Any help would be very much appreciated.

Best Regards

scala

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

A Beginner's Guide to Scala 3.0

Reading Time: 5 minutes This post describes what to expect and how to start with Scala 3 Or Dotty. In this post, we will get a teaser of what changes to expect from Dotty as well as some of the new changes in the major release. At the end of the post, we will see how we can quickly start with the Dotty compiler and example code.

Use of Either in Scala

In this blog, we are going to see the use of Either in scala. We use Options in scala but why do we want to go for Either? Either is a better approach in the respect that if something fails we can track down the reason, which in Option None case is not possible. We simply pass None but what is the reason we got None instead of Some. We will see how to tackle this scenario using Either.

Understanding Higher-Order functions in Scala

Reading Time: 4 minutes A blog on using higher-order functions in Scala.A higher-order function is a function that takes in another function as argument and itself returns some value or function. Some examples of higher-order functions include map, filter, reduce, foreach etc.

Java and Scala: Why Should You Learn Scala?

Java is a general purpose object oriented language. Scala is less readable due to nested code. The process of compiling source code into byte code is slow. ... Scala treated everything as an instance of the class and it is more object oriented language as compare to Java.

Exception handling in Scala

Scala is functional programming language. Except for the side effects that have a return type of Unit or (), everything else returns a value in scala. Scala treats everything as an expression and that is what makes scala such a powerful language.