Skip to content

Instantly share code, notes, and snippets.

View gist:1333534
(defn mandelbrot [^double cr ^double ci ^long limit]
(loop [rate limit
zr 0.0
zi 0.0]
(let [zr2 (* zr zr)
zi2 (* zi zi)]
(if (and (> rate 0) (< (+ zr2 zi2) 4.0))
(recur (dec rate) (+ cr (- zr2 zi2)) (+ ci (* 2.0 (* zr zi))) )
View gist:1333551
(defn mandelbrot [c ^long limit]
(loop [rate limit
s (iterate #(+ (* % %) c) 0.0)]
(if (and (> rate 0) (< (abs (first s)) 2.0))
(recur (dec rate) (rest s))
View gist:1333587
(defn- mandelbrot-seq [x-range y-range [lr li] d limit]
(for [y y-range :let [i (+ li (* y d))]
x x-range :let [r (+ lr (* x d))]]
[x y (mandelbrot r i limit)]))
View gist:1333629
(defn- mandelbrot-seqs [n size a b limit]
(let [[lower upper] (normalize a b)
d (delta size lower upper)
[w h] (dimensions size lower upper)]
[w h
(for [[ly uy] (ranges h (int (/ h (min n h))))]
[ 0 ly w (- uy ly) (mandelbrot-seq (range 0 w) (range ly uy) lower d limit) ]
View gist:1333693
(defn- ranges [l s]
(partition 2 1 [l] (range 0 l s)))
View gist:1333743
(defn text-mandel [size a b limit]
(let [[w _ ms] (mandelbrot-seqs 1 size a b limit)
[_ _ _ _ m] (first ms)
s (map (fn [[_ _ i]]
(if (zero? i) " " (str (int (* 9 (/ i limit)))))) m)]
(interpose \newline
(map #(apply str %)
(partition w s))))))
View gist:1333795
(defn image-mandel
([size a b limit]
(let [[_ _ ms] (mandelbrot-seqs 1 size a b limit)
[_ _ image] (image-mandel-doseq (first ms) limit)]
([n size a b limit]
(let [[w h ms] (mandelbrot-seqs n size a b limit)
image (BufferedImage. w h BufferedImage/TYPE_INT_RGB)
raster (.getRaster image)]
View gist:1333859
(defn- image-mandel-doseq
([[dx dy w h m] limit]
(let [image (BufferedImage. w h BufferedImage/TYPE_INT_RGB)]
(image-mandel-doseq dx dy m image limit)))
([dx dy m ^BufferedImage image limit]
(let [raster (.getRaster image)]
(doseq [[x y rate] m]
(.setPixel raster (int (- x dx)) (int (- y dy)) (ints (rgb (angle rate limit)))))
[dx dy image])))
View gist:1334699
(defn render-image [image]
(let [out (ByteArrayOutputStream.)]
(do (ImageIO/write image "png" out) (ByteArrayInputStream. (.toByteArray out)))))
; ":use" needed for compojure.response
(extend-protocol Renderable
(render [image _]
(-> (ring.util.response/response (render-image image))
(ring.util.response/content-type "image/png"))))
PaulSandoz /
Last active Dec 12, 2015
This is an example of Conway's Game of Life using the OpenJDK lambda Stream API. This code works with build from the tip of the lambda repository as of the 1st February 2013. Since the API is a moving this code may not compile with future builds.
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.