This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-09-22
Channels
- # beginners (104)
- # bitcoin (1)
- # boot (5)
- # clara (3)
- # cljs-dev (14)
- # cljsjs (5)
- # cljsrn (1)
- # clojure (242)
- # clojure-italy (17)
- # clojure-news (13)
- # clojure-norway (3)
- # clojure-russia (101)
- # clojure-spec (41)
- # clojure-uk (87)
- # clojurescript (38)
- # core-async (38)
- # cursive (6)
- # datomic (11)
- # defnpodcast (3)
- # docs (14)
- # editors (8)
- # events (1)
- # fulcro (7)
- # hoplon (25)
- # leiningen (4)
- # luminus (7)
- # off-topic (25)
- # onyx (1)
- # portkey (14)
- # random (1)
- # re-frame (7)
- # reagent (4)
- # rum (4)
- # schema (8)
- # shadow-cljs (257)
- # spacemacs (10)
- # specter (4)
- # unrepl (3)
- # yada (1)
Cosa usereste voi per risolvere il seguente?
(def input
[["FooYep"]
["BlahBlah:" "FooFoo"]
["BazBaz" "PatPat"]
["TypeA"]
["Foo1" "East" "North" "South"]
["10.0" "11.0" "12.0" "13.0"]
["A1" "MDD"]
["ClassA" "34.97" "33.6" "34.5"]
["ClassB" "11.7" "11.4"]
["Foo" "MDD"]
["TypeA"]
["Foo1" "East" "North" "South"]
["10.0" "11.0" "12.0" "13.0"]
["A1" "MDD"]
["ClassA" "34.97" "33.6" "34.5"]
["ClassB" "11.7" "11.4"]
["ClassC" "11.7" "11.4"]])
Filtrare gruppi di vectors che matchano le seguenti regole: * 1 Vector di due elementi * Seguito da 1 o + vector che iniziano con str e poi nums
Expected:
[[["A1" "MDD"]
["ClassA" "34.97" "33.6" "34.5"]
["ClassB" "11.7" "11.4"]]
[["A1" "MDD"]
["ClassA" "34.97" "33.6" "34.5"]
["ClassB" "11.7" "11.4"]
["ClassC" "11.7" "11.4"]]]
@reborg mi sembra un buon use case per clojure.spec
Almeno per quanto riguarda il "pattern" matching
first cut
(let [v input]
(loop [res [] idx 0]
(if (= idx (count v))
res
(let [current (v idx)]
(if (= 2 (count current))
(let [more
(conj res
(loop [idx2 (inc idx) res [(v idx)]]
(if (and (string? (first (v idx2)))
(> (count (v idx2)) 2)
(re-find #"\d+" (or (second (v idx2)) "")))
(recur (inc idx2) (conj res (v idx2)))
res)))]
(if (> (count (first more)) 1)
(recur more (inc idx))
(recur res (inc idx))))
(recur res (inc idx)))))))
e' un pattern matching con look ahead. Primo loop vado sugli indici. Se trovo match iniziale, parto col secondo loop
qualcosa come
(s/def ::group-start (s/coll-of any? :kind vector? :count 2))
(s/def ::group-body-item (s/cat :start string? :rest (s/+ (every-pred #(re-matches #"\d+(\.\d+)?" %) string?))))
(s/def ::group-body (s/+ (s/spec ::group-body-item)))
(s/def ::group (s/cat :start ::group-start :body ::group-body))
per descrivere un singolo gruppo