Skip to content

Instantly share code, notes, and snippets.

View sundarj's full-sized avatar

Sundar Joshi sundarj

View GitHub Profile
boot.user=> (def foo 2)
#'boot.user/foo
boot.user=> (def ^:private bar 4)
#'boot.user/bar
boot.user=> (def namespace-qualified-keyword ::yo)
#'boot.user/namespace-qualified-keyword
boot.user=> (def namespace-qualified-symbol `yo)
(defn render-pattern [pattern replacements-map]
(for [item pattern] (if (symbol? item) (get replacements-map item) item)))
#'boot.user/render-pattern
(render-pattern
`["my apple is named " ?apple-name " and i hate " ?hated-thing]
`{?apple-name "peter", ?hated-thing "imperative programming"})
("my apple is named " "peter" " and i hate " "imperative programming")
(apply str *1)
@sundarj
sundarj / clojure-core-ish.js
Last active November 4, 2017 02:23
some clojure.core-ish functions implemented in javascript, with profuse examples
console.clear()
function identity (x) {
return x
}
function add (x, y) {
return parseFloat(x) + parseFloat(y)
}

I am not a good programmer.

If I write in the mainstream, imperative, style, the code invariably results in a mire of utterly avoidable complexity, as I try – and naturally fail – to do the computer’s job for it. It is not the computer’s fault: it simply does what I tell it to. However, I am not good at the low-level details; I think at a much higher level of abstraction than the dumb computer is able to comprehend. I cannot reason at the computer’s level. I am not good at massaging that abstract thought into the lower-level (imperative) format that the computer requires.

Imperative languages are optimised for the computer: that is why they have always been fast, and resource-frugal. I am, alas, a human. I do not enjoy nor want to do computer-level thinking, I do not want to have to keep the irrelevant details of the computer in mind – I have my limits, and have been bitten too many times by being forced to pretend those limits do not exist, and try being a computer. Instead, I would like to reserve as much

;; Reducing function:
;; A function that takes an accumulator and an item and returns a new accumulator.
;; Like the function you would pass to reduce.
;; Transducer:
;; A function that takes a reducing function and
;; returns a new reducing function.
;; Slightly simplified, this is the meat of map with arity one.
boot.user=> '#(+ 1 %)
(fn* [p1__4155#] (+ 1 p1__4155#))
boot.user=> '#(+ 1 %1 %2)
(fn* [p1__4158# p2__4159#] (+ 1 p1__4158# p2__4159#))
boot.user=> '@foo
(clojure.core/deref foo)
boot.user=> '@@foo
(clojure.core/deref (clojure.core/deref foo))
boot.user=> '#'map
(var map)
boot.user=> (let [{:keys [x]} {:x 3, :y 9}] x)
3
boot.user=> (let [{:keys [x]} {:y 9}] x)
nil
boot.user=> (let [{:keys [x] :or {x 4}} {:y 9}] x)
4
boot.user=> (defn define-piece [{:keys [x-fn y-fn max] :or {x-fn identity, y-fn identity}}]
#_=> {:fn (fn [[x y]] [(x-fn x) (y-fn y)]) :max max})
#'boot.user/define-piece
boot.user=> ((:fn (define-piece {:x-fn inc})) [1 2])
[2 2]
@sundarj
sundarj / monoids.md
Created September 4, 2017 01:13 — forked from igstan/monoids.md

Monoids

A monoid is a type for which there exists:

  1. an associative binary operation, let's call it combine
  2. an identity element, let's call it neutral

Please note that it's the combination of an operation valid for that type with a particular instance of that type that forms a monoid, not just a type alone.

@sundarj
sundarj / bartosz-milewski--category-theory.html
Last active August 22, 2017 13:44
Bartosz Milewski's Category Theory articles (so far) in a single HTML file
<style>
body {
background-color: #f5f5f5;
color: #1f1f1f;
font: 110%/1.5 sans-serif;
max-width: 75ch;
margin: 0 auto;
padding: 1em;
box-sizing: border-box;
}