Skip to content

Instantly share code, notes, and snippets.

@rxacevedo
rxacevedo / Pascal.scala
Last active December 25, 2015 13:29
Pascal's triangle v2, 30 rows.
import annotation.tailrec
object Pascal {
def main(args: Array[String]) {
for (i <- 0 to 30) {
for (j <- 0 to i) {
print(pascalRowColumn(i, j) + " ")
}
@rxacevedo
rxacevedo / gist:14e10bb0afb84a9f4e2c
Created November 18, 2013 16:10
Higher-order functions in Scala
// O(e) memory/space, O(e) time
def combine(f: (Int, Int) => Int)(a: Int, b: Int, d: Int): Int = {
if (b == 0) d
else f(a, combine(f)(a, b - 1, d))
}
// O(1) memory/space, O(e) time
def combine2(f: (Int, Int) => Int)(a: Int, b: Int, d: Int): Int = {
def inner (acc: Int, iter: Int): Int = {
if (iter == 0) acc
;; This buffer is for notes you don't want to save, and for Lisp evaluation.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.
(defn pascal-row-column
"Compute the number in Pascal's triangle at the specified row/column
position using tree-recursive approach."
[r c]
(if (or (zero? c) (= r c)) 1
(+ (pascal-row-column (dec r) c)
@rxacevedo
rxacevedo / gist:8127831
Created December 25, 2013 23:19
Lazy Fermat sequence.
(def fermats (map #(inc (expt 2 (expt 2 %))) (iterate inc 0)))
@rxacevedo
rxacevedo / plotting_method_runtimes.clj
Last active January 2, 2016 03:49
Plot running times of three collection reversal implementations.
(let [coll-sizes (map #(expt 2 %) (range 5 10)) ;; Set up the collection sizes to be used
methods ['rvrs 'my-rvrs 'reverse]] ;; Hold method names in a collection as symbols
(-> (for [x methods
y coll-sizes
:let [z (binding [*out* (java.io.StringWriter.)] ;; Pipe stdout to a StringWriter (x * y instantiations)
(time ((resolve x) (range 0 y))) ;; Resolve x (symbol) to actual function and apply it to coll (size y), timing the running time and printing it to the StringWriter
(->> (.. *out* toString) ;; Retrieve output from StringWriter
(re-find #"\d+.\d+") ;; Parse out the numeric value (running time)
Double/parseDouble))]]
[x y z]) ;; Convert to double for plotting
@rxacevedo
rxacevedo / gist:8472321
Last active January 3, 2016 13:49
Interesting difference in the result of computing the sum of powers for a constant c where 0 < c < 1, powers 0 through n inclusive (elements in a geometric series).
concabs.core> (defn g [n]
"Returns a function that takes a constant c and sums
the powers of c, 0 to n inclusive."
(fn [c] (reduce + (map (fn [e] (expt c e)) (range 0 (inc n)))))) ;; No rounding, clojure.math.numeric-tower/expt
;; #'concabs.core/g
concabs.core> ((g 2000) 1/2)
;; 229626139054850904846566640235536396804463540417739040095528547365153252278474062771331897263301253983689192927797492554689423792172611066285186271233330637078259978290624560001377558296480089742857853980126972489563230927292776727894634\
;; 052080932707941809993116324797617889259211246623299072328443940665362688337817968917011204758969615828117801869553000858005433413251661044016264472562583522535766634413197990792836254043559716808084319706366503081778867804183841109915567\
;; 179344078320163914433261165510760851167452031056697572838864109017830551567765250350871057601645685541635930907524369702298058751/114813069527425452423283320117768198402231770208869520047764273682576626139237031
;; Triple-sum problem stuff
;;
;; Using this command:
(for [f ["1Kints.txt"
"2Kints.txt"
"4Kints.txt"]
:let [t (->> (:out (sh "java" "-cp"
".:stdlib.jar"
"ThreeSum"
(str "http://algs4.cs.princeton.edu/14analysis/" f)
@rxacevedo
rxacevedo / renewals_testing.clj
Last active January 4, 2016 19:59
Using Clojure to do just about everything.
;; Logging steps taken to validate local irjobs output against current dev.
;; Get the deps
(require '[java-jdbc [sql :as s]])
(require '[clojure.java [jdbc :as j]])
;; Count the records in the database from today's run:
(count (j/query dv01-shareduser (s/select * :shareduser.mlcrtpxp_renewals_result
(s/where {"TRUNC(process_date_time)" "28-JAN-2014"}))))
;; 45
@rxacevedo
rxacevedo / xpath_scraping.clj
Last active August 29, 2015 13:56
Scraping via XPath
;; xalan 2.6 gets put on the classpath when using
;; Incanter, causes issues with clj-xpath. So DON'T
;; use Incanter when using clj-xpath.
(->> (ClassLoader/getSystemClassLoader)
(.getURLs)
(map #(.toString %))
(clojure.string/join "\n")
(re-seq #".*xalan.*")
(pprint))
@rxacevedo
rxacevedo / hiccup_example.clj
Created February 26, 2014 18:00
HTML markup via hiccup in Clojure
; This
(html [:html
[:head
[:meta {:charset "utf-8"}]
[:title "Title"]]
[:body
[:header
[:h1 "H1 header!"]]
[:section
[:ul