Skip to content

Instantly share code, notes, and snippets.

View fogus's full-sized avatar
💭
attempting to learn how to better learn

Fogus fogus

💭
attempting to learn how to better learn
View GitHub Profile
(def mvec #(vec (map % %&)))
(mvec #(* % %) 1 2 3 4 5)
(mvec #(.toUpperCase %) "test")
;; The original doesn't return vectors -- it returns lazy seqs
(defmacro defunits-of [quantity base-unit & units]
(let [magnitude (gensym)
unit (gensym)
conversions (apply hash-map base-unit 1 units)]
`(defmacro ~(symbol (str "unit-of-" quantity))
[~magnitude ~unit]
`(* ~~magnitude
~(relative-units ~unit ~conversions)))))
@fogus
fogus / gvec.clj
Created August 27, 2010 01:43 — forked from richhickey/gvec.clj
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(set! *warn-on-reflection* true)
@fogus
fogus / refactor.py
Created December 8, 2010 13:00 — forked from jdp/refactor.py
#!/usr/bin/env python
import sys
import types
import operator
class Runtime:
def __init__(self, env={}, stack=[]):
self.env = {
# Primitive words, not an impressive base but it works
@fogus
fogus / memoize.clj
Created December 20, 2010 13:33 — forked from cemerick/memoize.clj
(ns com.snowtide.clojure.memoize)
(defn- mutable-memoize
[f #^java.util.Map map]
(fn [& args]
(if-let [e (find map args)]
(val e)
(let [ret (apply f args)]
(.put map args ret)
ret))))
@fogus
fogus / gist:760694
Created December 31, 2010 03:45 — forked from qbg/gist:747841
(defsyntax-rules plet
(plet [& var rhs] & body)
((fn [& var] & body) & rhs))
(plet [a 1 b 2] (+ a b)) ;=> 3
;;; Implementation of defsyntax-rules
; Using defmacro:
(defmacro defsyntax-rules
[name & rt-pairs]
@fogus
fogus / defn.clj
Created December 31, 2010 03:47 — forked from qbg/defn.clj
;;; Almost complete opinionated defn implementation
;;; Does not put the arg vector on the metadata
(defsyntax-class distinct-argument-vector []
"distinct argument vector"
[]
[var ...]
:fail-when (check-duplicate (syntax (var ...))) "duplicate binding form")
(defsyntax-class docstring []
"docstring"
class DynamicImpl(x: AnyRef) extends Dynamic {
def _select_(name: String): DynamicImpl = {
new DynamicImpl(x.getClass.getMethod(name).invoke(x))
}
def _invoke_(name: String)(args: Any*) = {
new DynamicImpl(x.getClass.getMethod(name, args.map(_.asInstanceOf[AnyRef].getClass) : _*).invoke(x, args.map(_.asInstanceOf[AnyRef]) : _*))
}
override def typed[T] = x.asInstanceOf[T]
override def toString = "Dynamic(" + x.toString + ")"
}
@fogus
fogus / let.tcl
Created March 18, 2011 12:28 — forked from sstephenson/let.tcl
#!/usr/bin/env TEST=1 tclsh
# Lexical scoping in Tcl
proc let {block args} {
try {
set captured_vars [uplevel [list capture [block args]]]
set all_var_names [uplevel {info vars}]
foreach var [block args] value $args {
uplevel [list catch [list unset $var]]
@fogus
fogus / lisp.hs
Created May 1, 2011 11:13
Minimal Lisp in Haskell
{-# LANGUAGE OverloadedStrings #-}
{- To Run:
Load in ghci
:set -XOverloadedStrings (for convenience)
Execute repl expr -}
import Control.Applicative
import Data.Attoparsec hiding (Result)
import Data.Attoparsec.Char8 (char8, isDigit_w8, isSpace_w8)