Skip to content

Instantly share code, notes, and snippets.

View pbaille's full-sized avatar
🏠
Working from home

Pierre Baille pbaille

🏠
Working from home
  • Freelance
  • France
View GitHub Profile
@pbaille
pbaille / dual-worlds.cljc
Created November 17, 2018 19:52
cljs dual names
;; derived from http://blog.fikesfarm.com/posts/2015-12-12-clojurescript-macro-functions.html
(ns dual-world
#?(:cljs (:require-macros [dual-world :refer [add]])))
#?(:clj (defmacro add [& xs] `(+ ~@xs 0.1))
:cljs (defn add [& xs] (apply + xs)))
(add 1 2)
#?(:cljs (apply add 1 2 []))
@pbaille
pbaille / km.cljs
Created November 17, 2018 18:38
keyword maps syntax sugar and basic operations
(ns utils.km-light
(:refer-clojure :exclude [get])
(:require [utils.core :as u]
[utils.check :as uc]
[cljs.core :as c]))
(declare km? kset? kset->km normalize)
(defn catv [a b]
(vec (concat a b)))
@pbaille
pbaille / weird.cljs
Last active November 17, 2018 18:34
weird cljs
(inc nil) ;=> 1
(update {} :a not) ;=> {:a true}
@pbaille
pbaille / shorts.clj
Created November 6, 2018 09:34
alternative control flow for clojure
(ns shorts
(:refer-clojure :exclude [cat])
(:require [clojure.core :as c]))
;; short-circuiting application
;; in this model when a function receive a nil argument it returns nil
;; in addition we can make predicates return their first argument in case of success or nil otherwise
;; we will call this 'guards'
;; ex: (pos? x) returns x only if x is positive else returns nil
@pbaille
pbaille / rules-unquote.red
Last active May 12, 2018 12:28
an attempt to compose parse rules in the same way that lisp compose expressions with macros. e.g unquote splice etc...
;; impl ---------------------------------------------------
traverse: func [x f /only /deep dive?][
until [
x: case [
all [deep dive? x/1] [traverse/deep x/1 :f :dive? next x]
all [not deep not only series? x/1] [traverse/deep x/1 :f :series? next x]
'else [f x]
]
tail? x
@pbaille
pbaille / metarules.red
Last active May 5, 2018 06:55
meta rules
Red [
Description: "a rule for rules"
]
rules: [rule '| rules | rule [end | rules]]
rule: [
m:
block! :m into rules
@pbaille
pbaille / pfn.red
Last active May 2, 2018 21:18
shen inspired function in red
Red []
;; https://gist.github.com/hiiamboris/5f85edba139fc88a5eb0ee9b7b30bc6b
arity?: function [p [word! path!]] [
either word? p [
preprocessor/func-arity? spec-of get :p
][
; path format: obj1/obj2.../func/ref1/ref2...
; have to find a point where in that path a function starts
; i.e. p2: obj1/obj2.../func
(do
(require '[clojure.core.logic :as l])
(require '[clojure.core.logic.fd :as fd])
(defn zip+o [l1 l2 l3]
(l/conde
[(l/== () l1) (l/== () l2) (l/== () l3)]
[(l/fresh [fl1 rl1 fl2 rl2 fl3 rl3]
(l/conso fl1 rl1 l1)
(l/conso fl2 rl2 l2)
(l/conso fl3 rl3 l3)
(letfn [(merge-in* [a b]
(if (map? a)
(merge-with merge-in* a b)
b))]
(defn merge-in
"Merge multiple nested maps."
[& args]
(reduce merge-in* nil args)))
@pbaille
pbaille / destruct.clj
Created February 10, 2017 17:13
IDestructure protocol
(ns exp.destruct
(:refer-clojure :exclude [destructure]))
(defprotocol IDestructure
(-destructure [this data]))
(extend-protocol IDestructure
clojure.lang.PersistentVector
(-destructure [this data]