This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Interactive shell | |
php > $x=array(1=>1,2=>1,3.3=>1); | |
php > $x | |
php > ; | |
php > print_r($x); | |
Array | |
( | |
[1] => 1 | |
[2] => 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Foo { | |
function hw() { | |
return 'hello world'; | |
} | |
} | |
$x = new Foo; | |
var_dump(is_callable(array($x, 'hw'))); | |
--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function askForAHonestAnswer () { | |
return prompt("Do you like me?") === 'Yes' || askForHonestAnswer(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./.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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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]]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |