Skip to content

Instantly share code, notes, and snippets.

View gdeer81's full-sized avatar

Gary Deer gdeer81

View GitHub Profile
@gdeer81
gdeer81 / mind-games.clj
Created November 29, 2017 19:41
My over-engineered solution to a deceptively simple problem
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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 November 29, 2017 17:56
lets print a triangle of stars for fun and profit
;;;;;;;;;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 November 17, 2017 23:14
Example of using :when qualifier in a for comprehension
(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 August 4, 2017 22:00
beginner performance bug
;;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)]
(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 July 5, 2017 15:06
This set of rules causes all products to be hidden...but why?
;; 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 January 23, 2017 12:49 — forked from Deraen/spec-coercion.clj
Clojure.spec coercion test
(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 December 12, 2016 17:24
every form in clojure.core that has four characters
*in*
*ns*
aget
amap
as->
aset
atom
bean
byte
case
@gdeer81
gdeer81 / bounceyball.clj
Last active December 10, 2016 07:25
bouncey ball problem with 2 different solutions
;;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
<!-- 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>