Skip to content

Instantly share code, notes, and snippets.

@codonnell
Last active January 7, 2018 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codonnell/27da48e74cc4dbc7d71b40fe6eb61e92 to your computer and use it in GitHub Desktop.
Save codonnell/27da48e74cc4dbc7d71b40fe6eb61e92 to your computer and use it in GitHub Desktop.
code kata parsing
(ns kata
(:require [clojure.string :as str]
[clojure.pprint :refer [pprint]]
[net.cgrand.xforms :as xf))
(def example-input "Los Angeles Clippers 104 Dallas Mavericks 88,New York Knicks 101 Atlanta Hawks 112,Indiana Pacers 103 Memphis Grizzlies 112, Los Angeles Clippers 100 Boston Celtics 12")
(defn score? [s]
(try (Integer. s)
true
(catch Throwable _
false)))
(defn parse-team
"Takes an input like ((\"Los\" \"Angeles\" \"Clippers\") (\"104\")) and returns
{:name \"Los Angeles Clippers\" :score 104}"
[[name-words [score]]]
{:name (str/join " " name-words) :score (Integer. score)})
(defn parse-game
"Takes an input like \"Los Angeles Clippers 104 Dallas Mavericks 88\" and
returns [{:name \"Los Angeles Clippers\" :score 104} {:name \"Dallas
Mavericks\" :score 88}]"
[s]
(->> (str/split s #" ")
(partition-by score?)
(partition 2)
(mapv parse-team)))
(defn split-games [s]
(str/split example-input #","))
(defn parse-ticker [s]
(mapv parse-game (split-games s)))
(defn -main [sports-ticker]
(pprint (parse-ticker sports-ticker)))
;; repl experimentation
;; (doc split-with)
;; -------------------------
;; clojure.core/split-with
;; ([pred coll])
;; Returns a vector of [(take-while pred coll) (drop-while pred coll)]
;; nil
;; user> (doc partition-by)
;; -------------------------
;; clojure.core/partition-by
;; ([f] [f coll])
;; Applies f to each value in coll, splitting it each time f returns a
;; new value. Returns a lazy seq of partitions. Returns a stateful
;; transducer when no collection is provided.
;; nil
;; user> (partition-by even? (range 10))
;; ((0) (1) (2) (3) (4) (5) (6) (7) (8) (9))
;; user> (partition-by #(= 0 (quot % 4)) (range 12))
;; ((0 1 2 3) (4 5 6 7 8 9 10 11))
;; user> (quot 8 4)
;; 2
;; user> (partition-by #(= 0 (rem % 4)) (range 12))
;; ((0) (1 2 3) (4) (5 6 7) (8) (9 10 11))
;; user> (def *example-input* "Los Angeles Clippers 104 Dallas Mavericks 88,New York Knicks 101 Atlanta Hawks 112,Indiana Pacers 103 Memphis Grizzlies 112, Los Angeles Clippers 100 Boston Celtics 12")
;;
;; Warning: *example-input* not declared dynamic and thus is not dynamically rebindable, but its name suggests otherwise. Please either indicate ^:dynamic *example-input* or change the name. (*cider-repl specter*:69)
;; #'user/*example-input*
;; user> (def example-input "Los Angeles Clippers 104 Dallas Mavericks 88,New York Knicks 101 Atlanta Hawks 112,Indiana Pacers 103 Memphis Grizzlies 112, Los Angeles Clippers 100 Boston Celtics 12")
;;
;; #'user/example-input
;; user> (require '[clojure.string :as str])
;; nil
;; user> (def game-strs (str/split example-input #","))
;; #'user/game-strs
;; user> (first game-strs)
;; "Los Angeles Clippers 104 Dallas Mavericks 88"
;; user> (def tokens (str/split game-strs #" "))
;; CompilerException java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to java.lang.CharSequence, compiling:(form-init4963496915355160444.clj:82:19)
;; user> (def tokens (map #(str/split % #" ") game-strs))
;; #'user/tokens
;; user> (first tokens)
;; ["Los" "Angeles" "Clippers" "104" "Dallas" "Mavericks" "88"]
;; user> (defn score? [s] (try (Integer. s) true (catch Throwable _ false)))
;; #'user/score?
;; user> (score? "20171126")
;; true
;; user> (score? "test")
;; false
;; user> (def token (first tokens))
;; #'user/token
;; user> (partition-by score? token)
;; (("Los" "Angeles" "Clippers") ("104") ("Dallas" "Mavericks") ("88"))
;; user> (partition 2 *1)
;; ((("Los" "Angeles" "Clippers") ("104")) (("Dallas" "Mavericks") ("88")))
;; user> (map (fn [[team score]] {:team (str/join " " team) :score (Integer. score)}))
;; #function[clojure.core/map/fn--5583]
;; user> (*1 *2)
;; #function[clojure.core/map/fn--5583/fn--5584]
;; user> (map (fn [[team score]] {:team (str/join " " team) :score (Integer. score)}) '((("Los" "Angeles" "Clippers") ("104")) (("Dallas" "Mavericks") ("88"))))
;; IllegalArgumentException No matching ctor found for class java.lang.Integer clojure.lang.Reflector.invokeConstructor (Reflector.java:183)
;; user> (map (fn [[team [score]]] {:team (str/join " " team) :score (Integer. score)}) '((("Los" "Angeles" "Clippers") ("104")) (("Dallas" "Mavericks") ("88"))))
;; ({:team "Los Angeles Clippers", :score 104} {:team "Dallas Mavericks", :score 88})
;; user> (map (fn [[team [score]]] {:name (str/join " " team) :score (Integer. score)}) '((("Los" "Angeles" "Clippers") ("104")) (("Dallas" "Mavericks") ("88"))))
;; ({:name "Los Angeles Clippers", :score 104} {:name "Dallas Mavericks", :score 88})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment