Skip to content

Instantly share code, notes, and snippets.

View hirak's full-sized avatar

Hiraku NAKANO hirak

View GitHub Profile
@hirak
hirak / stream.js
Created February 2, 2014 13:47
CURLOPT_FILEでHTTPレスポンスをファイルに直接書き込んでいくサンプル ref: http://qiita.com/Hiraku/items/9fb7a0df060e0424921d
var http = require('http');
http.createServer(function(req, res){
var i = 0;
var id = 100 * Math.random() | 0;
res.writeHead(200, {'Content-Type': 'text/plain'});
setInterval(function(){
var result = id + ':' + ++i;
console.log(result);
@hirak
hirak / typeOfの戻り値
Last active August 29, 2015 13:55
typeofを改善したtypeOf()関数 ref: http://qiita.com/Hiraku/items/87e5d1cdaaa475c80cc2
console.log(typeOf(undefined)); //'undefined'
console.log(typeOf(null)); //'null'
console.log(typeOf("str")); //'string'
console.log(typeOf(new String("str"))); //'String'
console.log(typeOf(true)); //'boolean'
console.log(typeOf(new Boolean(true))); //'Boolean'
console.log(typeOf(1)); //'number'
console.log(typeOf(NaN)); //'NaN'
console.log(typeOf(Infinity)); //'Infinity'
console.log(typeOf(-Infinity)); //'-Infinity'
@hirak
hirak / file0.php
Created February 20, 2014 15:11
SplFixedArrayで"純粋な配列"を作る ref: http://qiita.com/Hiraku/items/471747cacda58070f3a6
<?php
$a1 = [10,20,30]; //配列っぽい配列
$a2 = [2=>10, 0=>20, 1=>30]; //添え字は整数だが順番が変な配列
$a3 = ['a'=>10, 20, 30]; //添え字に文字列が混じってる
@hirak
hirak / file0.php
Created February 26, 2014 15:23
explode()の挙動がキモい ref: http://qiita.com/Hiraku/items/a58b8aac3f9e55cd1bd5
var_dump(explode(' ', ''));
//array("")
@hirak
hirak / FactoryDefault.php
Created February 26, 2014 23:47
Dependency Injectionを特定のDIコンテナに頼らず実現する ref: http://qiita.com/Hiraku/items/48fbdfca4b63c74494e9
<?php
class FactoryDefault
{
function createInfrastructureMailer()
{
return new SendmailMailer;
}
function createDomainTransferNewsletter()
{
@hirak
hirak / file0.php
Last active August 29, 2015 14:02
Iteratorをimplementsする奴は情弱。IteratorAggregateを使え ref: http://qiita.com/Hiraku/items/14722922441f9ed3fbbb
<?php
class A {
public $hoge = 1;
public $fuga = 2;
private $pri = 3;
protected $pro = 4;
}
$a = new A;
@hirak
hirak / file0.txt
Created July 5, 2014 03:48
Zend\LogでFingersCrossedモード(問題発生時に過去ログを一気に出力) ref: http://qiita.com/Hiraku/items/ebc2c6ec051c7dd15bd4
$ mkdir some-project
$ cd some-project
$ composer require 'zendframework/zend-log:*'
@hirak
hirak / curlstream.php
Last active August 29, 2015 14:12
curlstream
<?php
class CurlStream
{
/** @type resource $context */
public $context;
/** @type resource<url>[] */
private static $cache = array();
@hirak
hirak / msort.diff
Created June 29, 2011 14:33
merge sort patch
--- sort.js 2011-06-29 23:34:27.000000000 +0900
+++ sort2.js 2011-06-29 23:34:37.000000000 +0900
@@ -1,10 +1,10 @@
var sort = function(list, comparer) {
- if (!comparer) comparer = function(x, y) { return y - x; };
+ if (!comparer) comparer = function(x, y) { return x - y; };
return (function self(list) {
if (list.length < 2) return list;
var left = self(list.splice(0, list.length >>> 1)), right = self(list.splice(0));
while (left.length && right.length)
@hirak
hirak / file0.php
Created December 10, 2011 02:28
Yahoo!検索API経由でページの本文を取得するサンプル ref: http://qiita.com/items/1337
<?php
require_once 'Zend/Rest/Client.php';
//アプリケーションIDは各自で取得したものを使ってください。
$appid = 'プレミアム検索対応のアプリケーションIDをここに記入';
//APIのベースURLを指定する
$client = new Zend_Rest_Client('http://search.yahooapis.jp/PremiumWebSearchService/V1/webSearch');
//パラメータを設定