Skip to content

Instantly share code, notes, and snippets.

View raymcdermott's full-sized avatar

Ray McDermott raymcdermott

  • OpenGrail
  • Belgium
View GitHub Profile
@raymcdermott
raymcdermott / core.async filtering by time
Last active June 6, 2018 14:12
core.async filtering by time
(ns green-eggs.core
(:require [clojure.core.async
:as a
:refer [>! <! >!! <!! go chan buffer close! thread
alts! alts!! timeout onto-chan]]
[clj-time.core :as t]
[clj-time.coerce :refer [from-date]]))
;If you were to implement yourself, you could make a `go` that simply pulls from a channel, and adds to another as a
;`[timestamp item]` pair, then finally pushes into an unbounded `chan` that has a transducer that filters based on
@raymcdermott
raymcdermott / datomic-component-crud.clj
Last active December 7, 2015 21:13
CRUD on component entities ... more support than comes out of the box with Datomic
(def component-crud
(d/function
'{:lang "clojure"
:params [db entity comp-key]
:code (if-let [db-entity (d/pull db '[*] (:db/id entity))]
(let [db-comps (comp-key db-entity)
user-comps (comp-key entity)
s1 (into #{} (map :db/id db-comps))
s2 (into #{} (map :db/id user-comps))
diffs (clojure.set/difference s1 s2)
@raymcdermott
raymcdermott / core.clj
Last active September 23, 2015 23:11
Minimal configurator
(ns car-configurator.core
(:require [schema.core :as s]))
; NO CODE YET ... Just data
;---> define some basic attributes
(def fuel (s/enum :diesel :petrol :hybrid))
(def engine {:name s/Str :fuel fuel :volume s/Num :bhp s/Int}) ; c02, etc.
(def colour {:name s/Str :rgb {:red s/Int :green s/Int :blue s/Int} :hex s/Int}) ; metallic, etc.
; STILL NO CODE YET ... Just data
@raymcdermott
raymcdermott / gist:c961c3dd37d425af9c35
Created March 30, 2015 14:28
Run a deployment from Bamboo to Heroku with independence from locally installed tools
#!/usr/bin/env bash
############ CONFIGURE GIT from this repo
git config user.email "bamboo@company.com"
git config user.name "Bamboo Dev"
# Add a remote master to Heroku (if one does not exist)
(git remote -v | grep "^heroku" > /dev/null) ||
heroku git:remote --app $HEROKU_APP_NAME --org $HEROKU_ORG_NAME
@raymcdermott
raymcdermott / gist:f4ec1166bf48d02fb68e
Created February 1, 2015 21:39
request-promise fn composition
var express = require('express'),
reqpromise = require('request-promise');
function register(req, res, next) {
var user = {};
user.username = req.body.username;
user.password = req.body.password;
user.firstName = req.body.firstName;
user.lastName = req.body.lastName;
user.email = req.body.username;
@raymcdermott
raymcdermott / gist:908753
Created April 7, 2011 21:21
recursive java
public String scrubOut(String line, Collection<String> keys) {
if (keys.isEmpty()) return line;
else {
String key = keys.iterator().next();
keys.remove(key);
return scrubOut(line.replaceAll(key + "=\".*?\"", key + "=\"****\""), keys);
}
}
@raymcdermott
raymcdermott / gist:908742
Created April 7, 2011 21:15
simple recursion
def scrubOut(line: String, keys: Seq[String]): String = {
// Optimizations... most likely to the left
if (!line.contains("=") || !line.contains("\"") || keys.isEmpty || containsKeys(false, line, keys) == false)
line
else
scrubOut(line.replaceAll(keys.head + "=\".*?\"", keys.head + "=\"****\""), keys.tail)
}
@raymcdermott
raymcdermott / gist:908731
Created April 7, 2011 21:11
Recursion with locally recursive function with default and named arguments
def scrubOut(line: String, keys: List[String]): String = {
def containsKeys(found: Boolean = false, local_line: String = line, local_keys: List[String] = keys): Boolean = {
if (local_keys.isEmpty || found) found
else {
containsKeys(line.contains(local_keys.head), local_line, local_keys.tail)
}
}
if (!line.contains("=") || !line.contains("\"") || keys.isEmpty || containsKeys() == false)
@raymcdermott
raymcdermott / gist:824010
Created February 12, 2011 19:09
\ is an alias for foldLeft and / is an alias for foldRight
def getTotalItemFees = {
items.\(0.0d)((accum, item) => accum + (item._1.getFee * item._2)
}
@raymcdermott
raymcdermott / gist:823995
Created February 12, 2011 19:02
High order function, pure functional style
def getTotalItemFees = {
items.foldLeft(0.0d)((accum, item) => accum + (item._1.getFee * item._2)
}