Skip to content

Instantly share code, notes, and snippets.

@mrrodriguez
mrrodriguez / clara-logical-loop.situation.clj
Last active Sep 24, 2021
Clara-rules typical logical loop
View clara-logical-loop.situation.clj
;; Usually the solution is to reframe the problem you have in a way that avoids the logical contradiction,
;; ie. 'not B => B'
;; and continue to use the default inserts (not unconditional type) that participate in truth maintenance
;; To avoid a rule like this:
(r/defrule b-contradiction
[A]
[:not [B]]
=>
(r/insert! (->B))
@mrrodriguez
mrrodriguez / clj-data-fressian-serde.clj
Last active Feb 8, 2021
Clojure Serialization With Fressian
View clj-data-fressian-serde.clj
;;;; Fressian dependency used in these examples
;; [org.clojure/data.fressian "0.2.1"]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Using only `org.clojure/data.fressian`
(require '[clojure.data.fressian :as fres])
(defn serde-obj
@mrrodriguez
mrrodriguez / clara_tiered_fact_update_rules.clj
Last active Jul 1, 2020
Clara tiered fact update rules
View clara_tiered_fact_update_rules.clj
(require '[clara.rules :as r])
;;;; Define 3 rules, where the "priority" order is r1, r2, r3, where the highest priority is first
;;;; and the rest is in descending order of priority.
;;;; :type :rule/result "syntetic" fact is used to hold the final changes that can be queried out
;;;; from a session after `r/fire-rules` via `r/query` on the `find-results` query.
;;;; A namespace qualified keyword is used to avoid collision with externally given :type of
;;;; "real" facts.
@mrrodriguez
mrrodriguez / failures.clj
Created May 3, 2019
Using `clojure.core/eval` at CLJS macroexpansion-time
View failures.clj
#error {
:cause "Unable to resolve symbol: bar in this context"
:via
[{:type clojure.lang.ExceptionInfo
:message "failed compiling file:src/example/foo.cljs"
:data {:file #object[java.io.File 0x2766fb87 "src/example/foo.cljs"]}
:at [clojure.core$ex_info invokeStatic "core.clj" 4739]}
{:type clojure.lang.ExceptionInfo
:message "java.lang.RuntimeException: Unable to resolve symbol: bar in this context, compiling:(*thing*:6:9) at line 6 src/example/foo.cljs"
:data {:file "src/example/foo.cljs", :line 6, :column 1, :tag :cljs/analysis-error}
View reagent-fix-cursor-jump.cljs
;;;; Dealing with jumping cursors on controlled input (a classic React/Reagent problem).
;;;; For rationale see
;;;; https://github.com/reagent-project/reagent/issues/265#issuecomment-397895508
;;;; &
;;;; https://github.com/reagent-project/reagent/blob/e53be656073af9209c4fe8bc9119635953311d42/examples/material-ui/src/example/core.cljs#L34
;; In the ns header...
(:require [reagent.core :as r]
[reagent.impl.template :as template])
@mrrodriguez
mrrodriguez / print_method.clj
Last active Aug 24, 2016
print-method based clara-rules durability impl
View print_method.clj
(ns clara.rules.durability.print-method
(:require [clara.rules.durability :as d]
[clara.rules.memory :as mem]
[clara.rules.engine :as eng]
[clara.rules.compiler :as com]
[clojure.java.io :as jio]
[clojure.main :as cm])
(:import [clara.rules.durability
MemIdx]
[clara.rules.memory
@mrrodriguez
mrrodriguez / unindexing_durability.clj
Created Aug 19, 2016
Back when clara.rules.durability had the concept of "unindex"ing memory - was removed for efficiency
View unindexing_durability.clj
(ns clara.rules.durability
"Experimental namespace. This may change non-passively without warning.
Support for persisting Clara sessions to an external store.
See the serialize-session-state function to retrieve the full state of a session as a data structure that can
be easily serialized via EDN or Fressian. Sessions can then be recovered with the restore-session-state function.
TODO: diff support is pending -- functions for obtaining a diff of state from a previous point, allowing for a write-ahead log."
(:require [clara.rules :refer :all]
@mrrodriguez
mrrodriguez / gist:8046814
Last active Dec 31, 2015
Macro returning fn objects with closures
View gist:8046814
;;; case 1
(defn get-map-of-fn [x] (fn [] x))
;= #'user/get-map-of-fn
(defmacro map-of-fn [arg] (get-map-of-fn arg ))
;= #'user/map-of-fn
(map-of-fn 1)
;= #<user$get_map_of_fn$fn__9648 user$get_map_of_fn$fn__9648@47020d4>
View gist:7354670
I have some Clojure clj files in a jar called A.jar.
This jar is started up, and I see that all of the clj files are loaded by the Application ClassLoader, along with the rest of the classes in the jar.
At runtime a new jar is added to the classpath, with some clj files that `require`s some of the clj files already loaded by the App ClassLoader.
What I have observed is that, when this newly added jar's clj files are being loaded, the `require`s they have on already loaded clj classes, are causing them to be reloaded.
The issue is that if these files that are reloaded, have something like `defrecord` in them, they load a new "version" of the same class, with a different classloader.
View gist:7302050
user> (defrecord MyRec [x])
user.MyRec
user> (def m (->MyRec 5))
#'user/m
user> (instance? MyRec m)
true
user> (eval (defrecord MyRec [x]))
user.MyRec
user> (def m2 (->MyRec 5))
#'user/m2