Skip to content

Instantly share code, notes, and snippets.

View md2perpe's full-sized avatar

Per Persson md2perpe

View GitHub Profile
@md2perpe
md2perpe / gist:9683372
Last active August 29, 2015 13:57
Idea of new operator in PHP

Got an idea of a new assignment operator ?= in PHP.

Instead of writing

if (!$x)
    $x = $y;

one could use ?= and write

$x ?= $y;
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@md2perpe
md2perpe / Shapes
Created June 23, 2015 19:36
Square as a subclass of Rectangle
class Rectangle
{
protected double width, height;
constructor(double width, double height)
{
this.width = width;
this.height = height;
}
@md2perpe
md2perpe / gist:999471
Created May 30, 2011 21:09 — forked from GodsBoss/gist:960745
Access to protected base class members of a subclass via another subclass.
<?php
class Base
{
private $x;
public function getX()
{
return $this->x;
}
@md2perpe
md2perpe / tweet_preg.php
Created June 1, 2011 21:09
Twitter hashtag link base
<?php
$tweet = 'Detta är ett #test för min blog... #ignoreme #lördag #måla #mäktigt #coolt #åsa-nisse #()lol()';
$pattern = '/#(\pL+)/';
$replacement = '<a href="#$1">$0</a>';
echo preg_replace($pattern, $replacement, $tweet);
@md2perpe
md2perpe / mvf.js
Created June 1, 2011 22:05 — forked from fogus/gist:1002886
Methods and fields in Javascript
var M = function() { };
M.prototype = {
m : function() { return 42 }
};
var inst = new M();
inst.f = function() { return 42 };
@md2perpe
md2perpe / measurable.js
Created June 12, 2011 21:15 — forked from dachev/measurable.js
Is Measurable
// Recursive version
function isMeasurable(target, weights)
{
if (weights.length == 0)
return target == 0;
var first = weights.shift();
return
isMeasurable(target-first, weights) ||
@md2perpe
md2perpe / gist:1027615
Created June 15, 2011 17:32
Flyborg har fel
Att stödja en regeringen är inte samma sak som att sitta i regeringen. Flyborg påstod att MP var "i regeringsställning", vilket betyder att man verkligen sitter i regeringen.
data Tree t = EmptyTree
| Node t (Tree t) (Tree t)
deriving Show
nivel :: Int -> Int
nivel n = nivel' n 0
where
nivel' 0 _ = 0
nivel' 1 x = x
nivel' n x = nivel' (n `div` 2) (x + 1)
firstNonRep :: String -> Maybe Char
firstNonRep cs = fmap fst $ find (\(c, n) -> n == 1) $ foldl count [] cs
where
count [] c = [(c, 1)]
count (p@(c', n) : ps) c | c == c' = (c', n+1) : ps
| otherwise = p : count ps c