Skip to content

Instantly share code, notes, and snippets.

@searler
searler / HTTP.scala
Created November 10, 2012 17:21
Source code for Akka IO http server example
/**
* © 2012 Typesafe Inc.
*
* Extracted from http://doc.akka.io/docs/akka/2.1.0-RC2/scala/io.html
*/
import akka.actor._
import akka.util.{ ByteString, ByteStringBuilder }
import java.net.InetSocketAddress
@searler
searler / queryTxReport.scala
Created October 13, 2013 14:47
Delta value from Datomic txReport using Datomisca
import datomisca._
import scala.concurrent._
import scala.concurrent.duration.Duration
import datomisca.executioncontext.ExecutionContextHelper._
object TxReportQuery {
object PersonSchema {
object ns {
@searler
searler / annotateTxSnippet.scala
Created October 13, 2013 23:25
comment transaction to record side effect
// create attribute to record comment
val comment = Attribute(new Namespace("meta") / "comment", SchemaType.string, Cardinality.one)
Datomic.transact(comment)
// data from txReportQueue
val txr:datomisca.TxReport = ...
// assert "sent email" on all the transactions
Datomic.transact(txr.txData.map{case DDatom(_,_,_,tx,_) => tx}.distinct.
map{id => SchemaEntity.add(FinalId(id))(Props() + (comment-> "sent email"))})
@searler
searler / BaudotObservable.java
Created February 7, 2014 23:57
Baudot conversion using RxJava Observable
package baudot;
import java.util.Arrays;
import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.util.functions.Func1;
@searler
searler / Recursive.java
Last active August 29, 2015 13:56
RxJava I/O using recursive scheduler
public class Recursive {
public static void main(String[] args) throws InterruptedException {
final Scheduler scheduler = Schedulers.newThread();
Observable<Character> obs = Observable
.create(new OnSubscribeFunc<Character>() {
@Override
public Subscription onSubscribe(
@searler
searler / CamelBlocking.java
Created February 9, 2014 19:49
RxJava I/O using camel stream
public class CamelBlocking {
public static void main(String[] args) throws InterruptedException {
CamelContext camelContext = new DefaultCamelContext();
ReactiveCamel rx = new ReactiveCamel(camelContext);
Observable<String> observable = rx.toObservable(
"stream:in?scanStream=true", String.class);
observable.toBlockingObservable().forEach(new Action1<String>() {
@Override
public void call(String m) {
@searler
searler / editor.js
Created April 6, 2014 23:03
react.js textarea that updates via a web service
var Editor = React.createClass({
getInitialState: function() {
return {value: 'Hello!'};
},
handleChange: function(event) {
var caretPos = this.getDOMNode().selectionStart;
var msg = {text:event.target.value,caret:caretPos};
$.ajax({url: "/text", type: "POST",
data: JSON.stringify(msg),
contentType:"application/json; charset=utf-8", dataType:"json",
@searler
searler / CanadianEditor.scala
Created April 6, 2014 23:07
Play Action that implements provides web service for editor.js that discards the 'a' character.
def text = Action(parse.json) { request =>
val caret = (request.body \ "caret").as[Int]
val text = (request.body \ "text").as[String]
val trimmed = text.filterNot(_ == 'a')
Ok(Json.obj("caret" -> (caret-(text.length-trimmed.length)), "text" -> trimmed))
}
@searler
searler / OptimizedVariableSizeCodec.scala
Last active August 29, 2015 14:17
This code supports on over-the-wire serialization where the payload is normally very small (10s of bytes) but the maximum is 2^32 bytes.
val size: Codec[Int] = new Codec[Int] {
private val marker = 255
override def sizeBound = uint(8).sizeBound | uint(32).sizeBound
override def encode(value: Int) = if (value < marker)
uint(8).encode(value)
else
(uint(8) ~ uint(32)).encode(marker ~ value)
override def decode(bits: BitVector) = {
uint(8).decode(bits) match {
case Attempt.Successful(DecodeResult(value, remainder)) if value == marker => int(32).decode(remainder)
@searler
searler / SerialProtocol.scala
Created March 21, 2015 18:26
A simple remote control protocol codec for a serial port
import scodec.Codec
import scodec.codecs._
import scodec.bits.ByteVector
import scodec.bits.BitVector
import scodec.Attempt
import scodec.DecodeResult
object SerialProtocol {
sealed trait Parity