Skip to content

Instantly share code, notes, and snippets.

scala.collection.Searching provides binary search but you need an Indexed sequence (eg. Array), not a Linear sequence (eg. List). Search will still work on the List but you get linear O(n) behaviour rather than O(Log n).

case class Employee (id: Int, name: String, age: Int)

val emp1 = Employee(1, "Jane Doe", 45)
val emp2 = Employee(2, "Jon Doe", 54)
val emp3 = Employee(3, "Tera Patrick", 38)
val emp4 = Employee(4, "Jenna Jameson", 36)
val emp5 = Employee(5, "Aurora Snow", 34) // not added
@jonjack
jonjack / scala-for-comprehension-with-futures.md
Last active September 14, 2018 09:21
Scala for comprehension with Futures

Some ideas I gathered from various places for working with Futures in for comprehensions.

Import what we need for all the examples.

import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
@jonjack
jonjack / scala-library-metric-comparison.md
Last active September 28, 2018 19:58
Some comparison metrics for some of the popular Scala frameworks/libraries.

Metrics last collected 28.09.2018

Library GitHub stars Contract jobs (last 6 months)
Source: itjobswatch.co.uk
Description
Play 10737 290 Fullstack web framework.
Akka 9054 222 For highly concurrent, distributed, and resilient message-driven applications on the JVM.
Scalatra 2321 2 Microframework port of Ruby’s Sinatra
Lift 1170
@jonjack
jonjack / shell-function-get-absolute-path.md
Created October 1, 2018 10:58
Shell function to return the absolute path of some arbitrary one
#!/bin/sh
function abspath() {
pushd . > /dev/null;
if [ -d "$1" ]; then cd "$1"; dirs -l +0;
else cd "`dirname \"$1\"`"; cur_dir=`dirs -l +0`;
if [ "$cur_dir" == "/" ]; then echo "$cur_dir`basename \"$1\"`";
else echo "$cur_dir/`basename \"$1\"`"; fi; fi; popd > /dev/null;
}
@jonjack
jonjack / git-setup-osx.md
Last active October 10, 2018 18:44
Git configuration on Mac.

Set your global user details.

git config --global user.name "Your Name"
git config --global user.email your.email@elasticpath.com

Set your line feed settings.

@jonjack
jonjack / .bash_profile
Created October 12, 2018 18:40 — forked from natelandau/.bash_profile
Mac OSX Bash Profile
# ---------------------------------------------------------------------------
#
# Description: This file holds all my BASH configurations and aliases
#
# Sections:
# 1. Environment Configuration
# 2. Make Terminal Better (remapping defaults and adding functionality)
# 3. File and Folder Management
# 4. Searching
# 5. Process Management
@jonjack
jonjack / java-chaining-optional-checks.md
Last active March 23, 2021 01:26
Example of how to check multiple Optional values in a chain and return a default value if none contain a value.

The following scenario has multiple Optionals, a real example could be where you are calling multiple validation checks where each check returns an Optional<T>. You then want to check if each Optional contains a value or is empty - returning the first one that has a value. If none contain a value (ie. are instances of Optional.empty) then you return a default value.

In this example we are using Optional<Single> where Single is an RxJava Observer that returns a single value.

import com.google.common.collect.ImmutableList;

Mac Setup

Fonts

Check out Adobe Fonts.

brew tap caskroom/fonts &amp;&amp; brew cask install font-source-code-pro
@jonjack
jonjack / scala-snippets.md
Last active November 7, 2018 23:28
Random Scala snippets

Return the max value of a List[Int] if unique, otherwise return -1.

def maxIfUnique(li: List[Int]) = li.groupBy(_.intValue).maxBy(_._1)._2 match {
  case x if x.size == 1 => x.head
  case _ => -1
}

scala&gt; val list = List(1, 2, 1) // 2 is max and is unique

Scenario: We have an interface that defines an operation.

public interface Validator {
  Optional validate(String subject, String subject2);
}

Some Validators only have 1 String to validate so we can extend the interface with a simpler one.