Dutch national flag problem: Difference between revisions

(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 794:
print(sortedBalls2);
}</lang>
 
 
=={{header|Clojure}}==
<lang Clojure>(defn dutch-flag-order [color]
(get {:red 1 :white 2 :blue 3} color))
 
(defn sort-in-dutch-flag-order [balls]
(sort-by dutch-flag-order balls))
 
;; Get a collection of 'n' balls of Dutch-flag colors
(defn random-balls [num-balls]
(repeatedly num-balls
#(rand-nth [:red :white :blue])))
 
;; Get random set of balls and insure they're not accidentally sorted
(defn starting-balls [num-balls]
(let [balls (random-balls num-balls)
in-dutch-flag-order? (= balls
(sort-in-dutch-flag-order balls))]
(if in-dutch-flag-order?
(recur num-balls)
balls)))</lang>
 
{{out}}
<pre>
(def balls (starting-balls 20))
balls ; (:blue :red :red :blue :white :blue :white :blue :white :red
; :blue :red :white :white :white :red :blue :white :blue :blue)
 
(sort-in-dutch-flag-order balls) ; (:red :red :red :red :red :white :white :white :white :white
; :white :white :blue :blue :blue :blue :blue :blue :blue :blue)
</pre>
 
=={{header|D}}==
Anonymous user