Skip to content

Instantly share code, notes, and snippets.

View danprince's full-sized avatar

Dan Prince danprince

View GitHub Profile
@danprince
danprince / concerns.md
Last active August 24, 2016 19:59
Redux, Aphrodite & Zaphod

Concerns

React Dev Tools

Any wrapped components will be rendered into the React dev tools with their wrappers intact. This becomes pretty invasive when most of your components are using a HOC wrapper.

Here's a workaround for defining HOCs that respect stateless functional components and don't render a wrapper.

function withName(Component) {
 class EnhancedComponent extends React.Component {
@danprince
danprince / modal.js
Created August 9, 2016 09:18
Function-as-child Modals
class Modal extends Component {
constructor() {
super();
this.state = { showing: true };
this.close = this.close.bind(this);
}
close() {
this.setState({ showing: false });
}
render() {
@danprince
danprince / .gitignore
Last active July 13, 2016 13:29
WebTask URL Shortner
node_modules
_app.js
*.log
@danprince
danprince / flatten.js
Last active July 8, 2016 08:12
Flatten an array of arbitrarily nested elements.
// Flattens an array of arbitrarily nested elements.
// E.g. [[1,2,[3]],4] -> [1,2,3,4]
//
// - Doesn't work with negative array indices!
// - ES6 TCO requires recursion to stay in tail-call position.
function flatten(xs) {2
if(!Array.isArray(xs)) {
return xs;
}
@danprince
danprince / curry.js
Created May 23, 2016 18:41
Recursive Currying Into Unary Functions
function curry(func) {
const arity = func.length;
function recurry(args=[]) {
if(args.length == arity) {
return func(...args);
} else {
return arg => recurry([...args, arg]);
}
}
@danprince
danprince / README.md
Last active April 29, 2016 13:32
Redux as a Service

Redux as a Service

Hacked together example of usage of Redux as a predictable in memory db with a node http frontend.

Usage

Start the server:

node server.js
@danprince
danprince / article.md
Last active April 24, 2016 22:43
Academic Pandoc

There's a hypothetical paradise where we write all of our documents with markdown and this is well on the way to being a reality with [readmes][1], [websites][2], [wikis][3] and [documentation][4] but in academic writing, LaTeX reigns supreme.

Alone, markdown isn't much of a competitor. It can't do references, it doesn't understand chapters, it can't generate page numbers, you have to manage tables of contents manually, there's no support for captions or automatic numbering of figures and listings. Markdown wasn't designed with these kinds of complex features in mind --- and rightly so.

Maybe I've just been spoiled by the expressive nature of markdown but I don't enjoy writing LaTeX.

There's a project called [Pandoc][9], which allows you to create convert from one document type to another. We can use pandoc to convert markdown into PDFs and HTML.

If you need to convert files from one markup format into another, pandoc is your swiss-army knife.

@danprince
danprince / painter.cljs
Last active March 14, 2016 14:18
Immutable, undoable pixel editor
(def height 10)
(def width 10)
(def pixel-size 10)
(defn ->2D [i]
[(mod i width)
(.floor js/Math (/ i width))])
(def coords (map ->2D, (range (* height width))))
(def colors (repeat "#fff"))
@danprince
danprince / examples.md
Last active February 4, 2016 10:20
React Exercises

React Examples

A continuously updating clock component that shows the time and date.

@danprince
danprince / stacktrace.log
Created January 6, 2016 15:03
Analysis Error Stack Trace
Exception in thread "main" clojure.lang.ExceptionInfo: failed compiling file:src/game/ui/widgets.cljs {:file #object[java.io.File 0x18cc679e "src/game/ui/widgets.cljs"]}, compiling:(/tmp/lispjam/build.clj:3:1)
at clojure.lang.Compiler.load(Compiler.java:7239)
at clojure.lang.Compiler.loadFile(Compiler.java:7165)
at clojure.main$load_script.invoke(main.clj:275)
at clojure.main$script_opt.invoke(main.clj:337)
at clojure.main$main.doInvoke(main.clj:421)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.lang.Var.invoke(Var.java:379)
at clojure.lang.AFn.applyToHelper(AFn.java:154)
at clojure.lang.Var.applyTo(Var.java:700)