Skip to content

Instantly share code, notes, and snippets.

View abbas-taher's full-sized avatar

Abbas Taher abbas-taher

  • Canada
View GitHub Profile
@abbas-taher
abbas-taher / processing-duplicates-in-a-list-functional-way.md
Last active October 1, 2018 11:38
Tutorial 101: Processing consecutive duplicates in a list of data using Scala - a functional approach

Tutorial 101: Processing consecutive duplicates in a list using Scala - the functional way

Step by step examples to solving problems 8-11 from the Ninty-Nine set of exercises

This tutorial presents solutions for solving problems 8-11 of the famous 99 Prolog exercises (by Werner Hett). Given a list of integers, we’ll look at different methods to process consecutive duplicates. First, we start with a code snippet on problem#8 then gradually describe a more generalized algorithm using higher order functions to solve these 4 problems in Scala.

I am using the following integer list that contains multiple consecutive duplicates as a basis for testing.

val listdup = List (1,1,1,1,2,2,4,4,5,3,4,4,4,3,3)

Let's start with the first problem where we present two code samples: a hard coded approach and a flexible functional method that enables reusability.