Skip to content

Instantly share code, notes, and snippets.

View scottashipp's full-sized avatar

Scott Shipp scottashipp

View GitHub Profile
@scottashipp
scottashipp / LimitAccess.java
Last active November 19, 2015 18:02
Use a LinkedBlockingQueue to limit the number of times a method executes in a sliding window of time (60 times in the past minute in this example)
import java.util.concurrent.LinkedBlockingQueue;
import org.joda.time.*;
public class LimitAccess {
final private int LIMIT = 60;
final private LinkedBlockingQueue<DateTime> accessQueue;
public LimitAccess() {
accessQueue = new LinkedBlockingQueue<>();
@scottashipp
scottashipp / PartialFunctionExample.java
Created December 3, 2015 20:53
Kinda trying to do the same thing as PartialFunctionExample.scala, in Java. What a sad comparison.
import java.util.*;
public class PartialFunctionExample {
static class Tuple {
String s;
int length;
Tuple(String s) {
this.s = s;
@scottashipp
scottashipp / PartialFunctionExample.scala
Last active December 18, 2015 00:41
Showing the use of a PartialFunction on a purposefully simple example of getting the length of all strings in a list of any.
object PartialFunctionExample extends App {
val listOfStuff = List(13, "Scott", null, 25.50, "huh", null)
val stringLengths = new PartialFunction[Any, (String, Integer)] {
def apply(s: Any) = s match { case s: String => (s, s.length) }
def isDefinedAt(s: Any) = s != null && s.isInstanceOf[String]
}
val stringsAndLengths = listOfStuff collect stringLengths
@scottashipp
scottashipp / PartialFunctionExample2.scala
Last active December 18, 2015 00:42
Showing how a case statement without a match is a PartialFunction, equivalent to PartialFunctionExample.scala.
object PartialFunctionExample2 extends App {
val listOfStuff = List(13, "Scott", null, 25.50, "huh", null)
val stringsAndLengths = listOfStuff collect { case s: String if s != null => (s, s.length) }
stringsAndLengths foreach println
}
@scottashipp
scottashipp / SendVehicles.scala
Last active December 29, 2015 18:00
Factory Pattern in Scala
sealed abstract class Vehicle(val go: String)
class HotRod extends Vehicle("Vrooooom!")
class Train extends Vehicle("Chooo choooo")
class Moped extends Vehicle("Bzzzzzzzzzzz")
sealed abstract class Route(val name: String)
class Highway extends Route("Interstate")
class CityStreet extends Route("Boulevard")
class TrainTrack extends Route("Rail")
@scottashipp
scottashipp / gist:783c74f68cea2f2e338d
Last active January 13, 2016 23:19
REPL output, "Guessing Game" example of using Either
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Prize(val prizeName: String) {
def get = { new String(s"CONGRATULATIONS! YOU'VE WON A NEW ${prizeName}!") }
}
class GuessAgain(val x: Int) {
def howClose = {
if(x < 5)
@scottashipp
scottashipp / DailyAllowance.scala
Created April 18, 2016 19:54
Budgeting with discretionary income can be difficult because its hard when faced with daily decisions to think of that rather larger monthly figure in daily terms. This program helps you take your monthly discretionary money and turn it into a number you can target daily.
object DailyAllowance extends App {
val DaysInMonth = 30
val MonthlyNet = USD("$5000.00")
val MonthlyFixedExpenses = USD("$3000.00")
case class Money(rate: Int)
class USD(val dollars: Int, val cents: Int) extends Money(100) {
@scottashipp
scottashipp / gist:66f3eac5feaff22bda1d1bf8b722844e
Created May 10, 2016 21:39
How to force clients to provide an implementation for toString at the compiler level
//1. create the interface with a different method than toString
interface Loggable {
String toLogString();
}
//2. Create an abstract class that forwards toString() to this other method
abstract class LoggerBase implements Loggable {
public String toString() {
return toLogString();
@scottashipp
scottashipp / CountTo99WithoutNumbers.scala
Last active May 31, 2016 23:17
Scala app that prints the English words for the numbers 1 to 99 without ever using any number type
object CountTo99WithoutNumbers extends App {
object PrimaryNumber extends Enumeration {
val zero, one, two, three, four, five, six, seven, eight, nine = Value
}
object StrangeNumber extends Enumeration {
val eleven, twelve, thirteen = Value
}
@scottashipp
scottashipp / NullSafe.scala
Created July 27, 2016 20:25
Null-safety in Scala
implicit class NullSafeOption[A,B](val a: Option[A]) extends AnyVal {
def mapNullSafe(f: A => B): Option[B] = { a.flatMap(x => Option(x).map(f)) }
}
implicit class NullSafe[A,B](val a: A) extends AnyVal {
def ?(f: A => B): B = { if(a == null) null.asInstanceOf[B] else f(a) }
}
//Examples with Option + Map
scala> val s: String = null