Skip to content

Instantly share code, notes, and snippets.

View davidchambers's full-sized avatar

David Chambers davidchambers

View GitHub Profile
#!/usr/bin/env bash
set -o nounset
# JSON Web Tokens use RFC 4648 § 5 (https://www.rfc-editor.org/rfc/rfc4648#section-5).
# This "base64url" encoding differs from "base64" in a few small ways, handled below.
curl --silent --request POST --url "$URL" \
--data-urlencode grant_type=client_credentials \
--data-urlencode client_id="$CLIENT_ID" \
--data-urlencode client_secret="$CLIENT_SECRET" |
@davidchambers
davidchambers / ListLike.js
Created April 25, 2024 10:30
Goal: Write a function for testing any given “module” that provides a specific set of functions for creating and manipulating lists. This is trivial in JavaScript but tricky in Kotlin due to the lack of higher-kinded types.
import {deepStrictEqual as eq} from "node:assert";
// class ListLike f where
// empty :: f a
// cons :: a -> f a -> f a
// fold :: f a -> b -> (b -> a -> b) -> b
// map :: f a -> (a -> b) -> f b
// length :: f a -> Int
const Array = {
import S from 'sanctuary';
import Identity from 'sanctuary-identity';
// lens :: (s -> a) -> (a -> s -> s) -> Lens s a
const lens = getter => setter => f => s => (
S.map (v => setter (v) (s))
(f (getter (s)))
);
//# zip :: Array a -> Array a -> Array a
//.
//. Zip two arrays, xs and ys, of arbitrary lengths.
//.
//. let n = Math.min (xs.length, ys.length);
//.
//. If the first array is longer than the second, the result will
//. consist of the first n + 1 elements of the first array and
//. all n elements of the second array. Otherwise, the result will
//. consist of the first n elements of each array.
//# groupBy :: (a -> b) -> Array a -> Array (Array a)
//.
//. Group the given elements in accordance with the given comparator.
//.
//. > groupBy (x => x % 3) ([1, 2, 3, 4, 5, 6, 7, 8, 9])
//. [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
const groupBy = f => xs => {
const groups = [];
const ys = [];
xs.forEach (x => {
'use strict';
const Future = require ('fluture');
const S = require ('sanctuary');
// data Action = Reset | Increment Integer | Decrement Integer
// Reset :: Action
const Reset = {tagName: 'Reset'};
@davidchambers
davidchambers / dataTransform.js
Created November 14, 2017 20:02 — forked from josete89/dataTransform.js
Data mapping with sanctuary
'use strict';
const R = require('ramda');
const S = require('sanctuary');
const data = {
id: 1,
orderName: 'Order from spain',
teamName: 'Portland penguins',
// This file demonstrates a simpler approach to the problem posed in
// <https://gist.github.com/kurtmilam/ff453e13225580f098d1f64f4bc3e9c7>.
//
// Note that it's possible to define a parametrically polymorphic `move`
// function by using a `Shape s` constraint. On the other hand it is not
// possible to define a parametrically polymorphic `area` function since
// circles and rectangles require different logic.
//
// One could solve this problem by defining a type representative for each
// shape type, which would provide a suitable `area` function (`Z.of` takes

Challenge

Write a function of type String -> Integer. The input may or may not be a valid JSON string. If it is valid, the resulting JavaScript value is expected to be an object, but may not be. If it is an object, it is expected to have a foo property whose value is expected to be an object, but may not be. This value is expected to have a bar property which is expected to be an object with a baz property whose value is expected to be an array of strings. Each of these strings is expected to be a hex representation of an integer (e.g. 0xFF). If every element of the array meets this expectation, the

function drop(n) {
s = ""
for (i = n + 1; i <= NF; i += 1) { s = s (s == "" ? "" : " ") $i }
return s
}
/^ *\/\/\. ##/ {
indent = substr($2, 2) # reduce indentation by one level
gsub(/#/, " ", indent)
href = tolower(drop(2))