Skip to content

Instantly share code, notes, and snippets.

@harmenjanssen
harmenjanssen / nearest-vowels.clj
Created May 5, 2020 07:17
nearest-vowels Clojure kata
(def vowels "aeiou")
(defn is-vowel [ch] (not (nil? (some #{ch} vowels))))
(defn abs [n] (max n (- n)))
(defn distance
"Returns the distance from the number x to the closest number in xs"
[xs x]
(apply min (map (fn [y] (abs (- y x))) xs)))
@harmenjanssen
harmenjanssen / index.html
Created May 27, 2019 06:14
Wrap JS-dependent elements in a `<template>`, and simply unwrap at runtime.
<!doctype html>
<title>Hello</title>
<p>The following search field depends on Javascript, so let's render it with Javascript:
<template data-autorender="true">
<form>
<input type="search">
</form>
</template>
@harmenjanssen
harmenjanssen / example1.php
Last active May 10, 2019 09:40
Quick example of how Maybe could work in PHP.
<?php
$record = $model->fetchById(42); // return Maybe(Record)
$related = $record->map(function ($record) {
// If this function is executed, record is definitely available, no need to check.
return $otherModel->fetchWhere('foreign_key', $record->id);
});
// It's still a Maybe after mapping, so you can continue mapping on the value.
// If the first fetchById call got you a Nothing, this is still the same
// Nothing, but your code doesn't need to know, it can be written
@harmenjanssen
harmenjanssen / .env.template
Created March 26, 2019 15:27
Create a redirect programmatically in a website-configured S3 bucket.
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
AWS_S3_BUCKET=
AWS_BUCKET_WEBSITE_URL=
@harmenjanssen
harmenjanssen / middle_char.php
Last active July 29, 2018 07:33
Recursive function for grabbing the middle character (or 2) of a string.
<?php
function middle_char(string $str): string {
return strlen($str) <= 2
? $str
: middle_char(substr($str, 1, -1));
}
@harmenjanssen
harmenjanssen / my_test.php
Created March 27, 2018 20:48
Use `@dataProvider` to quickly pass a bunch of invalid input through your function to make sure it fails correctly.
<?php
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase {
/**
* @dataProvider invalidArgumentProvider
* @expectedException InvalidArgumentException
*/
public function test_should_throw_on_invalid_arguments($arg) {
my_func($arg);
@harmenjanssen
harmenjanssen / app.js
Created September 20, 2017 20:21
Struggling with "carrying over" arguments in a function composition. In the example I would like the final `assoc` call in the composition to end up knowing about both the page's URL property and the menuItem object.
import { pages, menuItems } from "./data.js";
import { propEquals, prop, assoc, compose, map } from "./util.js";
const findPageById = id => pages.find(propEquals("id", id));
// Put a "url" property on the menuItems, taken from the related Page
console.log(
map(
compose(
assoc(
/**
* Ha! I totally fucked up. I noticed after publishing this, so instead of quietly updating this I will
* add a bit of educational text.
*
* The function below, curry_bodged, makes the mistake of saving the args in lexical scope, resulting in
* an ever-expanding list of arguments. Every time you call the curried function the arguments are added to
* the same list, and the actual applied function will probably re-use the same arguments from the first time.
*
* Example with the `addThree` function below:
* addThree(1, 2, 3) // 6, args = [1, 2, 3]
@harmenjanssen
harmenjanssen / helpers.js
Last active September 12, 2017 21:52
`prop` and `propIn` utility functions using the Maybe monad.
// maybe :: ?a -> Maybe a
export const maybe = x => {
return x != null && x !== undefined ? Just(x) : Nothing;
};
// identity :: a -> a
export const identity = x => x;
// prop :: String -> Object -> Maybe x
export const prop = x => o => maybe(o[x]);
@harmenjanssen
harmenjanssen / Plugin.php
Created August 30, 2017 14:44
Javascript fallback for broken default values in OctoberCMS' Repeater fields
<?php
class Plugin extends PluginBase
{
public function boot()
{
Event::listen('backend.page.beforeDisplay', function($controller, $action, $params) {
$controller->addJs('/plugins/grrr/flexiblepages/assets/javascript/repeater.js');
});
}
}