Skip to content

Instantly share code, notes, and snippets.

Avatar

David Chambers davidchambers

View GitHub Profile
View zip.js
//# 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.
View groupBy.js
//# 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 => {
View adt.js
'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 Nov 14, 2017 — forked from josete89/dataTransform.js
Data mapping with sanctuary
View dataTransform.js
'use strict';
const R = require('ramda');
const S = require('sanctuary');
const data = {
id: 1,
orderName: 'Order from spain',
teamName: 'Portland penguins',
View shapes.js
// 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
View challenge.md

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

View toc.awk
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))
@davidchambers
davidchambers / comment.md
Created Jun 2, 2016
Quick introduction to chaining monads from a pull request review
View comment.md
var convertPercentage = function(percentage) {
  if (percentage == null) {
    return null;
  } else {
    return parseFloat(percentage.replace(/[^-\d.]/g, ''));
  }
};
View Main.hs
data Action = Coin | Turn deriving Show
data Locked = Locked | Unlocked deriving Show
data State = State { locked :: Locked, coins :: Int, gumballs :: Int } deriving Show
update :: Action -> State -> State
update Coin s@(State { locked = Locked, gumballs = g }) | g == 0 = s
update Coin s@(State { locked = Locked, coins = c }) = s { locked = Unlocked, coins = c + 1 }
update Turn s@(State { locked = Unlocked, gumballs = g }) = s { locked = Locked, gumballs = g - 1 }
update _ s = s
@davidchambers
davidchambers / io.js
Last active Sep 26, 2015
Sketch of possible (synchronous) IO and AsyncIO types in JavaScript
View io.js
'use strict';
const fs = require('fs');
const R = require('ramda');
const S = require('sanctuary');
// unit :: ()
const unit = void 0;