Created
July 24, 2017 13:44
-
-
Save saskali/2ab16d41d3313e7cfd2429064092c67c to your computer and use it in GitHub Desktop.
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 color-sorting.core | |
(:require [reagent.core :as reagent])) | |
(def colors ["blue" "yellow" "green" "purple" "red"]) | |
(def color-row (reagent/atom ["blue" "yellow" "green"])) | |
(def sorted-color-row (reagent.ratom/reaction (sort @color-row))) | |
(defn square [color] | |
[:svg {:style {:background color | |
:width "20px" | |
:height "20px"}} | |
[:rect]]) | |
(defn sorted-colors [] | |
;; random generation for sequence of color cubes. | |
;; using reagent's reaction for the sorted sequence allows | |
;; repeated usage of `sorted-color-row` while only being calculated once | |
[:div | |
[:button {:on-click #(swap! color-row conj (->> colors count rand-int (get colors)))} "new color!"] | |
[:button {:on-click #(reset! color-row [])} "reset"] | |
[:p "original:\n" (map square @color-row)] | |
[:p "sorted:\n" (map square @sorted-color-row)] | |
[:p "reverse:\n" (reverse (map square @sorted-color-row))]]) | |
(reagent/render [sorted-colors] (.getElementById js/document "app")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment