Skip to content

Instantly share code, notes, and snippets.

<?
# MIT license, do whatever you want with it
#
# This is my invoice.php page which I use to make invoices that customers want,
# with their address on it and which are easily printable. I love Stripe but
# their invoices and receipts were too wild for my customers on Remote OK
#
require_once(__DIR__.'/../vendor/autoload.php');
@MelkorNemesis
MelkorNemesis / clean-secret-from-url.js
Created July 25, 2022 20:02 — forked from DavidWells/clean-secret-from-url.js
Clean your access token from URL to guard against user accidentally copy + pasting url elsewhere
function removeAccessTokenFromUrl() {
const { history, location } = window
const { search } = location
if (search && search.indexOf('access_token') !== -1 && history && history.replaceState) {
// remove access_token from url
const cleanSearch = search.replace(/(\&|\?)access_token([_A-Za-z0-9=\.%]+)/g, '').replace(/^&/, '?')
// replace search params with clean params
const cleanURL = location.toString().replace(search, cleanSearch)
// use browser history API to clean the params
history.replaceState({}, '', cleanURL)
import fetch from "isomorphic-fetch";
function getUser(userId) {
return fetch(`https://jsonplaceholder.typicode.com/users/${userId}`)
.then((res) => {
if (!res.ok) {
throw new Error(res.statusText);
}
return res;
})
@MelkorNemesis
MelkorNemesis / global-variables-are-bad.js
Created November 23, 2021 12:40 — forked from hallettj/global-variables-are-bad.js
How and why to avoid global variables in JavaScript
// It is important to declare your variables.
(function() {
var foo = 'Hello, world!';
print(foo); //=> Hello, world!
})();
// Because if you don't, the become global variables.
(function() {
@MelkorNemesis
MelkorNemesis / combinators.js
Created February 9, 2021 19:07 — forked from Avaq/combinators.js
Common combinators in JavaScript
const I = x => x
const K = x => y => x
const A = f => x => f (x)
const T = x => f => f (x)
const W = f => x => f (x) (x)
const C = f => y => x => f (x) (y)
const B = f => g => x => f (g (x))
const S = f => g => x => f (x) (g (x))
const S_ = f => g => x => f (g (x)) (x)
const S2 = f => g => h => x => f (g (x)) (h (x))
@MelkorNemesis
MelkorNemesis / coContraFunctors.md
Created January 4, 2021 20:47 — forked from cscalfani/coContraFunctors.md
Covariant and Contravariant Functors

Covariant and Contravariant Functors

This article contains more than enough examples to hopefully understand Covariant and Contravariant Functors and how to create them for many situtations.

Note that all type annotations use Int as the concrete type but any concrete type would be equivalent as would any mix of concrete types.

It assumes a general understanding of Functor and a basic understanding of Haskell type classes and data types.

Mapping from a to b(#1)

@MelkorNemesis
MelkorNemesis / CompositionWithMultipleParameters.md
Created January 4, 2021 20:47 — forked from cscalfani/CompositionWithMultipleParameters.md
Functional Composition with Multiple Parameters in Haskell

Functional Composition with Multiple Parameters in Haskell

In the past, I've written composition functions in both Elm and Haskell that take multiple parameters for the leftmost function, i.e. the function that gets applied first.

(All examples here are in Haskell)

Here was my Haskell implemenation (stolen from the web):

compose2 :: (c -&gt; d) -&gt; (a -&gt; b -&gt; c) -&gt; a -&gt; b -&gt; d

Monads of Functions in Haskell

We're all pretty familiar with Monads that contain values, e.g. Maybe a and Either e a. Typically, these are value type, e.g. Maybe String, Maybe Int, Either String Int, Either MyError MyResult.

When fmap is called on a Maybe Int, it is clear that the Int is going to be mapped to its new value when the fmap is evaluated, e.g.:

fmap (* 10) Just 1 === Just 10
@MelkorNemesis
MelkorNemesis / DiscoveringApplicative.md
Created January 4, 2021 20:47 — forked from cscalfani/DiscoveringApplicative.md
Discovering Applicative Functors in Haskell

Discovering Applicative Functors in Haskell

When first learning Applicatives, they always seemed an oddball thing to me. At first, Functors made sense and Monads sort of made sense, but Applicatives felt shoehorned in between the two.

That is until I was reading Programming In Haskell by Graham Hutton, Second Edition (section 12.2) on Applicatives. His explanation takes a bit of a leap and is presented in an odd order, but I think I can reorder the information in such a way that we can feel as if we are discovering Applicative Functors (or Applicatives for short) ourselves.

Start with Functor

Let's remind ourselves of the Functor class definition:

(Compose) Compose (Compose)

Motivation

Multiple times I've wanted to understand how to derive the following by hand:

(.) . (.) :: (c -> d) -> (a -> b -> c) -> a -> b -> d

Which is the point free version of: