Skip to content

Instantly share code, notes, and snippets.

@axgle
axgle / socket.port.php
Created October 25, 2018 05:19
get valid random socket port with php
<?php
//get valid random socket port with php
$s = stream_socket_server("tcp://localhost:0");
$ss = stream_socket_get_name($s,0);
$pos =
strrpos($ss,":");
<?php
//HTTP server xample: hello world with php
//php myhttp.php
//open http://localhost:7788/
$h = stream_socket_server("tcp://0.0.0.0:7788");
print_r($h);
<?php
//stack is for LookBack (2018-10-22)
$d=range(1,10);
function p($d) {return print_r($d);}
@axgle
axgle / parle_test.php
Created October 21, 2018 11:32
parle lex parse test
<?php
use Parle\{Lexer,Parser,Token,Stack};
$p = new Parser();
/***
a=1;
b=2;
<?php
// based on https://github.com/axgle/Naive-Bayes-Classifier
class NativeBayes{
static function classify($table,$features,$category){
$db=new pdo("mysql:dbname=bayes","root","root");
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
$sql="select distinct($category) from $table";
$arg=array();
foreach($db->query($sql)->fetchAll() as $row){
<?php
function year_by_month($start=null,$end=null) {
$data=array();
$start=strtotime($start);
if($end===null){
$end=date("Y-m-d");
}
$end=strtotime($end);
$m_start=date("Y",$start)*12+(int)date("m",$start);
$m_end=date("Y",$end)*12+(int)date("m",$end);
package main
import (
"github.com/nsf/termbox-go"
)
var row int = 0
func echo(str string) {
x := 0
<?php
// http://www.php.net/shuffle
//http://www.cnblogs.com/Jerry-Chou/archive/2012/01/04/2311610.html
function sh($arr){
$n=count($arr);
while($n>1){
$n--;
$k=rand(0,$n);
$tmp=$arr[$n];
$arr[$n]=$arr[$k];
@axgle
axgle / undo.php
Last active August 29, 2015 14:01
<?php
class db{
static $count=0;//current data
static $undos=array();//stack
}
function add($n){
db::$count+=$n;
db::$undos[]=function() use($n){
db::$count-=$n;
@axgle
axgle / sync.Pool.md
Last active December 18, 2022 09:56

What is sync.Pool in golang and How to use it

sync.Pool (1/2)

Many Go libraries include custom thread-safe free lists, like this:

var objPool = make(chan *Object, 10)

func obj() *Object {

select {