Skip to content

Instantly share code, notes, and snippets.

View scottashipp's full-sized avatar

Scott Shipp scottashipp

View GitHub Profile
@scottashipp
scottashipp / gist:61ed8ce312a025e47b31
Last active August 29, 2015 14:13
Alternate Scala collect example
object MemberType extends Enumeration {
type MemberType = Value
val Free, Trial, Pay, Premium = Value
}
object MemberStatus extends Enumeration {
type MemberStatus = Value
val Active, Inactive, Disabled = Value
}
class DataRecord(val id: Long, val status: MemberStatus.Value, val name: String, val memType: MemberType.Value, val accessKey: String)
@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 / 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.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 / 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;
object SuperHeroEscape extends App {
def getFreeFromATrap(doSuperPower: String => Unit, trap: String) = {
println()
println(s"Oh no I'm trapped by ${trap}.")
println(s"I will break free!")
doSuperPower(trap)
}
def flyOut(trap: String) = println(s"I fly out of the ${trap} and into the sky!")
@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 / 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();