Skip to content

Instantly share code, notes, and snippets.

@alexpw
alexpw / poll.clj
Last active Aug 29, 2015
kathy's sandbox poll fn (quick and dirty)
View poll.clj
(defn poll []
(let [opts (atom {})]
(fn [& args]
(if (empty? args)
(when (seq @opts)
(print-table (for [[k v] (reverse (sort-by last @opts))]
{:name k :votes v})))
(doseq [arg args
:when (or (string? arg) (keyword? arg))
:let [arg (keyword arg)]]
@alexpw
alexpw / gist:93ee2082f0cd2a54496d
Created Nov 21, 2014
haskell fold/scan reimplementation exercise
View gist:93ee2082f0cd2a54496d
module FoldScan where
import Prelude hiding (foldl, foldl', foldl1,
foldr, foldr', foldr1,
scanl, scanl1, scanr, scanr1)
-- (map fn list) -> list
-- map is (1:1) with the original list and returns a copy of a list after applying a fn to each element
@alexpw
alexpw / gist:c229ab22a60c3ac4a5db
Last active Aug 29, 2015
ch1 hw - std and tail-call recursion
View gist:c229ab22a60c3ac4a5db
-- module cis194.ch1 where
-- std
toDigits' :: Integer -> [Integer]
toDigits' = reverse . f
where
f 0 = []
f x = (x `rem` 10) : f (x `div` 10)
@alexpw
alexpw / 2.1.suffix.clj
Created Oct 28, 2014
pfds 2.1 suffixes sml
View 2.1.suffix.clj
(defn suffixes
"Recursive suffixes"
[xs]
(if-let [xs (seq xs)]
(cons xs (suffixes (rest xs)))
[()]))
(defn suffixes
@alexpw
alexpw / 2.1.suffixes.sml
Created Oct 28, 2014
pfds 2.1 suffixes sml
View 2.1.suffixes.sml
(* use "suffix.sml"; *)
(* input = output *)
(* [1,2,3] = [[1,2,3], [2,3], [3], []] *)
(* std recursion *)
fun suffixes ([]) = [[]]
| suffixes (xs) = xs :: suffixes (tl xs);
View days-to-numbers.clj
(require '[clojure.string :as s])
(defn bool->int
[bool]
(if bool 1 0))
(defn single-char-days
"Translate two-char representation of certain day to one"
[days]
(-> days
@alexpw
alexpw / gist:c44ff885aadfef5c342e
Created Aug 28, 2014
xdebug segmentation fault 11; minimal test case
View gist:c44ff885aadfef5c342e
<?php
class F
{
public static $fns = [
'apply' => 'call_user_func_array',
'min' => 'min',
];
public static function __callStatic($method, $args)
@alexpw
alexpw / gist:61619aa5924514d1180a
Last active Aug 29, 2015
Example implementation of partial function application and currying in Javascript.
View gist:61619aa5924514d1180a
/**
* A partial function applicator.
*/
function partial() {
var fn = arguments[0];
var slice = Array.prototype.slice;
var partialArgs = slice.call(arguments, 1);
return function () {
var args = slice.apply(arguments);
return fn.apply(null, partialArgs.concat(args));
View gist:05980266ec3d4dd9714d
(mapcat (fn [[k coll]] (map vector (repeat k) coll))
[[:a [1 2]] [:b [3 4]]])
=> '([:a 1] [:a 2] [:b 3] [:b 4])
@alexpw
alexpw / gist:f20c7b3ac858003e07e2
Last active Aug 29, 2015
partition-by-seq: Search seqs in seq
View gist:f20c7b3ac858003e07e2
(defn partition-by-seq
"Partition coll by splitting each time it encounters sub-seq. The sub-seq
can optionally contain fns, just like partition-by."
[sub-seq coll]
(let [cnt-seq (count sub-seq)
match? (if (some fn? sub-seq)
(let [partials (mapv (fn [f]
(if (fn? f)
#(f %)
#(= f %))) sub-seq)]