Skip to content

Instantly share code, notes, and snippets.

View madfriend's full-sized avatar

Alex Chuchunkov madfriend

  • Yandex, OpenCorpora
  • St. Petersburg, Russia
View GitHub Profile
@madfriend
madfriend / foreach.php
Created June 29, 2012 09:03
PHP foreach fun
<?php
// How can foreach be implemented with while?
while (list($key, $value) = each($array)) {
print "$key is $value".PHP_EOL;
}
// How to get rid of @ in foreach (@$arr .. ) ?
// (this is always a bad practice, but anyway)
@madfriend
madfriend / loops.php
Created August 22, 2012 18:43
PHP loop API
<?php
function loop($count, $callback) {
if (is_int($count)) {
for ($i = 0; $i < $count; $i++) {
$callback($i);
}
}
elseif(is_array($count)) {
$_copy = $count;
@madfriend
madfriend / gist:3429018
Created August 22, 2012 20:19
Bot API example
<?php
// A short example of my current bot API usage
hear("/^close( issue)? #(?P<id>\d+)/iu", function($bot, $msg, $matches) {
// This bot likes to work with Redmine
$issue = new \Redmine\Issue();
$issue = $issue->find($matches['id']);
// Is issue present?
@madfriend
madfriend / ColorCLI.php
Created October 10, 2012 13:15 — forked from donatj/ColorCLI.php
Simple CLI color class
<?php
class Colorize {
static $foreground_colors = array(
'bold' => '1', 'dim' => '2',
'black' => '0;30', 'dark_gray' => '1;30',
'blue' => '0;34', 'light_blue' => '1;34',
'green' => '0;32', 'light_green' => '1;32',
'cyan' => '0;36', 'light_cyan' => '1;36',
@madfriend
madfriend / test.tag
Last active November 26, 2015 10:51
<my-parent-tag>
<div>
this is parent tag
<script type="riot/tag" src="https://rawgit.com/madfriend/51a535e989b710429334/raw/18753e624291e350e3f230aaece6a93be3785299/test2.tag"></script>
<my-tag>this should be replaced by my-tag contents</my-tag>
</div>
</my-parent-tag>
<my-tag>
hello
</my-tag>
@madfriend
madfriend / keeping-up-with-open-source.md
Created September 9, 2013 08:09
Keeping up with Open Source
@madfriend
madfriend / libtraits.php
Created April 29, 2012 21:27
PHP 5.4 Pattern Traits
<?php
namespace traits;
trait Singleton {
// Для тех, кто не предусмотрел конструктор в классе
private function __construct() {
}
#! coding: utf-8
# sklearn = библиотека scikit-learn (http://scikit-learn.org)
# Установить можно, например, так:
# сначала поставить дистрибутив Anaconda (https://www.continuum.io/downloads#_windows)
# а потом в терминале "conda install sklearn".
# Нам из sklearn нужно не так много
from sklearn.linear_model import LogisticRegression
# в sklearn есть удобная функция для создания
# отчетов по качеству классификации (Precision, Recall, F1)
from sklearn.metrics import classification_report
@madfriend
madfriend / preg_match_callback.php
Created April 17, 2013 20:14
preg_match_all + callbacks = preg_match_callback. No support for flags though.
<?php
function preg_match_callback($expression, $subject, $callback) {
static $matches_count = 0;
preg_replace_callback($expression, function($matches) use($callback) {
call_user_func($callback, $matches);
}, $subject, -1, $matches_count);
return $matches_count;
}