Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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))))
@donut
donut / getBGImgURLsFromCSSs.js
Created October 17, 2011 21:08
Builds a list of images found in the linked style sheets
/* Builds a list of images found in the linked style sheets
*
* Adapted from stackoverflow.com/questions/2430503/list-of-all-background-images-in-dom/2453880#2453880
*
* This method has the advantage of finding URLs for background images that no
* element in the DOM uses yet.
*
* @return {array}
* List of unique image URLs as specified in the style sheets.
------------------------------------------------------------------------- */

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 / is_popup_window.js
Created October 24, 2011 21:54
Is popup window
// Try to guess if the current window is a "popup"
window.isPopup =
window.isPopup || window.opener ||
(window.locationbar && window.locationbar.visible === false) ||
false;
@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
}
// Adapted from [mbilalsiddique1]'s comment on this gist:
// https://gist.github.com/alanhogan/2878758
//
// Note that sprites are ordered for first by file size (ascendingly) and then
// by name.
// https://github.com/chriseppstein/compass/issues/690#issuecomment-3729204
@import "compass/utilities/sprites"
@import "compass/css3/background-size"