Skip to content

Instantly share code, notes, and snippets.

View MichaelDrogalis's full-sized avatar

Michael Drogalis MichaelDrogalis

  • Confluent
  • Seattle, WA
View GitHub Profile
(ns topics.core
(:require [clamq.activemq :refer :all]
[clamq.protocol.connection :as connection]
[clamq.protocol.producer :as producer]
[clamq.protocol.consumer :as consumer]))
(def broker (activemq-connection "tcp://localhost:61616"))
(def topic "computational-topic")
(def producer (connection/producer broker {:pubSub true}))
(defn divider [a b]
(try
(/ a b)
(catch java.lang.ArithmeticException e
"Dividing by 0!")))
(divider 10 2) ; => 5
(divider 10 0) ; => Dividing by 0!
(defn divider [a b]
(/ a b))
(with-handler! #'divider
java.lang.ArithmeticException
(fn [e a b] "Dividing by 0!"))
(divider 10 2) ; => 5
(divider 10 0) ; => Dividing by 0!
(ns dire-example.core
(:require [dire.core :refer [with-precondition with-precondition! supervise]]))
(defn func1 [x]
{:pre [(< x 2)]}
"I have the precondition inline! (Clojure default)"
(println x " received a number smaller 2"))
(defn func2 [y]
"I outsourced the precondition in a cool AOP style using dire"
(ns dire-examples.bad-core
(:require [dire.core :refer [with-precondition! with-postcondition!]]))
(defn save-user [& {:keys [username email password state]}]
(if (and (and (> (count username) 4) (< (count username) 11))
(and (> (count password) 6) (re-matches #".*\d.*" password))
(re-matches #"\w+@\w+\.\w+" "[email protected]")
(re-matches #"[A-Z]{2}" state))
(let [result :ok] ; Or :not-ok
"Persist the user to a database here."
def divider a, b
a / b
end
handlers = {}
handlers[TypeError] =
Proc.new { |e| puts "Type exception occured." }
handlers[ZeroDivisionError] =
(ns clotp.core
(:require [clojure.core.match :refer [match]]))
(def next-process-id (atom 0))
(def processes (atom {}))
(defn next-pid []
(swap! next-process-id inc))
(defn spawn [f & args]
user> (def fiz (take 15 (cycle [nil nil "Fiz"])))
#'user/fiz
user> (def buzz (take 15 (cycle [nil nil nil nil "Buzz"])))
#'user/buzz
user> (map (partial apply str) (map list fiz buzz))
("" "" "Fiz" "" "Buzz" "Fiz" "" "" "Fiz" "Buzz" "" "Fiz" "" "" "FizBuzz")
-module(core).
-export([future/1, f/0]).
f() ->
timer:sleep(20000),
"My result.".
future(F) ->
Res = F(), % Evaluate ASAP.
receive % Wait until it's dereferenced to divulge the result.
-module(core).
-export([front_end_task/0, task/1, main/1]).
%% Call fn
%% Get back a future
%% Dereference
%% - Blocks until if not done, then returns
%% - If done, returns immediately
task(Pid) ->