This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;;;;;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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; 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 |
OlderNewer