Skip to content

Instantly share code, notes, and snippets.

View darkleaf's full-sized avatar

Mikhail Kuzmin darkleaf

View GitHub Profile
@darkleaf
darkleaf / merge-with-key.clj
Created January 9, 2021 07:13 — forked from mlimotte/merge-with-key.clj
Clojure merge-with-key
(ns mlimotte.util)
; A variation on clojure.core/merge-with
(defn merge-with-key
"Returns a map that consists of the rest of the maps conj-ed onto
the first. If a key occurs in more than one map, the mapping(s)
from the latter (left-to-right) will be combined with the mapping in
the result by calling (f key val-in-result val-in-latter)."
[f & maps]
@darkleaf
darkleaf / question.md
Last active April 15, 2023 08:34
Algebraic effects для Clojure.

Привет!

Ключевые слова: coroutine, continuation, generators, async/await, project loom, free monad, algebraic effects.

Я хочу сделать алгебраические эффекты для Clojure(Script). Я уже сделал библиотеку https://github.com/darkleaf/effect/blob/doc-2/README.md Но есть моменты, которые вызывают у меня вопросы.

Если очень кратко, то лично мне эффекты нужны, чтобы:

  1. удобно тестировать бизнес логику
(ns example.core
(:require
[goog.dom :as dom]
[applied-science.js-interop :as j]
[example.react :as react]
[goog.object :as gobj]
[clojure.walk :as w]))
(def e (gobj/get js/React "createElement"))
@darkleaf
darkleaf / example.core.clj
Created April 21, 2020 12:04
react interop example
(ns example.core
(:require
[hicada.compiler :as h]))
(defmacro html [body]
(h/compile body {} {} &env))
create table keys (
  id       integer      primary key,
  name     varchar(255) not null
);

create table data (
  point     timestamptz not null,
  key_id    integer not null,
  value     real not null
;; (t/deftest x1
;; (let [f (fn [a b]
;; (js/setTimeout a 0))]
;; (t/async done
;; (let [x (fn []
;; (t/is (= 1 1))
;; (done)
;; (prn :foo))]
;; (f x x)))))
@darkleaf
darkleaf / effect.md
Last active November 27, 2019 10:32

Подробно опишу суть вопроса.

Базовый пример:

(t/deftest simple-use-case
  (let [ef                (fn []
                            (eff
                              (let [rnd (! [:random])]
 (- (* 2. rnd)
(ns foo
(:import
[clojure.lang Compiler Compiler$C]))
(defn get-closure-names* [form env]
(let [ex (with-bindings {Compiler/LOCAL_ENV env}
(Compiler/analyze Compiler$C/EXPRESSION form))
closes (.closes ex)]
(->> closes
(keys) ;; у них ключи и значения совпадают
@darkleaf
darkleaf / validation.md
Last active January 6, 2019 18:05
validation

Предположим, что у нас есть приложение, хранящее и показывающие публикации. Публикации создают зарегистрированные пользователи. Просматривать публикации могут также и гости.

Неважно какие технологии использует приложение. Важно только то, что приложение использует базу данных и имеет возможность показывать формы, списки, текст.

У публикации есть поля:

  • заголовок
  • описание

Идея - валидировать данные, задавая путь к значению и проверющий предикат.

Проблема в том, что не понятно какой формат должен иметь отчет об ошибках. Допустим, ошиибка в множестве, или в ключе мапы. Как указать путь к ошибочному значению? Что потом с этим путем делать?

#{ {:key :correct}  {:key :wrong} }

{ {:key :wrong} :value }