Skip to content

Instantly share code, notes, and snippets.

@icejoywoo
icejoywoo / string_interpolation.scala
Created June 30, 2018 15:12
Scala string interpolation
// scala string interpolation examples
// https://docs.scala-lang.org/overviews/core/string-interpolation.html
import scala.language.implicitConversions
object TestStringInterpolation extends App {
// s string interpolation
val name = "James"
@icejoywoo
icejoywoo / IINA play.applescript
Created June 30, 2018 07:24
Alfred2 remote control NSApplceScript
on alfred_script()
tell application "IINA"
activate
tell application "System Events" to keystroke "space"
end tell
end alfred_script
@icejoywoo
icejoywoo / implicit_class.scala
Last active June 30, 2018 14:30
implicit simple usage
// from https://www.safaribooksonline.com/library/view/scala-cookbook/9781449340292/ch01s11.html
import scala.language.implicitConversions
// String to user-defined case class
implicit class Name(name: String) {
def toName(): Name = new Name(name)
override def toString() = s"Name($name)"
}
// Name("Joe").toName
@icejoywoo
icejoywoo / rapidjson_demo.cpp
Created June 12, 2018 12:51
rapidjson cpp 测试
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
using namespace rapidjson;
int main(int argc, char* argv[]) {
const char* json = "{\n"
" \"hello\": \"world 中文测试\",\n"
" \"t\": true ,\n"
@icejoywoo
icejoywoo / oomscore.sh
Created April 17, 2017 08:57
List 20 highest OOM score process.
#!/bin/bash
for proc in $(find /proc -maxdepth 1 -regex '/proc/[0-9]+'); do
printf "%2d %5d %s\n" \
"$(cat $proc/oom_score)" \
"$(basename $proc)" \
"$(cat $proc/cmdline | tr '\0' ' ' | head -c 100)"
done 2>/dev/null | sort -nr | head -n 20
@icejoywoo
icejoywoo / y-combinator.js
Last active March 22, 2017 02:10
y combinator simple derivation in javascript & python
// Y combinator:http://yaodanzhang.com/blog/2015/06/11/functional-programming-fundamental-y-combinator/
// lambda 演算:http://liujiacai.net/blog/2014/10/12/lambda-calculus-introduction/
// Jim Weirich: Adventures in Functional Programming:https://vimeo.com/45140590
// original recursive function
var factorial = function (n) {
return n == 1 ? 1 : n * factorial(n - 1);
};
#!/usr/bin/env python
# encoding: utf-8
"""
@author: wujiabin
@date: 2016.5.4
@brief: based on http://flask.pocoo.org/snippets/134/
"""
import os
@icejoywoo
icejoywoo / resolve.py
Created March 10, 2016 05:22
resolve the dotted name to python object (copy from logging.config)
def _resolve(name):
"""Resolve a dotted name to a global object."""
name = name.split('.')
used = name.pop(0)
found = __import__(used)
for n in name:
used = used + '.' + n
try:
found = getattr(found, n)
except AttributeError:
#!/usr/bin/env python
def trailing_zeroes(num):
"""Counts the number of trailing 0 bits in num."""
if num == 0:
return 32 # Assumes 32 bit integer inputs!
p = 0
while (num >> p) & 1 == 0:
p += 1
return p
@icejoywoo
icejoywoo / nat.py
Last active January 14, 2016 02:56
peano numbers
#!/usr/bin/env python2.7
# encoding: utf-8
""" the same with scala code belows
abstract class Nat {
def isZero: Boolean
def predecessor: Nat
def successor: Nat = new Succ(this)
def +(that: Nat): Nat
def -(that: Nat): Nat