Last active
August 29, 2015 14:17
-
-
Save nikolaplejic/a9edc537c46dd7b3f9d0 to your computer and use it in GitHub Desktop.
Lambda Zagreb - Clojure code snippets
This file contains hidden or 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 lambdazg.core | |
(:require [clojure.core.async :as async :refer [>! <! >!! <!! go chan buffer close! thread | |
alts! alts!! timeout]]) | |
(:gen-class)) | |
;; -- | |
(+ 2 2) | |
(println "Hello, world") | |
(def a '(1 2 3 4)) | |
a | |
;; -- | |
(map (fn [x] (* x 2)) a) | |
(reduce + a) | |
(for [x '(1 2 3 4) y '(5 6 7 8)] | |
(list x y)) | |
;; -- | |
(def v [1 2 3 4]) | |
(conj v 5) | |
v | |
;; -- | |
(defn make-it-double | |
[x] | |
(* x 2)) | |
(make-it-double 2) | |
;; -- | |
(def make-it-double! | |
(fn [x] (* x 2))) | |
(make-it-double! 2) | |
;; -- | |
(def a-future | |
(future | |
(Thread/sleep (+ 4000 (rand-int 2000))) | |
(println "done") | |
42)) | |
(future-done? a-future) | |
(deref a-future) | |
@a-future | |
;; -- | |
(def futures (for [x (range 10)] | |
(future | |
(Thread/sleep (+ 1000 (rand-int 20000))) | |
(println "done") | |
42))) | |
(while (not (every? true? (map future-done? futures))) | |
(println (map future-done? futures)) | |
(Thread/sleep 500)) | |
(map deref futures) | |
;; -- | |
(def c (chan)) | |
(future | |
(while true | |
(let [v (<!! c)] | |
(println v)))) | |
(future | |
(Thread/sleep 3000) | |
(>!! c "I'm in another thread again!")) | |
;; -- | |
(def at (atom [])) | |
at | |
@at | |
(conj at 5) ;; whoops | |
(swap! at conj 5) | |
@at | |
;; -- | |
This file contains hidden or 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
(defproject lambdazg "0.1.0-SNAPSHOT" | |
:description "FIXME: write description" | |
:url "http://example.com/FIXME" | |
:license {:name "Eclipse Public License" | |
:url "http://www.eclipse.org/legal/epl-v10.html"} | |
:dependencies [[org.clojure/clojure "1.6.0"] | |
[org.clojure/core.async "0.1.346.0-17112a-alpha"]] | |
:main ^:skip-aot lambdazg.core | |
:target-path "target/%s" | |
:profiles {:uberjar {:aot :all}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment