Skip to content

Instantly share code, notes, and snippets.

@cark
cark / gist:249761
Created December 5, 2009 16:59 — forked from offby1/gist:249755
(ns anagrams
(:use [clojure.contrib.str-utils])
(:require [clojure.contrib.str-utils2 :as su])
(:use [clojure.test]))
(set! *warn-on-reflection* true)
(defn contains-vowel? [w]
(some #(su/contains? w %)
(list "a" "e" "i" "o" "u" "y")))
(defmacro safe-agent [& body]
`(try
~@body
(catch Exception e#
(log [:agent :error] {:exception (str e#)
:agent (with-out-str (pr @*agent*))})
@*agent*)))
@cark
cark / gist:3667614
Created September 7, 2012 16:34
state-m fetch-val bug and efficiency issue
;; the bug
(defn fetch-val
"Return a state-monad function that assumes the state to be a map and
returns the value corresponding to the given key. The state is not modified."
[key]
(domonad state-m
[s (fetch-state)]
(key s))) ;; only works for keyword keys
@cark
cark / gist:3709973
Created September 12, 2012 21:13
one of two things
user> (defmacro as-doubled-pair [v]
`(let [v# (mapv (partial * 2) ~v)]
[v# v#]))
#'user/as-doubled-pair
user> (macroexpand (as-doubled-pair [1 2 3]))
[[2 4 6] [2 4 6]]
user> (defmacro as-doubled-pair2 [v]
(let [v (mapv (partial * 2) v)]
`(do [~v ~v])))
@cark
cark / gist:3709981
Created September 12, 2012 21:14
one of two things
user> (defmacro as-doubled-pair [v]
`(let [v# (mapv (partial * 2) ~v)]
[v# v#]))
#'user/as-doubled-pair
user> (macroexpand (as-doubled-pair [1 2 3]))
[[2 4 6] [2 4 6]]
user> (defmacro as-doubled-pair2 [v]
(let [v (mapv (partial * 2) v)]
`(do [~v ~v])))
user> (defmacro bleh [body]
`(fn [{:keys [~'action]}]
~body))
#'user/bleh
user> ((bleh action) {:action 12})
12
insertPerson :: MonadState MyDb m => Person -> m (Fact Person)
insertPerson = insertRecord personRel
--fillit :: MonadState MyDb m => m ()
fillit = do
a <- insertPerson (Person "john")
b <- insertPerson (Person "paul")
return ()
-- if i leave the type declaraion of fillit commented, i get this error :
(ns cara.type.core)
;(declare Nothing Just)
(defprotocol PDataType
(get-constructors [self]))
(defprotocol PConstructor
(get-datatype [self]))
@cark
cark / orc.hs
Created September 25, 2012 08:32
My take on the orc thingie
{-# LANGUAGE Rank2Types #-}
module Main where
import System.Random
import Control.Monad
import Control.Monad.State
import Data.List
data Player = Player { pHealth :: Int
, pAgility :: Int
, pStrength :: Int
app.core> (let [maybe (fn [val fun] (if (nil? val) nil (fun val)))]
[(-> nil (maybe inc)) (-> 1 (maybe inc))])
[nil 2]