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
(fn [s e] | |
(let [squares (apply str (take-while #(<= % e) (iterate #(* % %) s))) | |
filler (drop (count squares) (repeat (#(* % %) (Math/ceil (Math/sqrt (count squares)))) \*)) | |
elements (concat squares filler) | |
dimension (int (Math/sqrt (count elements))) | |
cardinal (- (* 2 dimension) 1) | |
canvas (vec (repeat cardinal "")) | |
start-pos (* 2 (quot (dec dimension) 2)) | |
path (take (count elements) (mapcat #(repeat (+ 2 (* 2 %)) (if (odd? %) dec inc)) (range))) | |
balance (fn [c] (#(apply str `(~@% ~@(interpose \ c) ~@%)) (repeat (/ (- cardinal (- (* 2 (count c)) 1)) 2) \ )))] |
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
;; ummels's solution to Squares Squared | |
;; https://4clojure.com/problem/138 | |
(fn [start end] | |
(letfn | |
[(squares [start end] | |
(take-while #(<= % end) (iterate #(* % %) start))) | |
(arrange [x] | |
(let [n (count x) | |
dim (int (Math/ceil (Math/sqrt n))) |
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
/** Implicit class for adding `|>` and `tap` operations to `AnyVal`. | |
* | |
* See `http://stackoverflow.com/a/11120847/839131`. | |
*/ | |
implicit class PipeAndTap[A](val x: A) extends AnyVal { | |
/** Applies `f` to the underlying value and returns the result. */ | |
def |>[B](f: A => B) = f(x) | |
/** Applies `f` to the underlying value `x` (presumably with side effects) | |
* and returns `x`. |
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
object Util { | |
/** Merges a collection of streams into a single stream. | |
* | |
* Returns a sorted stream if all input streams are sorted. | |
*/ | |
def mergeStreams[A](streams: Traversable[Stream[A]])(implicit ord: Ordering[A]): Stream[A] = { | |
val streams1 = streams.toList filterNot (_.isEmpty) sortBy (_.head) | |
if (streams1.isEmpty) | |
Stream.empty | |
else { |
NewerOlder