Skip to content

Instantly share code, notes, and snippets.

Avatar

Yehonathan Sharvit viebel

View GitHub Profile
@viebel
viebel / proxy.cljs
Last active Feb 22, 2021
Make a ClojureScript object accessible as a JavaScript object without converting it
View proxy.cljs
;; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
(defn proxy [m]
(js/Proxy. m #js {:get (fn [target prop]
(let [prop' (if (vector? target)
(js/parseInt prop)
(keyword prop))]
(let [v (or (get target prop')
(get target prop))]
(if (coll? v)
(proxy v)
View flattenObject.js
function setVerbatim (obj, k, v) {
// In Lodash, a dot in a key is interpreted as a nesting object
// For instance, _.set({}, "a.b", 2) returns {"a": {"b": 2}}
// While setVerbatim({}, "a.b", 2) returns {"a.b": 2}
return _.merge(obj, {[k]: v})
}
function flattenObject(obj, prefix = '') {
return _.reduce(obj,
function(acc, v, k) {
@viebel
viebel / protocol-do.clj
Last active Nov 26, 2020
Protocol and records in a DO compliant way
View protocol-do.clj
(defprotocol FullName
(full-name [person]))
(defrecord AuthorEncapsulation [firstname lastname]
FullName
(full-name [author] (str (:firstname author)
" "
(:lastname author))))
(defrecord AuthorData [firstname lastname])
View reagent-tree-comp.cljs
(ns simple.core
(:require
[reagent.core :as reagent :refer-macros [with-let]]
[reagent.dom :refer [render]]))
(def my-org
{:name "nestle"
:description "bla bla bla"
:children [{:name "Choco"
:description "woo"}
View reagent-demo.cljs
(ns simple.core
(:require [reagent.core :as reagent]
[reagent.dom :refer [render]]))
(defn ui []
[:div
[:h1 "Hello world"]])
View prewalk-vs-postwalk.clj
(require '[clojure.walk :refer [prewalk postwalk]])
(def data {:a 1
:b {:c 2}
:d [3 {:e 4}]})
;; prewalk: node -> map f to children of f(node)
;; prewalk: [a b c] -> (map f (f [a b c]))
;; prewalk: transform node and then transform chilren
View README.md

Self-hosted:

$ plk
ClojureScript 1.10.597
cljs.user=> (require '[foo.core :refer [defnmy]])
nil
cljs.user=> (defnmy FOO clojure.string/lower-case [x]
       #_=>   (inc x))
#'cljs.user/foo
View minesweeper.clj
(defn vec->matrix [n v]
(->> v
(partition n)
(mapv vec)))
(defn empty-vec [n]
(into [] (repeat n nil)))
(defn rand-numbers [n k]
(->> (range n)
View riccardo.clj
(defn read-input []
(loop []
(let [input (read-line)]
(when-not (= ":done" input))
(println (str "You entered: >>" input "<<"))
(recur))))
@viebel
viebel / rules.clj
Created Nov 14, 2019
text rules matching
View rules.clj
(ns my.chat
(:require [clojure.string :as str]))
(def rules
[["hello" "hello you"]
["bye" "see you soon"]
["my name is Riccardo" "nice to meet you Riccardo"]])
(defn respond-fixed [input]