Skip to content

Instantly share code, notes, and snippets.

@tyrcho
tyrcho / ExprParser.scala
Last active December 15, 2015 16:50 — forked from aneveux/LCEB.java
package info.daviot.lceb
import scala.util.parsing.combinator._
sealed trait Expr { def eval: Int }
case class Number(value: Int) extends Expr {
val eval = value
}
case class Prod(left: Expr, right: Expr) extends Expr {
def eval = left.eval * right.eval
}
@tyrcho
tyrcho / README.md
Last active September 10, 2018 06:09
Scala from (Windows) command line

A sample batch file running a scala script with process to call external commands.

The same can be done for *nix systems.

@tyrcho
tyrcho / Decompiled.java
Created May 17, 2013 11:37
Scala Import experiments
package misc;
import scala.LowPriorityImplicits;
import scala.Predef.;
import scala.collection.IterableLike;
import scala.collection.TraversableOnce;
import scala.collection.immutable.List;
import scala.collection.immutable.List.;
public final class ImportExperiment$
@tyrcho
tyrcho / config
Created May 21, 2013 15:36
Sample git config file when you have 2 remotes, one of which requires a proxy
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = http://a518291@kazan.priv.atos.fr/git/activiti-xa
[remote "github"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://github.com/Activiti/Activiti.git
proxy = http://proxy:3128
@tyrcho
tyrcho / ATGC.scala
Created May 22, 2013 15:56
Analyse the frequency of repeated chars in a given string
import scala.util.Random
import scala.collection.immutable.SortedMap
object ATGC extends App {
def randomString(size: Int) = List.fill(size)(randomChar).mkString
def randomChar = "ATGC"(Random.nextInt(4))
def analyze(s: String): Map[String, Int] = {
val (_, _, res) = (s + "Z").tail.foldLeft((s.head, 1, SortedMap.empty[String, Int] withDefaultValue 0)) {
case ((last, count, map), current) if current == last => (last, count + 1, map)
@tyrcho
tyrcho / SortTrello.scala
Created May 25, 2013 20:34
Gets all cards from a trello list, sorts them by last modification date, and prints this date together with the name of the card.
import scala.io.Source
import scala.util.parsing.json.JSON
object Sort extends App {
//enter your own values, these won't work !
val listId = "87308c95352724c5f1"
val key = "01e5ed84e035d27b365db068c"
val token = "56027af793b49845fde60505aa429ad82c6ec332a1f98170246"
val data = Source.fromURL(s"https://api.trello.com/1/lists/$listId/cards?key=$key&token=$token").getLines mkString
object Events {
object Event {
def build(typee: String) = {
typee match {
case "foo" => new FooEvent
case "bar" => new BarEvent
}
}
}
@tyrcho
tyrcho / blips.scala
Last active December 18, 2015 11:09
Exponentially decreasing blips
import scala.collection.immutable.SortedMap
import scala.collection.immutable.NumericRange
object Main extends App {
val halfLife: Double = 60
val data = Seq(
Blip("a", strength = 1),
Blip("b", strength = 1),
Blip("c", strength = 1),
@tyrcho
tyrcho / pca.sc
Last active November 25, 2020 21:37
Principal Component Analysis with Breeze (Scala)
// For an unkown reason, this works when copy/paste in ammonite but not with `amm pca.sc`
import $ivy.`org.scalanlp::breeze-natives:0.13.2`
import $ivy.`org.scalanlp::breeze-viz:0.13.2`
import $ivy.`org.scalanlp::breeze:0.13.2`
import breeze.linalg._
import breeze.linalg.svd._
import breeze.plot._
import scala.util.Random._
@tyrcho
tyrcho / ParallelPrimesCompute.scala
Last active December 24, 2015 22:09
scala primes
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.duration.DurationInt
object ParallelPrimesCompute extends App {
val primes: Stream[Long] =
2L #:: Stream.range(3, Long.MaxValue, 2).filter(
n => primes.takeWhile(_ <= Math.sqrt(n)).forall(n % _ != 0))