Skip to content

Instantly share code, notes, and snippets.

@laser
laser / b.js
Created December 31, 2014 18:26
b
var results = [],
i = fruitsOfTheWorld.length,
j;
while (i--) {
results.unshift([]);
var j = fruitsOfTheWorld[i].length;
while (j--) {
results[0].unshift(capitalize(fruitsOfTheWorld[i][j]));
}
@laser
laser / a.js
Created December 31, 2014 18:25
a
function capitalize(s) {
return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
}
var fruitsOfTheWorld = [
["apple", "pineapple", "pear"],
["manzana", "pera", "piña"],
["poma", "perera", "ananàs"]
];
@laser
laser / curried.md
Last active August 29, 2015 14:12
Gettin' Freaky Functional with Curried JavaScript

Gettin' Freaky Functional w/Curried JavaScript

Partial application refers to the practice of partially filling in a functions parameters with arguments, deferring others to be filled-in at a later time. JavaScript libraries like Underscore facilitate partial function application - but the API isn't for everyone. I, for one, feel icky sprinkling calls to _.partial and _.bind throughout my application.

Curried functions (found in Haskell and supported by Scala, among others) can be partially-applied with little ceremony; no special call format is required. In this blog post, I'll demonstrate an approach to currying JavaScript functions at the time of their definition in a way that enables partial function application without introducing lots of annoying parens, anonymous function expressions. This post is mostly for the lulz; I hope it stretches your mind, if nothing else.

Currying in JavaScript: Usually a Huge Pain

@laser
laser / original.js
Last active August 29, 2015 14:11
Cleaning up anonymous function expressions w/underscore
exports.index = function(req, res) {
async.waterfall([
//
// validate the search string
//
function(cb) {
// paramValidate protocol :
// url, sort ording, sorting field, searching field, valid attributes and callback
util.paramValidate(req.url, 'desc', 'id', Models.City.attributes, function (err, params) {
cb(err, params);
@laser
laser / database-pipeline.clj
Last active August 29, 2015 14:11
Adding database to pipeline
(defn internal-error
"Generate an HTTP response map representing an Internal Server Error."
[body]
(assoc (response body) :status 500))
(defn post-notes-handler
"Map an HTTP request to a newly-created note."
[req-body]
(let [[note error] (validate-note req-body)]
(if error
@laser
laser / database-connectivity.clj
Last active August 29, 2015 14:11
Adding database connectivity
(def db-config "jdbc:h2:mem:pipeline;DB_CLOSE_DELAY=-1")
(defn create-note
"Given a note's text, insert into database and return autogenerated id"
[{text :text}]
(try [(-> (query :notes)
(insert {:text text})
(exec db-config)
first
vals
@laser
laser / validation-in-pipeline.clj
Last active August 29, 2015 14:11
Validation-in-pipeline
(defn bad-request
"Generate an HTTP response map representing a Bad Request."
[body]
(assoc (response body) :status 400))
(defn ok
"Generate an HTTP response map representing a successful request."
[body]
(assoc (response body) :status 200))
@laser
laser / composing-with-err.clj
Created December 14, 2014 23:08
Composing validators with err->>
(defn validate-note
"Ensure that the provided note is valid."
[note]
(err->> note
ensure-text-presence
ensure-text-length))
@laser
laser / validator-composition.clj
Created December 14, 2014 23:08
Validator composition
(defn validate-note
"Ensure that the provided note is valid."
[note]
(let [[note error] (ensure-text-presence note)
[note error] (if (nil? error) (ensure-text-length note) [nil error])]))
@laser
laser / validation.clj
Last active August 29, 2015 14:11
Adding validation
(defn ensure-text-presence
[params]
"Ensure (body :text) is present"
(if (empty? (params :text))
[nil, "Please provide text for your note"]
[params]))
(defn ensure-text-length
"Ensure that the length of the text is between 1 and 140 characters"
[params]