Skip to content

Instantly share code, notes, and snippets.

(ns pie-a-la-mode)
;; Concurrency example from The Pragmatic Programmers. Waiters check if there's
;; enough pie and ice cream before they sell an order, but if they don't
;; coordinate it's still possible to sell more than we have.
;;
;; The below code should return either :ok or :not-available for a given order.
;; If we oversell, then it will throw an exception.
;;
;; How would you change this to ensure that that doesn't happen?
aabacadaeafagahaiajakalamanaoapaqarasatauavawaxayaza
babbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzb
cacbccdcecfcgchcicjckclcmcncocpcqcrcsctcucvcwcxcyczc
dadbdcddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd
eaebecedeefegeheiejekelemeneoepeqereseteuevewexeyeze
fafbfcfdfeffgfhfifjfkflfmfnfofpfqfrfsftfufvfwfxfyfzf
gagbgcgdgegfgghgigjgkglgmgngogpgqgrgsgtgugvgwgxgygzg
hahbhchdhehfhghhihjhkhlhmhnhohphqhrhshthuhvhwhxhyhzh
iaibicidieifigihiijikiliminioipiqirisitiuiviwixiyizi
jajbjcjdjejfjgjhjijjkjljmjnjojpjrjsjtjujvjwjxjyjzj
@tonsky
tonsky / init.lua
Created November 10, 2021 13:57
Hammerspoon config to toggle keyboard layouts
require("hs.hotkey")
require("hs.keycodes")
hs.hotkey.bind({}, "F13", function()
-- hs.keycodes.currentSourceID("me.tonsky.keyboardlayout.helix.english-helix")
hs.keycodes.setLayout("English - Helix")
end)
hs.hotkey.bind({}, "F14", function()
-- hs.keycodes.currentSourceID("me.tonsky.keyboardlayout.helix.russian-helix")
top10Salaries :: FilePath -> IO ()
top10Salaries path = do
Just (h, t) <- uncons . T.lines <$> T.readFile path
let
split = T.splitOn ","
Just ind = elemIndex "Salary" $ split h
top10 :: [Int] = t
& map (\s -> read $ T.unpack $ split s !! ind)
& sortBy (flip compare)
& take 10
@tonsky
tonsky / gist:a91c718800679a65881c2bbe103c7b1d
Created September 28, 2021 20:15
Notes on Designing and Evaluating Reusable Components https://www.youtube.com/watch?v=ZQ5_u8Lgvyk
- Types of reuse
- Layer
- Completely abstracts underlying problem. Your code calls layer
- Assume singleton, can conflict with another layer
- Engine
- Orchestrates whole app Engine calls you
- Component
- You call component, but component might call you back
- Most benefits, hardest to design
- Optimization axes
@tonsky
tonsky / refactored.java
Created September 25, 2021 18:28
Refactoring of piece of code from Clean Code book
package fitnesse.html;
import fitnesse.responders.run.SuiteResponder;
import fitnesse.wiki.*;
public class SetupTeardownIncluder {
public static String render(PageData pageData, boolean isSuite) throws Exception {
StringBuilder sb = new StringBuilder();
WikiPage testPage = pageData.getWikiPage();
if (pageData.hasAttribute("Test")) {
(def ^:private last-window-ms 120000)
(def ^:private max-last-txs 3)
defn- authorize-tx [authorizer tx]
(let [{:keys [active-card available-limit]} authorizer
{:keys [amount time]} tx]
(cond
(nil? active-card)
(assoc authorizer
@tonsky
tonsky / tictacttoe.clj
Last active October 14, 2020 10:17
Write a program that plays every possible tic-tac-toe game, and then prints the number of valid games
(defn end? [[x1 x2 x3 x4 x5 x6 x7 x8 x9]]
(or
(and (some? x1) (= x1 x2 x3))
(and (some? x4) (= x4 x5 x6))
(and (some? x7) (= x7 x8 x9))
(and (some? x1) (= x1 x4 x7))
(and (some? x2) (= x2 x5 x8))
(and (some? x3) (= x3 x6 x9))
(and (some? x1) (= x1 x5 x9))
(and (some? x3) (= x3 x5 x7))
package com.example.onair
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.*
import androidx.ui.core.Text
import androidx.ui.core.dp
import androidx.ui.core.setContent
import androidx.ui.foundation.Clickable
import androidx.ui.foundation.ColoredRect
(ns datomicdemo.core
(:require
[clojure.edn]
[clojure.string :as str]
[datomic.api :as d]))
(def url "datomic:free://localhost:4334/datomicdemo")
(d/create-database url)