Skip to content

Instantly share code, notes, and snippets.

@donut
donut / donut_react.ml
Last active August 7, 2019 17:53
An alternative to `React.useReducer` that supports middleware with side effects.
type ('action, 'state) middleware
= dispatch:('action -> unit)
-> 'state
-> 'action
-> [ `Rerun of 'action | `Next of 'action | `Stop of 'action ]
let apply_middleware middleware dispatch state action =
let rec apply action = function
@donut
donut / client_react.ml
Last active June 10, 2019 18:43
A wrapper for React's useReducer() hook that applies middleware. Written in OCaml.
let use_reducer ?(middleware=[]) reducer initial =
(* [middlewared_dispatch] is setup as a reference so we can change it to the
dispatch function returned by [useReducer]. This way we can insert
middleware before the real reducer function runs. *)
let middlewared_dispatch = React.useRef (fun a ->
Js.Console.error2 "Dispatch called before reducer hook initialized." a)
in
let apply_middleware middleware state action =
let dispatch = React.Ref.current middlewared_dispatch in

From:

https://reknew.org/2018/08/part-8-of-15-race-and-social-hierarchies/

Well, “back in the day” we had three television Networks, and it was in the interest of all of them to report the News with as little bias as possible to attract the widest possible audience. With the advent of Cable News, however, people are able to watch the filtered version of the News that they agree with and that therefore activates the pleasure centers of their brain. And when liberal and conservative minded people no longer have to try to see the world through each other’s eyes, they get hardened in their perspectives. In time, they lose the willingness, and then the ability, to understand the perspectives of those who fundamentally disagree with their deeply held beliefs. Those who oppose them, therefore, can’t possibly be doing so on rational or moral grounds, which means they must either be stupid or immoral. They therefore cannot be reasoned with. They must simply be defeated.

If someone has a practical solution as to

@donut
donut / calc_digits.ml
Last active June 22, 2023 15:57
Times different methods for calculating the number of digits of an integer.
#load "unix.cma";;
open Printf;;
let by_string x =
x |> string_of_int |> String.length
;;
let rec by_recursion = function
| 0 -> 1
| x -> 1 + by_recursion (x / 10)
@donut
donut / jbuild
Created October 30, 2017 21:42
Demonstrates "Commands out of sync" error with ocaml-mariadb and let
(jbuild_version 1)
(executable
((name main)
(libraries (core cohttp.lwt mariadb))))

Keybase proof

I hereby claim:

  • I am donut on github.
  • I am donut2d (https://keybase.io/donut2d) on keybase.
  • I have a public key ASC7Ca_uC20tY6gxRMv5jLjQq4AirMKa-6bHWB6W-kXPXgo

To claim this, I am signing this object:

@donut
donut / Dictionary.swift
Created March 9, 2016 19:34
Adds Dictionary.mapValues method
import Foundation
extension Dictionary {
func mapValues<T>(transform: Value->T) -> Dictionary<Key,T> {
// Adapted from http://stackoverflow.com/a/29460871/134014
var dict = [Key:T]()
for (key, value) in zip(self.keys, self.values.map(transform)) {
dict[key] = value
}
###*
* @file
* Provides support for classes to have event systems.
* @url https://gist.github.com/donut/e4268b600bf72a8290e8
###
class EventsMixin
constructor: ->
@_event_handlers = {}
###*
* @file
* Provides mixin support for classes.
* @url https://gist.github.com/donut/00e407abb7cece0bcdfa
* @see http://coffeescriptcookbook.com/chapters/classes_and_objects/mixins
###
module.exports = (base, mixins...) ->
constructors = []
@donut
donut / CaptainHook.coffee
Last active August 29, 2015 14:05
A simple hook system in JavaScript.
###*
* @file
* A hook system.
* @see https://gist.github.com/donut/f16040209cdc6f5e62c2
###
'use strict'
hook =
_callbacks: []