Skip to content

Instantly share code, notes, and snippets.

@cuixin
cuixin / fibonacci.lua
Created July 6, 2018 03:07
lua fibonacci with closure.
local function my_ipairs(t)
local i = 1
return function()
local v = t[i]
if v then
local ii = i
i = i + 1
return ii, v
else
return nil
@cuixin
cuixin / solve_birthday.go
Last active June 4, 2018 07:55
birthday problem solved in golang.
package main
import "fmt"
func main() {
count := 60
diff := float64(1.0)
for i := 0; i <= count; i++ {
diff *= float64((365 - float64(i)) / 365) // would be all different day.
fmt.Printf("times %d, diff [%.2f%%], same [%.2f%%]\n", i, diff*100, (1-diff)*100)
@cuixin
cuixin / print_r_test.lua
Created May 4, 2018 09:21
lua print_r test
x = {
value = 1,
text = "test",
inest = {
aaaa = "1",
eee = {
abc = 1
},
bl = true,
}
@cuixin
cuixin / slice_binary.go
Created April 17, 2018 05:12
Using reflect.SliceHeader to marshal data.
package main
import (
"fmt"
"reflect"
"unsafe"
)
type NestType struct {
S string
@cuixin
cuixin / map_perf.go
Created March 12, 2018 11:58
This is demonstrate the golang's gc latency when using map to storing struct or pointer. ref: https://groups.google.com/forum/#!topic/Golang-nuts/baU4PZFyBQQ
package main
import (
"fmt"
"os"
"runtime"
"time"
)
// Results of this program on my machine: (linux amd64, go 1.4):
@cuixin
cuixin / naive_test.sh
Created February 6, 2018 05:22
naive chain written in golang.
#!/bin/bash
curl -v "http://localhost:8080/"
curl -v --data-urlencode "value=1" "http://localhost:10888"
curl -v --data-urlencode "value=2" "http://localhost:10888"
curl -v --data-urlencode "value=3" "http://localhost:10888"
curl -v --data-urlencode "value=4" "http://localhost:10888"
curl -v --data-urlencode "value=5" "http://localhost:10888"
curl -v "http://localhost:8080/"
@cuixin
cuixin / build.bat
Created December 26, 2017 09:02
my unity game daily build script
mkdir C:\DailyBuild\%date:~10,4%%date:~4,2%%date:~7,2%
cd C:\MyGame
copy /Y ProjectSettings.asset MyGame\Project\ProjectSettings\
"C:\Program Files\Unity\Editor\Unity.exe" -batchmode -nographics -quit -projectPath "C:\MyGame\Project" -buildTarget Win -buildWindowsPlayer "C:\DailyBuild\%date:~10,4%%date:~4,2%%date:~7,2%\cm.exe"
@cuixin
cuixin / hlist_del_example.c
Created December 24, 2017 07:05
This is an example of using pointer of pointer to delete singly linked list element.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node* next;
} node_t;
void print_list(node_t* head) {
node_t* cur = head;
@cuixin
cuixin / go_bootstrap.sh
Created October 31, 2017 02:42
make minimal go build bootstrap files to build go again.
#!/bin/bash
# Ensure that you have compiled the entire go source.
# set -x
set -e
[ ! -d go ] && { echo "There is no go source code here."; exit 1; }
[ -d go_bootstrap ] && { echo "You have had go_bootstrap directory."; exit 1;}
mkdir -p go_bootstrap/pkg
echo "Create go_bootstrap directory successful"
cp -r go/{bin,src} go_bootstrap/
cp -r go/pkg/{include,linux_amd64,tool} go_bootstrap/pkg/
@cuixin
cuixin / json_to_map.go
Created October 25, 2017 01:53
json to map[string]interface{} example in golang
package main
import (
"encoding/json"
"fmt"
)
func dumpMap(space string, m map[string]interface{}) {
for k, v := range m {
if mv, ok := v.(map[string]interface{}); ok {