Skip to content

Instantly share code, notes, and snippets.

@tonsky
tonsky / btset_bench.clj
Created March 16, 2017 06:13
Benchmarking DataScript’s btset against Clojure’s sorted-set/vector
(defn bench-btset []
(doseq [distinct [:distinct :duplicates]
size [20000]
[tn target] [["sorted-set" (sorted-set)]
["btset" (btset/btset)]
["vector" []]]
:let [range (if (= :distinct distinct)
(shuffle (range size))
(repeatedly size #(rand-int size)))
shuffled-range (shuffle range)
@tonsky
tonsky / clojure+nbsp.clj
Last active January 17, 2018 09:14
Clojure and nbsp
(let [a 1, a  2,  a 3]
(and (= a 1) (= a  2) (=  a 3)))
@tonsky
tonsky / routing.clj
Created July 4, 2018 22:09
Trouble with bidi routing
;; I have this
(def routes ["/" [["" :ui/index]
["api/query" :api/query]
["api/data-init" :api/data-init]]])
;; And I want to make api/data-init only accesible through POST
;; I tried
(def routes ["/" [["" :ui/index]
(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)
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
@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))
(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 / 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")) {
@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
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