Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@PaulSandoz
PaulSandoz / Life.java
Last active December 12, 2015 05:28
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.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.
*
(defn render-image [image]
(let [out (ByteArrayOutputStream.)]
(do (ImageIO/write image "png" out) (ByteArrayInputStream. (.toByteArray out)))))
; ":use" needed for compojure.response
(extend-protocol Renderable
BufferedImage
(render [image _]
(-> (ring.util.response/response (render-image image))
(ring.util.response/content-type "image/png"))))
(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])))
(defn image-mandel
([size a b limit]
(let [[_ _ ms] (mandelbrot-seqs 1 size a b limit)
[_ _ image] (image-mandel-doseq (first ms) limit)]
image))
([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)]
(dorun
(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)]
(println
(interpose \newline
(map #(apply str %)
(partition w s))))))
(defn- ranges [l s]
(partition 2 1 [l] (range 0 l s)))
(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) ]
)]))
(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)]))
(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))
rate)))
@PaulSandoz
PaulSandoz / gist:1333534
Created November 2, 2011 12:50
mandelbrot
(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))) )
rate))))