Skip to content

Instantly share code, notes, and snippets.

Avatar

Gary Deer gdeer81

View GitHub Profile
@gdeer81
gdeer81 / mind-games.clj
Created Nov 29, 2017
My over-engineered solution to a deceptively simple problem
View mind-games.clj
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; string s = "123456789"
;; int target = 100
;; how many combinations of +/- give you the target int?
;; + and - can be inserted at any index
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;helper functions;;;;;;
(defn str->vec
"converts a string of consecutive numbers to a vector of individual numbers
@gdeer81
gdeer81 / make-stars.clj
Created Nov 29, 2017
lets print a triangle of stars for fun and profit
View make-stars.clj
;;;;;;;;;Given this yucky implementation:
(defn print-row [num]
(loop [iter 0])
(if (< iter num)
(do (print "*")
(recur (inc iter)))
(println "")))
(defn print-a-triangle [num]
(loop [iter 0]
@gdeer81
gdeer81 / for-when.clj
Created Nov 17, 2017
Example of using :when qualifier in a for comprehension
View for-when.clj
(set! *unchecked-math* false)
(defn find-the-pairs [^long n]
"takes an upper bound and returns all the pairs of numbers where
(* x y) equals the sum of the entire collection without x and y
(find-the-pairs 26) => [[15 21] [21 15]]
"
(let [^long coll-sum (/ (* n (inc n)) 2)]
(for [^long i (range 1 (inc n)) ^long j (range 1 (inc n))
:when (= (- coll-sum i j) (* i j))]
@gdeer81
gdeer81 / findpairs.clj
Last active Aug 4, 2017
beginner performance bug
View findpairs.clj
;;this works until you pass in ridiculously large numbers
;; the for loop lets me try all of the numbers in the collection with each other
(defn find-the-pairs [n]
"takes an upper bound and returns all the pairs of numbers where
(* x y) equals the sum of the entire collection without x and y
(find-the-pairs 26) => [[15 21] [21 15]]
"
(let [coll (take n (iterate inc 1))
coll-sum (apply + coll)]
View random-examples.clj
(def inc-coll (partial map inc))
;;#'user/inc-coll
(inc-coll [4 5 6])
;;(5 6 7)
(def inc-coll-coll (partial map inc-coll))
;;#'user/inc-coll-coll
(inc-coll-coll [[1] [2]])
;;((2) (3))
(inc-coll-coll [[1] 3])
;;IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:542)
@gdeer81
gdeer81 / rules.cljs
Created Jul 5, 2017
This set of rules causes all products to be hidden...but why?
View rules.cljs
;; Filtering
; product visible when no filter
(define [:app :visible-product/id ?e] :- [:not [_ :product-filter/range]]
[:not [_ :product-filter/category]]
[[?e :product/name]])
; product visible when filter active and price in range
(define [:app :visible-product/id ?e]
:- [[_ :product-filter/range ?range]]
@gdeer81
gdeer81 / spec-coercion.clj
Last active Jan 23, 2017 — forked from Deraen/spec-coercion.clj
Clojure.spec coercion test
View spec-coercion.clj
(ns spec-test.core
(:require [clojure.spec :as s]))
(defn x-integer? [x]
(if (integer? x)
x
(if (string? x)
(try
(Integer/parseInt x)
(catch Exception e
@gdeer81
gdeer81 / 4char_forms.txt
Created Dec 12, 2016
every form in clojure.core that has four characters
View 4char_forms.txt
*in*
*ns*
aget
amap
as->
aset
atom
bean
byte
case
@gdeer81
gdeer81 / bounceyball.clj
Last active Dec 10, 2016
bouncey ball problem with 2 different solutions
View bounceyball.clj
;;compare the two approaches to solving the same problem
;;A child plays with a ball on the nth floor of a big building the height of which is known
;;(float parameter "h" in meters, h > 0) .
;;He lets out the ball. The ball rebounds for example to two-thirds
;;(float parameter "bounce", 0 < bounce < 1)
;;of its height.
;;His mother looks out of a window that is 1.5 meters from the ground
;;(float parameters window < h).
;;How many times will the mother see the ball either falling or bouncing in front of the window
View gist:1a87b20b628b9becd7c8
<!-- server passes in a collection of product maps -->
<!-- controller now sends a collection of collections of product maps, specifically (partition-all 4 products) -->
{% for product-partition in partitioned-products %}
{% for product in product-partition %}
<div class="col-sm-10 col-sm-offset-2">
<div class="row">
<div class="col-sm-3" style="height:250px;">
<p>{{product.name}}</p>
<p>Price ${{product.price}}</p>