-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcompress_js_hofs.rye
32 lines (23 loc) · 1.02 KB
/
compress_js_hofs.rye
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
; Third set of solutions is inspired by Simon's Clojure solution.
; Clojure function Partition seems like made for this problem,
; so I added it to Ryelang.
;
; (ns clojpression-puzzle
; (:require [clojure.test :refer [is]]))
;
; (def compress (partial transduce (comp (partition-by identity)
; (mapcat (fn [[c & _ :as cs]]
; [(count cs) c])))
; str))
;
; (defn run [& _]
; (is (= (compress "AAABBAAC") "3A2B2A1C")))
; using partition, map and join
compress-a: fn1 { .partition { , } |map { :a .length .concat first a } |join }
; using partition and fold
compress-b: fn1 { .partition { , } |fold 'acc "" { :a .length .concat first a |concat* acc } }
compress-c: fn1 { .partition { , } |fold 'acc "" { :a { acc length a first a } .eval .join } }
; assertions
"AAAAABBCCC" .compress-a .assert-equal "5A2B3C"
"AAAABBBCCC" .compress-b .assert-equal "4A3B3C"
"AAAABBCCCC" .compress-c .assert-equal "4A2B4C"