Skip to content

Instantly share code, notes, and snippets.

View rbobillot's full-sized avatar

Raphael Bobillot rbobillot

View GitHub Profile
@rbobillot
rbobillot / EncoreLaPuissance.scala
Last active August 29, 2015 14:19
EncoreLaPuissance.scala
class Puissance(init:Int)
{
private var _maVariable = init
def maVariable = _maVariable
def maVariable_= (n:Int) {
_maVariable = n
}
}
implicit def facto( x:Int ) = new {
def ! = (1 to x).foldLeft( BigInt(1) )( _ * _ )
}
println( 8! ) //equivaut a un simple appel de methode -> println( 8.! )
import scala.language.reflectiveCalls
import scala.language.implicitConversions
object MyMath {
implicit def doubleOps(x: Double) = new {
def **(n:Double):Double = Math.pow( x, n )
def <<<(n:Double):Double = Math.pow( 2, n ) * x
}
val tolerance = 0.0001
def isCloseEnough( x: Double, y: Double ) = Math.abs( (x - y) / x ) < tolerance
def fixedPoint( f: Double => Double )( first: Double ) = {
def iterate(guess: Double): Double = {
val next = f(guess)
if (isCloseEnough( guess, next ))
next
import scala.language.postfixOps
implicit class factorial(n:Int) {
def ! = (1 to n).foldLeft(BigInt(1))(_ * _)
}
println( (5!) * (5!) )
@rbobillot
rbobillot / cp.scala
Last active August 29, 2015 14:21
In Scala (and Java), it is SO EASY to read any file (text, binary...)
import java.io._
object Read
{
val out = "dest"
val ext = ".out"
def main( av:Array[String] )
{
try {
import java.io.File
/*
* list files in "/"
*/
new File("/").listFiles.sortWith( _.toString < _.toString ).map(println)
/*
* make directory
*/
implicit def manOf[T: Manifest]( t:T ) = new {
def getType = manifest[T]
}
implicit def range( x:Int ) = new {
def ->( n:Int ) = (x to n).toList
}
val list = (1 -> 7)
val res = list.mkString("[", ",", "]")
import scala.io.Source._
try
{
val datas = fromFile( args(0) ).mkString // file's text to a BIG string
.groupBy( x => x ) // group occurrences into strings in a map (Char -> String)
.mapValues( _.size ) // converts map (Char -> Int) => where Int is: String.size
.toList // map to list (preparing it for sorting)
.sortWith( _._2 > _._2 ) // reverse sorting with the 2nd value of each element
def yolo( x ):
p1 = x * x * x
p2 = x + x * x + x * x * x
if p1 < (1<<x) and (1<<x) < p2 and x != (x-(~(-x))):
print ( (x/(x/2))*(x-(~x)) )
else:
yolo(x + 1)
yolo(0)