Skip to content

Instantly share code, notes, and snippets.

View stoeckley's full-sized avatar

andrew stoeckley

  • Balcony Studio
  • Netherlands
View GitHub Profile
@stoeckley
stoeckley / vector drag
Created July 30, 2017 17:37
Drag index through a vector
(defn slide
"Returns the final vector after dragging an indexed item to another index, which means it swaps incrementally with each index it passes through. v should be a vector but if it isn't it will be converted to one."
[v o n]
(let [v (vec v) ;; ensures v is a vector
item (v o)
removed (if (= o (count v))
(pop v)
`[~@(concat
(subvec v 0 o)
(subvec v (inc o)))])]
@stoeckley
stoeckley / bench.clj
Created July 30, 2017 17:37 — forked from hiredman/bench.clj
core.logic for joinery
#!/usr/bin/java -jar clojure-1.7.0-master-SNAPSHOT.jar
(let [pom-uber-jar
(str "http://thelibraryofcongress.s3.amazonaws.com/"
"pomegranate-0.0.13-SNAPSHOT-jar-with-dependencies.jar")
cl (java.net.URLClassLoader. (into-array [(java.net.URL. pom-uber-jar)]))
cx (.getContextClassLoader (Thread/currentThread))]
(push-thread-bindings {clojure.lang.Compiler/LOADER cl})
@stoeckley
stoeckley / fold-right.clj
Created July 30, 2017 17:37 — forked from kohyama/fold-right.clj
fold-right and reduce-right in Clojure
(use 'clojure.test)
(defn fold-right [f z coll]
(loop [[c & cs] coll rvsd '()]
(if (nil? c)
(loop [acc z [r & rs] rvsd]
(if r (recur (f r acc) rs) acc))
(recur cs (cons c rvsd)))))
(defn reduce-right [f coll]
;; based on core.logic 0.8-alpha2 or core.logic master branch
(ns sudoku
(:refer-clojure :exclude [==])
(:use clojure.core.logic))
(defn get-square [rows x y]
(for [x (range x (+ x 3))
y (range y (+ y 3))]
(get-in rows [x y])))
@stoeckley
stoeckley / drain
Created July 30, 2017 17:34
drain a core.async chan
(defn drain
"Takes all values available on a chan then returns the number taken to the channel drain creates. Compatible with cljs too."
[c]
(go
(loop [r 0]
(alt!
(timeout 100) r
c ([v] (recur (inc r)))))))
(defn drainf
@stoeckley
stoeckley / clojure-emacs.el
Created July 20, 2017 19:54
clojure setup in emacs
;; Clojure IDE and REPL for Emacs
(require-package 'cider)
;; autocompletion
(require-package 'company)
;; REPL related stuff
;; REPL history file
(setq cider-repl-history-file "~/.emacs.d/cider-history")
@stoeckley
stoeckley / elm-questions.md
Last active July 13, 2017 23:38
Questions about The Elm Architecture

Philosophies that good functional programmers from the Elm and Clojurescript communities should agree on:

  1. An application should have a single source of truth -- all your model data in one place.
  2. Your model data structure should not depend on your interface; a data model should be organized in whatever way is good for the sake of the data, only. View structure != model structure.
  3. View functions should receive only the data they need for building themselves. Whole model state should not be passed freely everywhere, as this gives most views much more information than they need; this would also make it difficult to build re-usable components, if view functions must also query deeply into a custom data store in addition to performing view logic. Beyond this, it creates a conceptual burden on developers when all views are handling whole model state, you cannot easily follow the flow of data access.
  4. Related to 3), a view should not inject a data dependency into its parent. That is, all the view nodes/fu
@stoeckley
stoeckley / destructuring.md
Created July 10, 2017 15:16 — forked from yang-wei/destructuring.md
Elm Destructuring (or Pattern Matching) cheatsheet

Should be work with 0.18

Destructuring(or pattern matching) is a way used to extract data from a data structure(tuple, list, record) that mirros the construction. Compare to other languages, Elm support much less destructuring but let's see what it got !

Tuple

myTuple = ("A", "B", "C")
myNestedTuple = ("A", "B", "C", ("X", "Y", "Z"))
@stoeckley
stoeckley / Main.elm
Created July 9, 2017 19:03 — forked from anonymous/Main.elm
Untitled
port module Main exposing (..)
import Html exposing (Html, text, program)
import Html.Events exposing (on)
import Html.Attributes exposing (type_, src)
import Json.Decode as Decode exposing (Value, Decoder)
port readImage : Value -> Cmd msg
@stoeckley
stoeckley / makeAnimatedGif.m
Created June 3, 2017 13:45 — forked from mayoff/makeAnimatedGif.m
Example of creating an animated GIF on iOS, with no 3rd-party code required. This should also be easy to port to OS X.
#import <UIKit/UIKit.h>
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
static UIImage *frameImage(CGSize size, CGFloat radians) {
UIGraphicsBeginImageContextWithOptions(size, YES, 1); {
[[UIColor whiteColor] setFill];
UIRectFill(CGRectInfinite);
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(gc, size.width / 2, size.height / 2);