Skip to content

Instantly share code, notes, and snippets.

View apaleslimghost's full-sized avatar
💚
a pale slim ghost

Kara Brightwell apaleslimghost

💚
a pale slim ghost
View GitHub Profile
@apaleslimghost
apaleslimghost / go.ls
Last active October 7, 2015 02:28
Cute one-line alternative to function pipe chaining in LiveScript
Object::go = (...funcs)->fold (|>), this, funcs
[1 to 10].go do
filter odd
map (^ 2)
fold1 (+)
#=>165
#compare:
[1 to 10]
@apaleslimghost
apaleslimghost / holding.ls
Last active December 15, 2015 00:09
Simulate import•io's backend in holding mode.
require \http .create-server (req,res)->
status =
| req.url is '/' => 200
| otherwise => 503
res.write-head status,"Service Unavailable" {
"Content-Type": "application/json; charset=UTF-8"
"Connection": "close"
"X-Powered-By": "PHP/5.3.10-1ubuntu3.4"
"Server": "Apache/2.2.22 (Ubuntu)"
@apaleslimghost
apaleslimghost / borked-mlt.ls
Created March 18, 2013 10:33
Simulate an import.io backend where the ES morelikethis 404s.
fs = require \fs
http = require \http
global.options = JSON.parse fs.read-file-sync "importio-webui/options.json" \utf8
console.log options
http.create-server (req,res)->
if req.url == /_mlt$/
res.write-head 404 "Not Found"
# lol jetty 404
res.end """
@apaleslimghost
apaleslimghost / timeout.ls
Created March 22, 2013 12:38
an import.io backend proxy that sometimes takes 51 seconds to respond
http = require \http
http.create-server (req,res)->
opts = {
host: "api.import.io"
port: 80
req.method
path: req.url
req.headers
}
req.pipe http.request opts, (response)->
curl -XPOST 'http://importio.uservoice.com/api/v1/tickets.json?client=83JT0Uy7WDJAUv4aHiohQw' -d '{"email":"matt.brennan@import.io","ticket":{"subject":"TEST TICKET IGNORE ME","message":"lorem","attachments":[{"name":"test.txt","content_type":"text/plain","data":"aGVsbG8gd29ybGQ="}]}}' -H "Content-Type: application/json"
@apaleslimghost
apaleslimghost / co.js
Last active December 18, 2015 04:09
visionmedia/co + curried functions
const co = require('co'),
fs = require('fs'),
curry = require('prelude-ls').curry;
var read = curry(fs.readFile());
co(function* () {
var a = yield read('.gitignore','utf8');
var b = yield read('Makefile','utf8');
var c = yield read('package.json','utf8');
# semigroup
Readable::concat = (b)->
a = this
new class extends Readable
->
super ...
@read-from = a
_read: (size)->
Readable.of = (body)->
new class extends Readable
offset: 0
_read: (size)->
@push body.slice @offset,@offset+size-1
@offset += size-1
if @offset > body.length then return @push null # end the Readable
Readable::chain = (f)->
orig = this
new class extends Readable
_read: (size)->
if (orig.read size)?
if ((f that)read size)?
@push that
else @push ""
else
# applicative derived from monad
Readable::ap = (m)->
@chain (f)->
m.map f
# functor derived from monad
Readable::map = (f)->
@chain (a)~>
Readable.of f a