Skip to content

Instantly share code, notes, and snippets.

@m0smith
m0smith / fizzbuzz.clj
Created September 22, 2012 15:52
fizzbuzz in Clojure
(take 100 (map #(let [s (str %2 %3) ] (if (seq s) s (inc %)) )
(range)
(cycle [ "" "" "Fizz" ])
(cycle [ "" "" "" "" "Buzz" ])))
@m0smith
m0smith / pipe.cljs
Created December 19, 2012 23:36
A ClojureScript usable version of pipe that will return a seq and a function to add elements to the end of the pipe. I found it here: http://clj-me.cgrand.net/2009/11/18/are-pipe-dreams-made-of-promises/ and just wanted to have a copy I could find
(defn pipe
"Returns a pair: a seq (the read end) and a function (the write end).
The function can takes either no arguments to close the pipe
or one argument which is appended to the seq. Read is blocking."
[]
(let [promises (atom (repeatedly promise))
p (second @promises)]
[(lazy-seq @p)
(fn
([] ;close the pipe
@m0smith
m0smith / featurec.clj
Created January 4, 2013 17:53
An example of Clojure core.logic that finds map that match a certain structure. Both functions are equilivent but one uses list comprehension and the other uses core.logic
(ns logicfun.core
;(:refer-clojure :exclude [==])
(:use [clojure.core.logic :rename {== ?==}]))
(def data [
{:id 1 :type :t1 :name "one" :other1 :o2}
{:id 2 :type :t2 :name "two" :other2 :o1 :another :ao2}
{:id 3 :type :t3 :name "three" :other2 :o1 :another :ao1}
])
@m0smith
m0smith / blackjack.clj
Last active December 14, 2015 22:29
Basic Black Jack engine in Clojure
(def suites [:heart :club :diamond :spade])
(def ranks
{
:ace [1 11]
:two [2]
:three [3]
:four [4]
:five [5]
:six [6]
:seven [7]
@m0smith
m0smith / iterate-while.clj
Last active December 14, 2015 23:59
Same as the core iterate function except the lazy sequence ends when the value is nil.
(defn iterate-while-1
[f x]
(when x
(cons x (lazy-seq (iterate-while f (f x))))))
(defn iterate-while
[f x]
(take-while identity (iterate f x)))
@m0smith
m0smith / ip.clj
Last active December 16, 2015 22:59
Get a list of active IP addresses for this host
(import (java.net NetworkInterface Inet4Address))
(defn ip-filter [inet]
(and (.isUp inet)
(not (.isVirtual inet))
(not (.isLoopback inet))))
(defn ip-extract [netinf]
(let [inets (enumeration-seq (.getInetAddresses netinf))]
(map #(vector (.getHostAddress %1) %2) (filter #(instance? Inet4Address %) inets ) (repeat (.getName netinf)))))
(ns topoged.tt
(:require [clj-time.core :refer [epoch]]))
(epoch)
@m0smith
m0smith / rps.clj
Last active December 19, 2015 16:59 — forked from puredanger/rps.clj
(require 'clojure.core.async :refer :all)
(def MOVES [:rock :paper :scissors])
(def BEATS {:rock :scissors, :paper :rock, :scissors :paper})
(defn rand-player
"Create a named player and return a channel to report moves."
[name]
(let [out (chan)]
(go (while true (>! out [name (rand-nth MOVES)])))
@m0smith
m0smith / gist:6083731
Created July 25, 2013 21:02
Stack Trace from Archiva
javax.servlet.ServletException: javax.jdo.JDODataStoreException: Insert request failed: INSERT INTO JDOAUTHENTICATIONKEY (DATE_CREATED,PURPOSE,MODEL_ENCODING,DATE_EXPIRES,FOR_PRINCIPAL,AUTHKEY) VALUES (?,?,?,?,?,?)
NestedThrowables:
SQL Exception: An SQL data change is not permitted for a read-only connection, user or database.
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
@m0smith
m0smith / wkp_Delicious.php
Last active December 20, 2015 07:28
A Lionwiki plugin that will pull data from Delicious. The usage is {delicious}user/tag{/delicious}. See https://delicious.com/developers/rssurls
<?php
/*
* Written by Matthew O. Smith <m0smith@yahoo.com>
* With {delicious}user/tag{/delicious} insert links from the user and tags
* With {delicious}user/tag?count=50{/delicious} you can spcifiy the number of items returned
*/
class Delicious
{
var $desc = array(