Skip to content

Instantly share code, notes, and snippets.

@hisui
hisui / Application.scala
Created March 27, 2014 03:57
Play2.1+WebSocket
package controllers
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.Play.current
import play.api.libs.functional.syntax._
import play.api.libs.json._
import play.api.libs.json.Json.toJson
import play.api.libs.iteratee.{Concurrent, Iteratee}
import play.api.libs.concurrent.Akka
@hisui
hisui / SFSeq.hpp
Last active August 29, 2015 13:59
[iOS] Immutable vector for sharing use.
#ifndef __sf_iOS_SFSeq_hpp_
#define __sf_iOS_SFSeq_hpp_
#include <memory>
#include <vector>
#include <iostream>
namespace sf {
template<typename A> class SeqBuilder;
@hisui
hisui / SFLRUCache.hpp
Last active August 29, 2015 13:59
[iOS] LRU cache
#ifndef __sf_iOS_SFRLUCache_hpp_
#define __sf_iOS_SFRLUCache_hpp_
#include <unordered_map>
#include <utility>
namespace sf {
// http://twitter.github.io/commons/apidocs/com/twitter/common/util/caching/LRUCache.html
template<typename KeyT, typename ValT
@hisui
hisui / iconv.mm
Last active August 29, 2015 14:00
[iOS] iconv+NSString
#include <iconv.h>
static NSString *decode(const char *encoding, char *src, size_t len, double capacityRatio=2)
{
if (len == 0) {
return @"";
}
auto size = size_t(len * capacityRatio);
auto dest = (char*) malloc(size);
size_t pos = 0;
@hisui
hisui / .gitignore
Created May 10, 2014 05:00
[iOS] .gitignore for Xcode project
# http://stackoverflow.com/questions/49478/git-ignore-file-for-xcode-projects
.DS_Store
*.swp
*~.nib
DerivedData/
build/
xcuserdata
xcuserdata/**/*
*.pbxuser
@hisui
hisui / currying.hpp
Last active August 29, 2015 14:04
[C++14] Making a currying functor from a functor.
#include <utility>
#include <tuple>
namespace {
using std::forward;
using std::move;
using std::tuple;
template<size_t H, size_t ...T> struct apply_by_tuple
@hisui
hisui / monad.swift
Last active August 29, 2015 14:06
Monad in Swift
// A trivial type just for this example
final class Id <A> {
let raw: A
init(_ a: A) { self.raw = a }
}
// A typeclass of the Monad
infix operator >>| { associativity left precedence 95 }
public protocol Monad {
typealias Elem
@hisui
hisui / undefined.swift
Created September 21, 2014 00:07
undefined
func undefined<A>() -> A { return (nil as A?)! }
@hisui
hisui / .scala
Created September 27, 2014 02:40
[Swift] currying
('A' to 'F').foldLeft(Seq(Seq[Char]())) { (a, c) => a :+ (a.last :+ c)}.tail.foreach { a =>
val `A,` = a.mkString(", ")
val `->` = a.mkString(" -> ")
println(s"""
func curried<${`A,`}, Z>(fn: (${`A,`}) -> Z) -> (${`->`} -> Z) {
return ${ a.foldRight(s"fn(${`A,`})") { (c, a) => s"{ $c in $a }" }.toLowerCase }
}
""")
}
@hisui
hisui / hotp.scala
Last active August 29, 2015 14:11
HOTP
def HMAC_SHA1(key: Array[Byte], text: Array[Byte]): Array[Byte] = {
val mac = Mac.getInstance("HmacSHA1")
mac.init(new SecretKeySpec(key, "RAW"))
mac.doFinal(text)
}
def hton64(n: Long): Array[Byte] =
(0 to 7)
.map(i => (n >>> 8 * (7 - i)) & 0xff)
.map(_.toByte)