This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class PartialFunctionExample { | |
static class Tuple { | |
String s; | |
int length; | |
Tuple(String s) { | |
this.s = s; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |