Skip to content

Instantly share code, notes, and snippets.

@kiritsuku
kiritsuku / bf.scala
Created May 16, 2016 13:02
A brainfuck interpreter in Scala
object Test extends App {
new Interpreter("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.")
}
class Interpreter(str: String) {
val prog = """[^\+\-\<\>\.\,\[\]]""".r replaceAllIn (str, "")
val openBrackets = findBraces(0, Nil, Map.empty)
val closeBrackets = openBrackets map { _.swap }
exec()
@kiritsuku
kiritsuku / ComplexTcpClient.scala
Last active August 15, 2018 06:11
Complex TCP server and client
import scala.io.StdIn
import scala.util._
import akka.actor._
import akka.stream._
import akka.stream.scaladsl._
import akka.stream.stage._
import akka.util._
object ComplexTcpClient extends App {
@kiritsuku
kiritsuku / SimpleTcpClient.scala
Last active April 29, 2018 05:44
Simple TCP client
import scala.io.StdIn
import scala.util._
import akka.actor._
import akka.stream._
import akka.stream.scaladsl._
import akka.stream.stage._
import akka.util._
object SimpleTcpClient extends App {
@kiritsuku
kiritsuku / SimpleTcpServer.scala
Last active April 26, 2018 09:57
Simple TCP server
import scala.util._
import akka.actor._
import akka.stream._
import akka.stream.scaladsl._
import akka.util._
object SimpleTcpServer extends App {
val address = "127.0.0.1"
@kiritsuku
kiritsuku / ClickStreamExample.scala
Last active September 24, 2018 00:44
click stream example
import java.awt.Dimension
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.JFrame
import scala.collection.immutable
import scala.concurrent.duration._
import akka.actor._
import akka.stream._
@kiritsuku
kiritsuku / log.txt
Created May 16, 2013 11:05
maven scalastyle plugin debug output
% mvn -X scalastyle:check :(
Apache Maven 3.0.5 (rNON-CANONICAL_2013-02-25_10-23_root; 2013-02-25 11:23:59+0100)
Maven home: /opt/maven
Java version: 1.6.0_27, vendor: Sun Microsystems Inc.
Java home: /usr/lib/jvm/java-6-openjdk/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.8.10-1-arch", arch: "amd64", family: "unix"
[INFO] Error stacktraces are turned on.
[DEBUG] Reading global settings from /opt/maven/conf/settings.xml
[DEBUG] Reading user settings from /home/antoras/.m2/settings.xml
@kiritsuku
kiritsuku / PatternMatch.scala
Created May 14, 2013 10:50
all pattern extracted to there own partial function
object Test extends App {
def x(in: Any): List[String] = in match {
case i: Int => List(i.toString)
case s: String => List(s)
case xs: List[_] => xs flatMap x
case _ => Nil
}
@kiritsuku
kiritsuku / ExprParsers.scala
Created May 7, 2013 00:35
simple math expression parser combinator in Scala
import scala.util.parsing.combinator.JavaTokenParsers
object Test extends App with ExprParsers2 {
println(eval(parseAll(expr, "1+1-2*3").get))
}
trait ExprParsers1 extends JavaTokenParsers {
@kiritsuku
kiritsuku / FloatParser.scala
Created May 7, 2013 00:24
parser combinator in Scala that parses floats
import scala.util.parsing.combinator.JavaTokenParsers
trait FloatParser {
private object parsers extends JavaTokenParsers {
implicit class RichElem[A <% Parser[A]](p: A) {
def <::>(q: Parser[List[A]]) = p ~ q ^^ { case m ~ n => m :: n }
}
@kiritsuku
kiritsuku / Macros.scala
Last active December 16, 2015 22:39
Scala 2.11 untyped hygienic swap macro
package macros
import language.experimental.macros
import scala.reflect.macros.Macro
object Macros {
def swap(a: _, b: _) = macro Impl.swap
}
trait Impl extends Macro {