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
;; "Find the sum of all the multiples of 3 or 5 below 1000." | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Filtering | |
;; "Create a divides? predicate that takes a dividend and a divisor, and returns true if divisor evenly | |
;; divides the dividend" | |
;; My solution to the filtering "divides?" problem: | |
(defn divides? [num div] (= 0 (mod num div))) |
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Atoms | |
;; Using swap! and merge-with on a map | |
;; the trick is to put merge-with inside a function. | |
(def m (atom {:a 1})) | |
;=> #'user/m | |
user> (swap! m #(merge-with + % {:a 2})) |
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 3.2 Nil pun with care | |
(defn print-seq [s] | |
(when (seq s) | |
(prn (first s)) | |
(recur (rest s)))) | |
;; #'user/print-seq | |
(print-seq [1 2]) |
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
(re-find #"A|B|C" "B") ;; Two Problems | |
;=> "B" | |
(some #(= % "B") ["A" "B" "C"]) ;; A little better...but not the same | |
;=> true | |
(get #{ "A" "B" "C"} "B") ;; more readable? | |
;=> "B" | |
(#{"A" "B" "C"} "B") ;; Idiomatic per Joy of Clojure |
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 joy.q) | |
;; nil | |
(defn nom [n] (take n (repeatedly #(rand-int n)))) | |
;; #'joy.q/nom | |
(defn sort-parts [work] | |
(lazy-seq | |
(loop [[part & parts] work] ;; Pull apart work - note: work will be a list of lists. | |
(if-let [[pivot & xs] (seq part)] ;; This blows up unless work was a list of lists. |
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
;; Example of using comp | |
(map (comp keyword #(.toLowerCase %) name) '(a B C)) | |
;=> (:a :b :c) | |
;; equpivalent code using -> (but which doesn't use functions as data. | |
(map #(-> % (name) (.toLowerCase) (keyword)) '[a B C]) | |
;=> (:a :b :c) | |
;; Using partial |
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
import java.util.Random; | |
import java.util.concurrent.*; | |
/** | |
* App that tests completion service. | |
*/ | |
public class CompletionServiceExample { | |
private static final int COUNT = 25; |
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
package com.transacttools.imaginary; | |
import com.transacttools.imaginary.util.ActionUtils; | |
import java.util.Date; | |
import java.text.SimpleDateFormat; | |
/** | |
* This class processes actions from the User Interface. | |
* | |
* TODO: Get this ready for production! |
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
#include <stdio.h> | |
// see http://stackoverflow.com/a/7943955/7507 | |
int main(int argc, char *argv[]) { | |
int bestSoFar = 0; | |
int bestNow = 0; | |
int bestStartIndexSoFar = -1; | |
int bestStopIndexSoFar = -1; |
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
(defn factorial-using-recur [x] | |
(loop [current x | |
next (dec current) | |
total 1] | |
(if (> current 1) | |
(recur next (dec next) (* total current)) | |
total))) | |
(defn factorial-using-reduce [x] | |
(reduce * (range 1 (inc x)))) |