Skip to content

Instantly share code, notes, and snippets.

(defn- debounce-future
"Returns future that invokes f once wait-until derefs to a timestamp in the past."
[f wait wait-until]
(future
(loop [wait wait]
(Thread/sleep wait)
(let [new-wait (- @wait-until (System/currentTimeMillis))]
(if (pos? new-wait)
(recur new-wait)
(f))))))
(defn filter-map
"Returns map with items in m for which f returns true.
f should take 2 arguments [key value]"
[f m]
(into {} (for [[k v] m :when (f k v)] [k v])))
(defn map-map
"Returns map after applying f to key/values in m.
f should take 2 arguments [key value] and return a sequence of 2 items [key' value']"
[f m]
@loganlinn
loganlinn / p4.clj
Last active December 12, 2015 10:39
(ns euler.p4
"A palindromic number reads the same both ways. The largest palindrome made
from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.")
(defn palindromic?
[n]
(loop [s (seq (str n))]
(if (empty? s)
@loganlinn
loganlinn / dijkstra.clj
Last active March 21, 2022 15:01
Dijkstra's Algorithm in Clojure
(def ^:private inf (Long/MAX_VALUE))
(defn neighbors
"Returns n's neighbors, optionally filtered if unvisited"
([g n] (get g n {}))
([g n uv] (select-keys (neighbors g n) uv)))
(defn update-costs
"Returns costs updated with any shorter paths found to curr's unvisisted
neighbors by using curr's shortest path"
<?php
$data = array(
array(
'user_id' => 1,
'total_score' => 1,
),
array(
'user_id' => 2,
'total_score' => 1,
@loganlinn
loganlinn / switch_continue.php
Last active December 19, 2015 05:19
What do you expect would be output by this script?
<?php
// some math operations
$instructions = array(
array('*', 2, 4),
array('/', 2, 4),
array('/', 2, 0),
);
// compute
require 'nokogiri'
require 'open-uri'
require 'colorize'
require 'ripl'
require 'yaml'
class RegexWorkshop
def initialize()
reset
<?php
class TestClass {
private $id;
public function __construct($id) {
$this->id = $id;
}
public function getPublic() {
<?php
function all_gens_valid($gens) {
foreach ($gens as $gen) {
if (!$gen->valid()) {
return false;
}
}
return true;
}
<?php
function gen_file_lines($filename) {
try {
$fp = fopen($filename, 'r');
while (($line = fgets($fp)) !== false) {
yield $line;
}
} finally {