Skip to content

Instantly share code, notes, and snippets.

@MelkorNemesis
MelkorNemesis / ThinkAboutMonads.md
Created January 4, 2021 20:46 — forked from cscalfani/ThinkAboutMonads.md
How to think about monads

How to think about Monads

Initially, Monads are the biggest, scariest thing about Functional Programming and especially Haskell. I've used monads for quite some time now, but I didn't have a very good model for what they really are. I read Philip Wadler's paper Monads for functional programming and I still didnt quite see the pattern.

It wasn't until I read the blog post You Could Have Invented Monads! (And Maybe You Already Have.) that I started to see things more clearly.

This is a distillation of those works and most likely an oversimplification in an attempt to make things easier to understand. Nuance can come later. What we need when first learning something is a simple, if inaccurate, model.

This document assumes a beginner's knowledge of pure functional programming and Haskell with some brief encounters of Monads, e.g. [Functors, Applicatives, And

@MelkorNemesis
MelkorNemesis / FuncApplicationHaskell.md
Created January 4, 2021 20:46 — forked from cscalfani/FuncApplicationHaskell.md
Fun with Function Application (Haskell version)

Fun with Function Application (Haskell version)

Function application operators in Haskell make programming more understandable and help reduce parenthesis.

($)

The application operator, $, can be used to reduce the need for parenthesis. The following:

How to Think About Monads 2

How to Think about Monads, starts with composition of pure functions and quickly transitioned to composition of pure functions with side-effects. If you haven't read it yet, please do. This continues where that left off.

By the end of that article, we created specialized composition functions that were equivalent to the Monadic bind.

From that article we can conclude that Monadic computations have the following properties:

  1. Functions are executed sequentially
  2. Optionally, control-flow can be short-circuited

Monoids in Haskell, an Introduction

Why should programmers care about Monoids? Because Monoids are a common pattern that shows up over and over in programming. And when patterns show up, we can abstract them and leverage work we've done in the past. This allows us to quickly develop solutions on top of proven, stable code.

Add Commutative Property to a Monoid (Commutative Monoid) and you have something that can be executed in parallel. With the end of Moore's Law, parallelism is our only hope to increasing processing speeds.

What follows is what I've learned after studying Monoids. It is hardly complete, but hopefully will prove to be helpful as an introduction for others.

Monoid Lineage

import {curry} from 'lodash/fp';
/**
* lift2 (apply) for Functions.
*
* @see http://www.tomharding.me/2017/04/15/functions-as-functors/
*
* Type signature:
* (a -> b -> c) -> (x -> a) -> (x -> b) -> (x -> c)
*
@MelkorNemesis
MelkorNemesis / socket-cheatsheet.js
Created July 26, 2020 06:04 — forked from alexpchin/socket-cheatsheet.js
A quick cheatsheet for socket.io
// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
@MelkorNemesis
MelkorNemesis / mapDispatchToProps.md
Created September 19, 2018 07:06 — forked from heygrady/mapDispatchToProps.md
Redux containers: mapDispatchToProps

Redux containers: mapDispatchToProps

This document details some tips and tricks for creating redux containers. Specifically, this document is looking at the mapDispatchToProps argument of the connect function from [react-redux][react-redux]. There are many ways to write the same thing in redux. This gist covers the various forms that mapDispatchToProps can take.

@MelkorNemesis
MelkorNemesis / index.html
Created July 26, 2018 09:45 — forked from wesbos/index.html
ios 11 compatible getUserMedia
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
@MelkorNemesis
MelkorNemesis / enzyme_render_diffs.md
Created July 16, 2018 09:22 — forked from fokusferit/enzyme_render_diffs.md
Difference between Shallow, Mount and render of Enzyme

Shallow

Real unit test (isolation, no children render)

Simple shallow

Calls:

  • constructor
  • render