Skip to content

Instantly share code, notes, and snippets.

@worthlesscog
worthlesscog / NumberText1.scala
Created April 29, 2012 11:59
Converting numbers to English text
object NumberText1 {
val u = Map(
1 -> "one", 2 -> "two", 3 -> "three", 4 -> "four", 5 -> "five",
6 -> "six", 7 -> "seven", 8 -> "eight", 9 -> "nine", 10 -> "ten",
11 -> "eleven", 12 -> "twelve", 13 -> "thirteen", 14 -> "fourteen", 15 -> "fifteen",
16 -> "sixteen", 17 -> "seventeen", 18 -> "eighteen", 19 -> "nineteen"
)
val t = Map(
2 -> "twenty", 3 -> "thirty", 4 -> "forty", 5 -> "fifty",
@worthlesscog
worthlesscog / NT.scala
Created April 29, 2012 19:17
801 bytes
object Z{
val z="teen";val k=1000;val t=""::""::"twenty"::"thirty"::"forty"::"fifty"::"sixty"::"seventy"::"eighty"::"ninety"::Nil
val u=""::"one"::"two"::"three"::"four"::"five"::"six"::"seven"::"eight"::"nine"::"ten"::"eleven"::"twelve"::"thir"+z::"four"+z::"fif"+z::"six"+z::"seven"+z::"eight"+z::"nine"+z::Nil
def c(n:Int)={def c(n:Int,x:String*):String={if(n>0){val m=n%k;val r=m%100;val s=if(r<20)u(r)else t(r/10)+(if(u(r%10)=="")""else" "+u(r%10))
val h=if(m/100>0)u(m/100)+" hundred "else"";val j=if(h=="")s else h+(if(s=="")""else"and ")+s;c(n/k,x.tail:_*)+j+(if(j=="")""else x.head)}else""}
if(n>0){val f=c((n/k)*k,""," thousand "," million "," billion ");val r=c(n%k,"");f+(if(f==""||n%k>99)r else(if(r.isEmpty)""else"and "+r))}else"zero"}
def main(x:Array[String]):Unit={println(c(10201))}}
@worthlesscog
worthlesscog / Cards.scala
Created May 1, 2012 20:02
Cards and Suits
import Rank.{ _ }
import Suit.{ _ }
class Rank(val faceValues: Int*) {
def of(suit: Suit) = Card(this, suit)
}
object Rank {
case object Ace extends Rank(1, 11)
case object Two extends Rank(2)
case object Three extends Rank(3)
@worthlesscog
worthlesscog / Deck.scala
Created May 3, 2012 19:32
Card deck with Shuffle
import scala.util.Random
trait Shuffle {
var cards: List[Card]
lazy val random = new Random
def shuffle {
def shuffle(cards: List[Card], size: Int, pile: List[Card] = Nil): List[Card] = {
if (size > 0) {
val card = cards(random.nextInt(size))
val (bef, aft) = cards.span(_ != card)
@worthlesscog
worthlesscog / Deck2.scala
Created May 7, 2012 17:51
Chute and revised deck with Shuffle
trait Shuffle {
var cards: List[Card]
def next = {
if (!cards.isEmpty) {
val card = cards.head
cards = cards.tail
Some(card)
} else None
}
lazy val random = new scala.util.Random
@worthlesscog
worthlesscog / Hand.scala
Created May 7, 2012 18:02
Blackjack hands for initial, in-play and split
abstract class Hand {
val cards: Seq[Card]
def +(card: Card): Hand
def split: List[Hand]
lazy val scores = {
def combinations(cards: Seq[Card]): List[Seq[Int]] = {
if (!cards.isEmpty) {
for (c ← combinations(cards.tail); v ← cards.head.rank.faceValues) yield v +: c
} else List(Nil)
}
@worthlesscog
worthlesscog / Reader.scala
Created May 13, 2012 19:24
Decimal number reader and splitter
import scala.actors.remote.RemoteActor.select
import scala.actors.remote.Node
import scala.actors.Actor
object Reader {
def main(args: Array[String]) {
println("Reader running")
val actor = new Actor {
def act() {
@worthlesscog
worthlesscog / Mapper.scala
Created May 13, 2012 19:37
Convert numbered 3 digit chunks and stack them for later
import scala.actors.Actor.self
import scala.actors.remote.RemoteActor.{ select, register, alive }
import scala.actors.remote.Node
import scala.actors.Actor
import scala.collection.mutable.Map
import NumberText1.conv
object Mapper {
def main(args: Array[String]) {
@worthlesscog
worthlesscog / Reducer.scala
Created May 13, 2012 19:51
Collect the numbered chunks for reassembly
import scala.actors.Actor.self
import scala.actors.remote.RemoteActor.{ register, alive }
import scala.actors.Actor
import scala.collection.immutable.TreeMap
import scala.collection.mutable.Map
object Reducer {
def main(args: Array[String]) {
println("Reducer running")
@worthlesscog
worthlesscog / gist:3027281
Created July 1, 2012 07:15
Proportionally scale and smooth
/**
* @see http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
*/
private BufferedImage scale(BufferedImage image, int width, int height, int smoothingSteps) {
int w = image.getWidth();
int h = image.getHeight();
int targetH, targetW;
if (w > h) {
targetW = width;