{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "; lazy infinite sequence with recursive definition\n", "(def fib-seq-lazy \n", " ((fn rfib [a b] \n", " (lazy-seq (cons a (rfib b (+ a b)))))\n", " 0 1))\n", "(take 20 fib-seq-lazy)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Will print\n" ] }, { "data": { "text/plain": [ "null" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "; 'unless' cannot be defined with a function because\n", "; it does not always evaluate both its arguments.\n", "(defmacro unless [pred a b]\n", " `(if (not ~pred) ~a ~b))\n", "(unless false (println \"Will print\") (println \"Will not print\"))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "([0 1 2 3 4 5] [6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23] [24 25 26 27 28 29] [30 31 32 33 34 35])\n", "Distinct: 36\n", "([17 2 16 26 24 25] [11 28 27 3 23 35] [34 21 8 31 7 19] [20 0 30 15 1 12] [29 22 32 13 6 14] [5 10 18 4 9 33])\n", "Distinct: 36\n" ] }, { "data": { "text/plain": [ "null" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn run [nvecs nitems nthreads niters]\n", " (let [vec-refs (vec (map (comp ref vec)\n", " (partition nitems (range (* nvecs nitems)))))\n", " swap #(let [v1 (rand-int nvecs)\n", " v2 (rand-int nvecs)\n", " i1 (rand-int nitems)\n", " i2 (rand-int nitems)]\n", " (dosync\n", " (let [temp (nth @(vec-refs v1) i1)]\n", " (alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2))\n", " (alter (vec-refs v2) assoc i2 temp))))\n", " report #(do\n", " (prn (map deref vec-refs))\n", " (println \"Distinct:\"\n", " (count (distinct (apply concat (map deref vec-refs))))))]\n", " (report)\n", " (dorun (apply pcalls (repeat nthreads #(dotimes [_ niters] (swap)))))\n", " (report)))\n", "; When run, we see no values get lost or duplicated in the shuffle.\n", "; There are 36 distinct numbers before and after. \n", "(run 6 6 6 100000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "metadata": { "beakerx_kernel_parameters": { "classpath": [], "imports": [ "com.twosigma.beaker.chart.xychart.*", "com.twosigma.beaker.chart.categoryplot.plotitem.*", "com.twosigma.beaker.chart.Filter", "com.twosigma.beaker.chart.categoryplot.*", "com.twosigma.beaker.chart.Color", "com.twosigma.beaker.chart.xychart.plotitem.*", "com.twosigma.beaker.chart.GradientColor", "com.twosigma.beaker.NamespaceClient", "com.twosigma.beaker.chart.legend.*", "com.twosigma.beaker.chart.histogram.*", "com.twosigma.beaker.chart.heatmap.HeatMap" ] }, "kernelspec": { "display_name": "Clojure", "language": "clojure", "name": "clojure" }, "language_info": { "codemirror_mode": "Clojure", "file_extension": ".clj", "mimetype": "text/x-clojure", "name": "Clojure", "nbconverter_exporter": "", "pygments_lexer": "", "version": "1.7" } }, "nbformat": 4, "nbformat_minor": 2 }