Skip to content

Instantly share code, notes, and snippets.

@hoshi-takanori
hoshi-takanori / gob-example.go
Created May 27, 2015 08:30
How to use bytes.Buffer with gob encoder/decoder.
package main
import (
"bytes"
"encoding/gob"
"io"
)
type Data struct {
A int
@hoshi-takanori
hoshi-takanori / nolist.go
Last active February 24, 2024 18:56
http.FileServer with no directory listing.
// http://grokbase.com/p/gg/golang-nuts/12a9xcadca/go-nuts-disable-directory-listing-with-http-fileserver
package main
import (
"net/http"
"os"
)
type NoListFile struct {
@hoshi-takanori
hoshi-takanori / BinaryTree.swift
Last active March 24, 2019 21:04
Binary Tree in Swift 2.0.
//: Binary Tree
protocol CollectionTypeConvertible {
typealias T
var array: [T] { get }
}
class Node<T: Comparable> {
let value: T
var left: Node?
@hoshi-takanori
hoshi-takanori / SurrogatePair.java
Last active March 15, 2016 07:04
Surrogate pair index conversion in Java.
public class SurrogatePair {
public static int convertToUTF16Index(String str, int index) {
int result = 0;
for (int i = 0; i < index && result < str.length(); i++, result++) {
if (result + 1 < str.length() &&
Character.isSurrogatePair(str.charAt(result), str.charAt(result + 1))) {
result++;
}
}
return result;
@hoshi-takanori
hoshi-takanori / sample.svg
Last active March 20, 2016 08:53
Convert SVG to Android VectorDrawable in Haskell.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@hoshi-takanori
hoshi-takanori / MainActivity.java
Created May 12, 2016 13:03
RecyclerView with sections.
package com.example.recyclertest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
class Person {
let name: String
init(name: String) { self.name = name }
func printName() { print("name = \(name)") }
deinit { print("\(name) is being deinited") }
}
var p: Person? = Person(name: "Hoshi")
p?.printName() // name = Hoshi
extension Optional {
func isNilOr(block: Wrapped -> Bool) -> Bool {
switch self {
case .None:
return true
case .Some(let value):
return block(value)
}
}
}
#import <Foundation/Foundation.h>
const char *kindForString(NSString *str)
{
if (str.length > 1) {
return "surrogate";
}
if ([@"0123456789" containsString:str]) {
return "digit";
@hoshi-takanori
hoshi-takanori / rename-imageset.sh
Created December 2, 2016 03:13
Rename xcode image assets.
#!/bin/sh
old="$1"
new="$2"
if [ "x" = "x$old" -o "x" = "x$new" ]
then
echo "usage $0 old new"
exit 2
fi