Skip to content

Instantly share code, notes, and snippets.

@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> 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.

We have a method to test which iterates over some List. The example is a validator service which iterates over a List of validators applying each one and returning when one fails ie. it returns an Optional containing an error message (this is just a concrete example to demonstrate Mockito and is not important to understand).

public class SomeValidationService implements ValidationService {

    /* List of validators which all encapsulate some different business validation logic */
    /* This gets injected say via Spring */
    private List<Validator> validators;
 
    public Optional<ErrorMessage> validateAction(final ObjectToValidate obj) {
import org.joda.time.LocalTime;
public class Timer {
public static void main(String[] args) {
LocalTime start = LocalTime.now();
// some event to be timed
LocalTime end = LocalTime.now();
int delta = end.getMillisOfDay() - start.getMillisOfDay();
System.out.println("Time took: " + Double.valueOf(delta)/1000 + " seconds");
}
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;
public class Timer {
public static void main(String[] args) {
LocalTime start = LocalTime.now();
// some event to be timed
LocalTime end = LocalTime.now();
long delta = TimeUnit.NANOSECONDS.toMillis(end.toNanoOfDay() - start.toNanoOfDay());
System.out.println("Time took: " + Double.valueOf(delta)/1000 + " seconds");
if (tweeted || facebooked) {
gratitude += 1;
}
if (githubStarred) {
karmaBalance = true;
} else {
$('we').say('ok');
}

Check whats available

brew search maven
==> Formulae
maven                maven-completion     maven-shell          maven@3.0            maven@3.1            maven@3.2            maven@3.3
@jonjack
jonjack / osx-brew-install-not-symlinked-chown-user-local.md
Last active November 24, 2020 12:43
cannot complete brew install, not symlinked into /usr/local, requires me to chown /usr/local

While installing some formula I get:-

The formula built, but is not symlinked into /usr/local

Running brew as root is not allowed.

sudo brew install awscli

Mac Setup

Fonts

Check out Adobe Fonts.

brew tap caskroom/fonts &amp;&amp; brew cask install font-source-code-pro

Assumes you have brew installed.

Install pyenv.

brew install pyenv

Check what versions of python are available.