-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
polish up the index and some exposition
- Loading branch information
Showing
19 changed files
with
458 additions
and
193 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Nature of Code | ||
# Nature of Code - Clojure | ||
|
||
[![License][license]][license-url] | ||
|
||
|
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
```clojure | ||
(ns background | ||
{:nextjournal.clerk/visibility {:code :hide}} | ||
(:require | ||
[nextjournal.clerk :as clerk])) | ||
``` | ||
|
||
# Background | ||
|
||
In addition to Clojure itself, I'm using a few stellar libraries that make this beautiful and readable in the browser. | ||
|
||
**Rendering**: I am using [Quil](http://quil.info/) for rendering. Quil is a Clojure wrapper around p5.js, so much of the rendering code is thankfully the same. | ||
|
||
**Static Publishing**: I am also using [Clerk](https://clerk.vision/) to render the sketches among prose. Clerk is a Clojure library that allows you to embed interactive Clojure code in a notebook. | ||
|
||
### Mutation-free Sketches | ||
|
||
Though I've tried to keep the code as close to the original as possible, Clojure is an immutable functional programming language, and my translation of the examples adhere to that property (I mean, that's what makes this a fun exercise in the first place!). | ||
|
||
You will always see at least three functions in each example | ||
|
||
```clojure | ||
^{:nextjournal.clerk/visibility {:code :show :result :hide}} | ||
(defn init-state | ||
"Calculate the initial state given the width and height of the sketch. | ||
This pure function is called once at the beginning of the sketch, and again | ||
whenever the sketch is restarted. The return value must be a map that contains the | ||
initial state of the sketch." | ||
[{:keys [width height]}]) | ||
|
||
^{:nextjournal.clerk/visibility {:code :show :result :hide}} | ||
(defn setup! | ||
"Setup initial canvas | ||
This function is called once at the beginning of the sketch, and again | ||
whenever the sketch is restarted, and it is called in the Quil context, so it | ||
can make drawing calls. The return value is ignored." | ||
[]) | ||
|
||
^{:nextjournal.clerk/visibility {:code :show :result :hide}} | ||
(defn tick | ||
"Update the state of the sketch | ||
This pure function is called once per frame. It takes the current state and returns | ||
the next state." | ||
[state]) | ||
|
||
^{:nextjournal.clerk/visibility {:code :show :result :hide}} | ||
(defn draw! | ||
"Draw the current state of the sketch. | ||
This function is called once per frame after the tick function. It takes the | ||
current state and draws it to the screen. The return value of this function is ignored." | ||
[state]) | ||
``` |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
```clojure | ||
(ns index | ||
{:nextjournal.clerk/visibility {:code :hide}} | ||
(:require | ||
[noc.render] | ||
[nextjournal.clerk :as clerk] | ||
[clojure.java.io :as io])) | ||
``` | ||
|
||
# Nature of Code in Clojure | ||
|
||
This is my ClojureScript implementation of the samples and exercises from [Nature of Code (2nd Edition)][noc2] by Daniel Shiffman. | ||
|
||
The book will be released in July 2024 but is available for pre-order now, [go check it out!][noc2] | ||
|
||
[noc2]: https://github.com/nature-of-code/noc-book-2 | ||
|
||
This project is a work in progress and isn't yet finished. I add to it when I find the time, which may not be often. | ||
|
||
|
||
### Why? | ||
|
||
I've been a fan of Daniel Shiffman's work (the 1st edition of Nature of Code, as well as his excellent [Coding Train](https://thecodingtrain.com/) for a long time. I've always wanted to work through the book. This is a great way to do it! | ||
|
||
Implementing it in Clojure lets me train my functional muscles and it's rather fun. | ||
|
||
### How? | ||
|
||
Though I've tried to keep the code as close to the original as possible, Clojure is an immutable functional programming language, and my translation of the examples adhere to that property. | ||
|
||
To learn how I've structured the code to avoid willy-nilly mutation or what tech I'm using to render all of this, check out the [background](background.md). | ||
|
||
## Table of Contents | ||
|
||
```clojure | ||
(clerk/html | ||
(into | ||
[:div.md:grid.md:gap-8.md:grid-cols-2.pb-8] | ||
(map | ||
(fn [{:keys [path preview title description]}] | ||
[:a.rounded-lg.shadow-lg.border.border-gray-300.relative.flex.flex-col.hover:border-indigo-600.group.mb-8.md:mb-0 | ||
{:href (clerk/doc-url path) :title path :style {:height 300}} | ||
[:div.flex-auto.overflow-hidden.rounded-t-md.flex.items-center.px-3.py-4 | ||
(when preview | ||
(noc.render/image-ext {:width "100%" :style {:object-fit "contain"}} | ||
preview))] | ||
[:div.sans-serif.border-t.border-gray-300.px-4.py-2.group-hover:border-indigo-600 | ||
[:div.font-bold.block.group-hover:text-indigo-600 title] | ||
[:div.text-xs.text-gray-500.group-hover:text-indigo-600.leading-normal description]]]) | ||
[{:title "Chapter 0" | ||
:preview (io/resource "assets/img/chapter0.png") | ||
:path "notebooks/chapter_0.clj" | ||
:description "Randomness"}]))) | ||
``` |
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.