View PermutationGenerator1.java
import java.util.ArrayList;
import java.util.Scanner;
public class PermutationGenerator1 {
private static String inputWithoutC(String input, char c) {
String inputWithoutC = "";
char[] temp = input.toCharArray();
for(char d : temp) {
if(d !=c ) {
inputWithoutC += d;
View NullSafe.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
View gist:a2e76d98bb86ed054c0fb8317c2117ce
implicit class NullSafe[A, B](val a: A) extends AnyVal {
def ?(f: A => B): B = { if(a == null) null.asInstanceOf[B] else f(a) }
}
//example
val s = "Hello, World!"
s?(_.reverse)?(_.reverse) //returns "Hello, World!"
val sNull: String = null
sNull?(_.reverse)?(_.reverse) //returns null
View CountTo99WithoutNumbers.scala
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
}
View gist:66f3eac5feaff22bda1d1bf8b722844e
//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();
View DailyAllowance.scala
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) {
View SendVehicles.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")
View SuperHeroEscape.scala
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!")
View PartialFunctionExample.java
import java.util.*;
public class PartialFunctionExample {
static class Tuple {
String s;
int length;
Tuple(String s) {
this.s = s;
View PartialFunctionExample2.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
}