How to create a MongoDB Replica Set in macOS Local Environment


Install and run MongoDB with Homebrew

  • The first step is updating Homebrew. Open the Terminal application and type: brew update
  • After updating Homebrew: brew install mongodb
  • After downloading Mongo, create the db directory. This is where the Mongo data files will be stored: mkdir-p /data/db
  • Make sure that the /data/db directory has the right permissions by running: sudo chown -R `id -un` /data/db

Creating the Replica Set

  1. In different terminals, we can start three nodes for the Replica by running:
mongod --port 27017 --dbpath /data/db/rs0-0 --replSet rs0 --bind_ip localhost

mongod --port 27018 --dbpath /data/db/rs0-1 --replSet rs0 --bind_ip localhost

mongod --port 27019 --dbpath /data/db/rs0-2 --replSet rs0 --bind_ip localhost

2. For the next step, we need to open a mongo shell that connects to the primary node by using the mongo command and run:

        _id: "rs0",
        members: [
            { _id: 0, host : "" },
            { _id: 1, host : "" },
            { _id: 2, host : "" }


Sometimes, one of the common MongoDB ports might be already in use. In order to find the process that is running on a specific ports, we can run:

lsof -n -i :<port-number> | grep LISTEN

This will provide us the process id, which we can kill later by running:

kill -9 <process-id>


5 ways to solve Fibonacci in Scala – Tail Recursion, Memoization, The Pisano Period & More

One of the classic problems in order to explain Recursion in Computer Science is the typical Fibonacci Sequence, which takes its name in honor to Leonardo De Pisa, the Italian Mathematician who wrote the Liber Abaci in order to convince the public about the superiority of the Hindu-Arabic numeral system.

The Fibonacci Sequence is characterized by the fact that every number in it is equal to the sum of the preceding ones:

0, 1, 1, 2, 3, 5, 8, 13, 21….. 

The formal definition of the sequence Fn of Fibonacci numbers is:

Fn = Fn-1 + Fn-2

Where F0 = 0 and F1 = 1

We can solve this classical problem in Scala using 5 different approaches with their own advantages and disadvantages depending on how large is the Fibonacci sequence needed to get our solution.

Case 1: Pattern Matching
– Simple and well suited for small numbers, but it doesn’t scale
– If n is big, we run the risk of getting a Stack Overflow

def fib1(n: Long): Long = n match {
  case 0 | 1 => n
  case _ => fib1(n - 1) + fib1(n - 2)

Case 2: Loop
– Handles Long numbers (64 bit)
– A little bit too verbose, non-idiomatic, mutable variables.

def fib2(n: Long): Long = {
  var first = 0
  var second = 1
  var count = 0

  while(count < n){
    val sum = first + second
    first = second
    second = sum
    count = count + 1

  return first

Case 3: Tail Recursion
– Optimized by the compiler. We say a function is tail recursive when the recursive call is the last thing executed by the function. In this example, we can see the fib_tail call being applied in the last line of code.

def fib3(n: Int): Int = {
  def fib_tail(n: Int, a: Int, b: Int): Int = n match {
    case 0 => a
    case _ => fib_tail(n - 1, b, a + b)
  return fib_tail(n, 0 , 1)

Case 4: Lazy Evaluation with Scala Streams and Memoization

A Stream is similar to a list, with the exception that its elements are computed lazily, this means that a Stream can be infinitely long and only the elements requested at a given time will be computed. By combining this technique with the usage of  BigInt types, we obtain nothing less than a mortal weapon that we can use on sites like LeetCode and Hackerrank to completely obliterate Fibonacci-based problems and obtain massive performance improvements.

val fib: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: => p._1 + p._2)

def fib4(n: Int) = fib(n)

Also, we can create a memoize wrapper in combination with the previously defined stream to keep a cache and make things even more performant.

def memoize[A, B](f: A => B): A => B =
  new collection.mutable.WeakHashMap[A, B] {
    override def apply(a: A) = getOrElseUpdate(a, f(a))

val memofib = memoize(fib)

And then, we can call it using memofib(<number>), for example: memofib(100)

Extra Case: The Pisano Period
– Gets the last n digits of the Fibonacci sequence with tail recursion (6 for this example).

The nth Pisano Period, written π(n), is the period with which the sequence of Fibonacci numbers taken modulo n repeats. Pisano periods are named after Leonardo Pisano, better known as Fibonacci. This name was made up in 1838 by the Franco-Italian historian Guillaume Libri and is short for filius Bonacci (‘son of Bonacci’).

def fib5( n : Int) : Int = {
  def fib_tail( n: Int, a: Int, b: Int): Int = n match {
    case 0 => a
    case _ => fib_tail(n - 1, b, (a + b) % 1000000)
  return fib_tail( n % 1500000, 0, 1)

Grab the code at: Github

The Scala Ecosystem: Infographics

the scala ecosystem (6)



  • Play!: Play is an open source web application framework, written in Scala and Java, which follows the model–view–controller (MVC) architectural pattern. It aims to optimize developer productivity by using convention over configuration, hot code reloading and display of errors in the browser.
  • Lift: Lift is an expressive framework for writing web applications. It draws upon concepts from peer frameworks such as Grails, Ruby on Rails, Seaside, Wicket and Django. It favors convention over configuration in the style of Ruby on Rails, although it does not prescribe the model–view–controller (MVC) architectural pattern.
  • Skinny: Skinny is a full-stack web app framework built on Skinny Micro. To put it simply, Skinny framework’s concept is Scala on Rails. Skinny is highly inspired by Ruby on Rails and it is optimized for sustainable productivity for Servlet-based web app development.
  • Scalatra: Scalatra is a simple, accessible and free web micro-framework. It combines the power of the JVM with the beauty and brevity of Scala, helping you quickly build high-performance web sites and APIs.
  • Spray: Spray is an open-source toolkit for building REST/HTTP-based integration layers on top of Scala and Akka. Being asynchronous, actor-based, fast, lightweight, modular and testable it’s a great way to connect your Scala applications to the world.


  • Akka: Akka is an open-source toolkit and runtime simplifying the construction of concurrent and distributed applications on the JVM. Akka supports multiple programming models for concurrency, but it emphasizes actor-based concurrency, with inspiration drawn from Erlang.
  • ScalikeJDBC: ScalikeJDBC is a tidy SQL-based DB access library for Scala developers. This library naturally wraps JDBC APIs and provides you easy-to-use and very flexible APIs. What’s more, QueryDSL makes your code type-safe and reusable.
  • ScalaJS: Scala.js is a compiler that compiles Scala source code to equivalent Javascript code. That lets you write Scala code that you can run in a web browser, or other environments (Chrome plugins, Node.js, etc.) where Javascript is supported.
  • Slick: Slick is a modern database query and access library for Scala. It allows you to work with stored data almost as if you were using Scala collections while at the same time giving you full control over when a database access happens and which data is transferred.
  • Kafka: is an open-source message broker project developed by the Apache Software Foundation written in Scala. The project aims to provide a unified, high-throughput, low-latency platform for handling real-time data feeds.



Live Events & Conferences

Job Websites

Useful Links & Resources

Functional Programming: Solving typical problems with Lists in Scala

Here are some solutions in Scala for typical problems with Lists that may arise when developing larger tasks, these ones can become repetitive over time and they could be used as a “recipe”. Also, they’re classic/basic problems for practice.

  •  Five different ways to create a function that returns a list of N elements:
def a(n: Int): List[Int] = List.fill(n)(1) 
def b(n: Int): List[Int] = (1 to n).toList 
def c(n: Int): List[Int] = if(n == 0) Nil else n :: c(n-1) 
def d(n: Int): List[Int] = List.tabulate(n)(n => 1) 
def e(n: Int): List[Int] = List.range(0,n)


  • Create a function that reverses a list without using the native Scala reverse method:
def rev(l: List[Any]): List[Any] = l match {
   case Nil => l
   case head :: tail => rev(tail) ::: List(head)


  •  Given a list, repeat each element in the list N amount of times:

def f(n: Int, ls: List[Int]): List[Int] = ls.flatMap(List.fill(n)(_))


  •  Filter a given list of integers and output only those values that are less than a specified value:


def g(delim: Int, ls: List[Int]): List[Int] = for(i <- ls if i < delim) yield i



  • For a given list with N integers, return a new list removing the elements at odd positions:
def removeOdds(ls: List[Int]): List[Int] = {ls.zipWithIndex.filter(_._2 % 2 != 0).map(_._1)}


  • Create a function that returns the sum of the odd elements from a list:
def sumOdds(ls: List[Int]): Int = ls.filter(x => x % 2 != 0).sum


  • Create a function that returns the size of a list of integers without using the native Scala length method:
def s(ls: List[Int]): Int = {
    var size = 0
    for(i <- ls) size = size + 1