Skip to content

Instantly share code, notes, and snippets.

View kojiromike's full-sized avatar

Michael A. Smith kojiromike

View GitHub Profile
Interactive shell
php > $x=array(1=>1,2=>1,3.3=>1);
php > $x
php > ;
php > print_r($x);
Array
(
[1] => 1
[2] => 1
@kojiromike
kojiromike / gist:4506569
Created January 10, 2013 23:05
lol php
<?php
class Foo {
function hw() {
return 'hello world';
}
}
$x = new Foo;
var_dump(is_callable(array($x, 'hw')));
-->
@kojiromike
kojiromike / gist:5068968
Created March 2, 2013 00:14
I love recursion
function askForAHonestAnswer () {
return prompt("Do you like me?") === 'Yes' || askForHonestAnswer();
}
@kojiromike
kojiromike / gist:6655738
Last active December 23, 2015 15:29
Example of differences between slice copy and slice mutation of a list
>>> a = b = c = list(range(12))
>>> a is b and a is c
True
>>> a = a[2:]
>>> a
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> a is b and a is c
False
>>> b is c
True
@kojiromike
kojiromike / gist:6754695
Last active December 24, 2015 06:08
Try to set a dict to true at a key only if the key already exists in the dict. Assume dict is a vanilla dict.
def setExistingKeyToTrue(addict, key):
"""Try to set a dict to True at a key only if the key already exists in the dict. Assume addict is a vanilla dict. Try to do this with only a single hash table lookup."""
try:
addict[key] = bool(addict[key]) # Fails criteria, two hash lookups
addict[key] = True # Fails criteria, always sets value
except KeyError:
pass
./.cabal/lib/dlist-0.5
./.cabal/lib/either-3.4.1/ghc-7.6.3/Control/Monad/Trans
./.cabal/lib/either-3.4.1/ghc-7.6.3/Control/Monad
./.cabal/lib/either-3.4.1/ghc-7.6.3/Control
./.cabal/lib/either-3.4.1/ghc-7.6.3
./.cabal/lib/either-3.4.1
./.cabal/lib/email-validate-1.0.0/ghc-7.6.3/Text/Email
./.cabal/lib/email-validate-1.0.0/ghc-7.6.3/Text
./.cabal/lib/email-validate-1.0.0/ghc-7.6.3
./.cabal/lib/email-validate-1.0.0
#!/usr/bin/env python
correct = [[[1,2,3],
[2,3,1],
[3,1,2]],
[[1, 2, 3, 4],
[2, 3, 4, 1],
[4, 1, 2, 3],
[3, 4, 1, 2]]]
@kojiromike
kojiromike / anyall.py
Created October 16, 2013 02:54
I would like Python's any/all better if they took predicates.
from functools import reduce
nopred = lambda i: i
def any(it, pred=nopred):
'''True if pred(i) is True for any member of the iterable `it`.
If pred is None, pred(i) == i.
Like the builtin `any`, return False if `it` is empty.'''
if not callable(pred):
pred = nopred
def accumulate(prev, cur):
<?php
// Variable for keeping track if a user has posted. Set to false initially. If found it will be changed to true.
$alreadyPosted = false;
// Loop through all the posts
foreach ($posts_array as $post) {
// Check to see if the person in the database ($post['name']) matches the supplied name ($name)
if ($post['name'] == $name) {
// It does. Great. Set the variable to true.
$alreadyPosted = true;
break;
from collections import deque
last3 = deque(maxlen=3)
def pings():
while True:
yield timepassed() * 17000 # Assume timepassed is updated somehow
# This may seem not D.R.Y., but there are only three…
ping = next(pings) # ;) Who knows how the pings are gotten
print("Current average: " + ping) # avg = ping/1