Skip to content

Instantly share code, notes, and snippets.

View rbobillot's full-sized avatar

Raphael Bobillot rbobillot

View GitHub Profile
val f0 = (g0:Int=>Int, x:Int) => g0(x)
val h0 = ( x:Int ) => f0( p=>p+1, x )
println( "41 + 1 = " + h0(41) )
/******************************************************************************/
/******************************************************************************/
import java.net.URL
import java.io.File
import javax.imageio.ImageIO
//lazy val file = new File( "/tmp/testing" )
lazy val file = new URL( "http://goo.gl/vOYtrv" )
lazy val img = ImageIO.read( file )
lazy val width = img.getWidth
lazy val height = img.getHeight
def dimRefs = ( 160, 190 )
def deleteFile( name:String ) = new File(name).delete
def createImage( name:String ):BufferedImage = name match {
case n if n startsWith "http://" => ImageIO.read( new URL (name) )
case n if n startsWith "https://" => ImageIO.read( new URL (name) )
case _ => ImageIO.read( new File(name) )
}
object LeetCrypt
{
implicit class Encrypt( src:String )
{
def leetOfString( s:String ) = {
s.map(
c => c match {
case 'a' | 'A' => '4'
case 'b' => '6'
case 'B' => '8'
def infiniteArg( arg:Any* ) = arg.foreach( println )
infiniteArg( 1, 2, 3 )
infiniteArg( "1", 2, '3' )
@rbobillot
rbobillot / parseJson.scala
Last active September 8, 2015 02:14
Little example of powerful JSON parsing in Scala
import scala.io.Source
import scala.util.parsing.json._
object Main
{
/*
** FLICKR SPECIFIC FUNCTIONS
*/
def getFlickrInfosJson( content:String ) = {
val rawJson = content.split("\n")
import scala.language.implicitConversions
case class Node(nodeVal:Any, left:Option[Node] = None, right:Option[Node] = None)
{
def preorder(f:Node => Unit) {
f( this )
left.map( _ preorder f )
right.map( _ preorder f )
}
}
import java.util.{Random, Scanner}
//
// ScalaCorrection.scala
// ScalaCorrection
//
// Created by Remus32 on 20/09/15.
// Copyright (c) 2015 Remus32, and rbobillo(revision). All rights reserved.
object ScalaCorrection{
def factorial(n:Int):BigInt = ((1 to n) :\ BigInt(1))(_ * _)
// (1 to n).foldLeft(BigInt(1))(_ * _)
def recFactorial(n:Int):BigInt = if (n < 2) n else n * recFactorial(n-1)
def tailRecFactorial(n:Int):BigInt = {
def fac(n:Int, acc:BigInt):BigInt = n match {
case 1 => acc
case _ => fac(n-1, n*acc)
}
object TeslaTechQuestion {
case class Coord(x:Float, y:Float) // use case class, rather than Tuple
val A = Coord(487,143) // shema: B
val B = Coord(720,384) // A
val C = Coord(839,-235) // C
def gradient(a:Coord, b:Coord) = (b.y - a.y) / (b.x - a.x) // calculate 'a'
def intersec(a:Coord, b:Coord) = a.y - gradient(a,b) * a.x // calculate 'b'
def getCoord(a:Coord, b:Coord, x:Float) = (gradient(a,b) * x + intersec(a,b)) // calculate 'ax + b'