Skip to content

Instantly share code, notes, and snippets.

View pepe's full-sized avatar

Josef Pospíšil pepe

View GitHub Profile
@jamesmacaulay
jamesmacaulay / debounce.clj
Last active January 25, 2016 09:29
debounce with core.async
(defn debounce
([input ms] (debounce (chan) input ms))
([output input ms]
(go
(loop [val (<! input)]
(let [t (timeout ms)
[next-val port] (alts! [input t])]
(if (= port t)
(do
(>! output val)
@mattdenner
mattdenner / gist:dd4cfde3f355ff6b7be8
Last active May 18, 2016 18:52
From imperative to functional: functors, applicatives, monads & other link bait

I’ve been writing a load of Swift code recently for work and this has lead me into the world of typed functional programming. The app needs to build certain objects from a comma separated string, and this lead me to applicative functors, which lead me to brain ache but enlightenment. So here’s my thoughts on how I got to understand these a little better.

All of the code is in Swift, so less clean than Haskell. I’m also only a about 6 weeks into Swift development so I probably haven’t got all of the idioms right. I’ve avoided the optional shorthand wherever possible here, preferring Optional<Type> over Type? because I believe the latter is hiding something that helps understand this code in the context of other generic classes.

It’s also long! I think it’s probably the longest blog post I’ve ever written but I found it interesting and useful, for myself, to write. If you’re one of those people who skip to the end of a book to find out whodunit then I’ve included

@vikeri
vikeri / debug-re-frame-subs.clj
Last active September 3, 2016 15:35
re-frame debug sub macro
;; A macro that replaces reg-sub, it inserts debug print statements into reg-sub if a flag of choice is set
;; Developed since 0.8.0 does not work with re-frame-tracer
;; Use it by replacing re-frame.core/reg-sub with this macro
(defmacro reg-sub [label & sub]
(let [fun (last sub)
arg (get-in (vec fun) [1 1 1])
[before after] (split-at 2 fun)
new-fun (concat before [`(js/console.log "Sub: " [~label ~arg])] after)]
(if (= "true" env/PRODUCTION) ;; Or whatever way you want to disable this, may also be done by replacing js/console.log above
@mtnygard
mtnygard / generator.clj
Created March 2, 2017 17:29
Simulating foreign exchange rates as Brownian motion processes
;; Caution: do not attempt to print this out. It contains many
;; lazy infinite sequences.
(def rate-table
(atom
(into {}
(map (fn [[ccypair rate]]
[ccypair (rand/brownian rate 0.0005)]))
rates/rate-table)))
(defn exchange-rate
@jdubie
jdubie / macros.clj
Last active March 5, 2017 12:30
ClojureScript macros.
;; nice for reagent
(defmacro defc
"syntactic sugar for declaring stateful components. Keeps signature of inner
and outer params consistent.
Before:
(defn stateful-component
[params]
(let [local state]
@aphyr
aphyr / structure.clj
Created April 16, 2017 21:12
Extract a schema from a clojure object
(require '[clojure.walk :as w])
(defmacro both [a b pred] `(and (~pred ~a) (~pred ~b)))
(defn union-structure [a b]
(cond (= a b) a
(both a b vector?) [(reduce union-structure (concat a b))]
(both a b map?) (merge-with union-structure a b)
; Dunno, said the ghoul
@burn2delete
burn2delete / spec.cljs
Last active June 7, 2017 10:20
Use data to validate data.
;; Basic Specs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(spec/def ::fn fn?)
(spec/def ::any any?)
(spec/def ::map map?)
(spec/def ::nil nil?)
(spec/def ::set set?)
@botanicus
botanicus / gh_open.zsh
Created November 16, 2010 14:45
ZSH: gh_open for opening current repo's page in browser.
# Open GitHub page for the current repository.
#
# Example:
# $ git clone https://github.com/majek/puka.git
# $ cd puka
# $ gh_open # Browser: https://github.com/majek/puka
function gh_open {
open $(git config remote.origin.url | ruby -ne 'puts "https://" + $_.split(%r{[/:@]})[1..-1].join("/").sub(/\.git$/, "")')
}
@marshallswain
marshallswain / feathersjs-auth0.js
Created June 4, 2016 20:49 — forked from theevangelista/feathersjs-auth0.js
A hook to populate an user from Auth0 into feathersjs
const request = require('request-promise');
const errors = require('feathers-errors');
const options = {
idField: 'sub',
issuer: 'iss'
};
module.exports = function() {
@rarous
rarous / orig.clj
Last active November 27, 2017 09:38
Refactoring of Tic-Tac-Toe in Clojure
(ns piskvorky.core
(:require [clojure.string :as s])
(:gen-class))
(defn usage []
(println "Ahoj v piskvorkach naslepo.\nPovolene prikazy jsou:\nnew - nova hra\nquit - konec\n[a-i][0-9] - tah na pole, kde rada je pozice a, b, c, d, e, f, g, h, i. Sloupec je 1 ... az 9.\nformat zapisu je napr. e5\nZacina x"))
(defn make-board []
(vec (repeat 9 (vec (repeat 9 :nothing)))))