Skip to content

Instantly share code, notes, and snippets.

@mflatt
mflatt / functional.rkt
Created January 16, 2014 17:13
Programs by Jay and Matthew at Lambda Lounge Utah, 14 Jan 2014: * "functional.rkt" is from Jay's introduction to functional programming * "web.rkt" and "page.rkt" are from Matthew's explanation of Racket macros & modules
#lang racket
(require rackunit)
;; A singly linked list is either
;; - NULL
;; - pointer to data and a pointer a sll
(struct node (data-ptr next-ptr))
(define n4 (node 4 null))
(defmacro defactions
"Takes a list of function symbols and defines atom-swapping versions of each."
[& syms]
`(do ~@(for [sym syms
:let [bang (-> (name sym) (str "!") symbol)]]
`(def ~(with-meta bang
{:arglists (->> (resolve sym) meta :arglists
(map (fn [xs] (into ['state-atom] xs)))
(cons 'quote))})
(fn [state# & args#]
@c-spencer
c-spencer / core.clj
Last active December 23, 2015 02:49
Simple demo of type construction from Datomic with core.typed and a little glue
(defn create-type
"Extract a type from provided field idents stored in Datomic database at uri."
[uri type-name overrides]
(let [c (d/connect uri)
d (d/db c)
datomic-type-map {:db.type/string 'String
:db.type/ref 'Any}
mt (dt/q> :- [EntityID]
'[:find ?e
:in $ ?t-name
@fogus
fogus / core.clj
Last active December 20, 2015 09:09
(ns depr.core)
(defn ^:private !! [c]
(println "WARNING - Deprecation of " c " in effect."))
(defmacro defn-deprecated
[nom _ alt ds & arities]
(let [silence? (:silence-deprecations (meta clojure.core/*ns*))]
(when-not silence?
(!! alt)))
@ztellman
ztellman / gist:5603216
Last active May 26, 2019 17:08
an exploration of the memory implications of call-site caching for protocols
;; let's create a simple protocol that just returns a number
user> (defprotocol NumberP (number [_]))
NumberP
;; now we'll create an implementation that always returns '1'
user> (deftype One [] NumberP (number [_] 1))
user.One
;; unsurprisingly, this type only has a single static value, which wraps the '1'
> (-> One .getDeclaredFields seq)
@mtnygard
mtnygard / query.clj
Created February 8, 2013 22:19
Use Datomic queries as a source for RxJava pipes.
(ns rxjava-datomic.query
(:require [datomic.api :as d])
(:use [clojure.repl :only [pst]])
(:import [rx Observable]
datomic.Peer))
(defn query [qry & inputs]
(Observable/toObservable
(Peer/q qry (object-array inputs))))
@candera
candera / README.md
Last active February 13, 2024 10:04
Clojure config files

A little Clojure configuration reader

This is a handy bit of code I've written more than once. I thought I'd throw it in here so I can refer back to it later. Basically, it lets you read a config file to produce a Clojure map. The config files themselves can contain one or more forms, each of which can be either a map or a list. Maps are simply merged. Lists correspond to invocations of extension points, which in turn produces a map, which is merged along with everything else.

An Example

Consider the following files:

names.edn

@BekeJ
BekeJ / py2tex.py
Last active March 9, 2017 06:41
IPython notebook py2tex
"""
Module for IPython to display code with TeX representation.
This makes for example the following workflow possible:
.. sourcecode:: ipython
In [1]: %load_ext py2tex
In [2]: from math import *
@kachayev
kachayev / clojure-channels-1-generator.clj
Last active October 7, 2015 09:57
Channels-driven concurrency with Clojure
;; Channels-driven concurrency with Clojure
;; Clojure variant for code examples from this gist:
;; https://gist.github.com/3124594
;; Primarily taken from Rob Pike's talk on Google I/O 2012:
;; http://www.youtube.com/watch?v=f6kdp27TYZs&feature=youtu.be
;;
;; Concurrency is the key to designing high performance network services.
;; Clojure provides several concurrency primitives, like futures/promises, atom, agent etc.
;; There is no implementation for "Go channels" in core, but we can use
;; 3rd-party library Lamina to do the same things.
@kachayev
kachayev / go-channels-1-generator.go
Created July 16, 2012 19:42
Channels-driven concurrency with Go
// Channels-driven concurrency with Go
// Code examples from Rob Pike's talk on Google I/O 2012:
// http://www.youtube.com/watch?v=f6kdp27TYZs&feature=youtu.be
//
// Concurrency is the key to designing high performance network services.
// Go's concurrency primitives (goroutines and channels) provide a simple and efficient means
// of expressing concurrent execution. In this talk we see how tricky concurrency
// problems can be solved gracefully with simple Go code.
// (1) Generator: function that returns the channel