Skip to content

Instantly share code, notes, and snippets.

@FLamparski
FLamparski / FakeLoops.kt
Last active January 2, 2016 21:10
Let's reimplement while and for loops in Kotlin with its omit-parens-around-last-function-argument syntax
fun wwhile (cond: () -> Boolean, body: () -> Unit) {
if (cond()) {
body()
wwhile(cond, body)
}
}
fun <T> ffor(initialise: () -> T, check: (T) -> Boolean, step: (T) -> T, body: (T) -> Unit) {
var loopvar = initialise()
wwhile({check(loopvar)}) {
>>> function coerc2($optional = null) { printf("Optional is set? %b, Is coercable to true? %b\n", isset($optional), (bool) $optional); }
=> null
>>> coerc2()
Optional is set? 0, Is coercable to true? 0
=> null
>>> coerc2(1)
Optional is set? 1, Is coercable to true? 1
=> null
>>> coerc2(2)
Optional is set? 1, Is coercable to true? 1
@FLamparski
FLamparski / compose.php
Created December 15, 2015 13:40
A semi-workable implementation of functional composition in PHP.
<?php
/*
This function takes in a bunch of callables (closures, functions, strings naming functions, etc.)
and returns a function such that compose(f, g)(x) <=> f(g(x)). In other words, the return function
will take exactly one argument, and call the functions supplied to compose right to left
on that argument.
*/
function compose() {
$functions = func_get_args();
return (function() use ($functions) {
@FLamparski
FLamparski / index.js
Created November 26, 2015 10:34
requirebin sketch
var yaml = require('yamljs')
var _ = require('lodash')
function print(s) {
var p = document.createElement('p')
p.appendChild(document.createTextNode(s))
document.body.appendChild(p)
}
var o = {a: 1, b: [{a: 2}, {c: 3}], x: 'lol what'}
@FLamparski
FLamparski / ugly.md
Last active October 25, 2015 17:02
A bookmarklet to un-uglify default styles for text-heavy but style-light web pages

Loads of HTML out there is unstyled, so you get a whole lot of 1990 in your browser. If you prefer it to be prettier, try this:

(function(){
  var s = document.body.style;
  s.maxWidth='660px';
  s.margin='8px auto';
  s.fontFamily='Source Sans Pro'; // adjust to your liking
 s.fontSize='16px';
@FLamparski
FLamparski / Main.partial.java
Created September 19, 2015 17:20
"Partial classes" in Java
public class Main {
#include "main2.java"
public static void main(String[] args) {
frobozz();
}
}
@FLamparski
FLamparski / first_class_fns.php
Created September 16, 2015 08:48
In PHP, functions "are" "first" "class" "objects"
// Psy Shell v0.4.4 (PHP 5.6.4-4ubuntu6.2 — cli) by Justin Hileman
>>> function foo($x) { return $x * 2; }
=> null
>>> foo(5)
=> 10
>>> is_callable(foo)
PHP error: Use of undefined constant foo - assumed 'foo' on line 1
>>> is_callable('foo')
=> true
>>> 'foo'(5)
@FLamparski
FLamparski / index.es6.html
Created April 16, 2015 18:11
Dynamically changing prototypes in Js
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Brian Blessed demo</title>
<style type="text/css">
.out {
font-weight: bold;
}
body {
@FLamparski
FLamparski / gist:072f43cab3bab669c016
Created April 16, 2015 09:25
A function to serialise an object to JSON which also tries to pull in non-enumerable properties. Example usage: turning exceptions to JSON.
function serialiseAll(object) {
var properties = Object.getOwnPropertyNames(object);
var plainObject = _.reduce(properties, function(obj, prop) {
obj[prop] = object[prop];
return obj;
}, {});
return JSON.stringify(plainObject);
}
@FLamparski
FLamparski / COPYING
Last active August 29, 2015 14:17
A CSS3 spinnin' loader.
The MIT License (MIT)
Copyright (c) 2015 Filip Wieland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: