Skip to content

Instantly share code, notes, and snippets.

@minimal
Last active August 29, 2015 14:06
Show Gist options
  • Save minimal/d432e2444f652b461671 to your computer and use it in GitHub Desktop.
Save minimal/d432e2444f652b461671 to your computer and use it in GitHub Desktop.
(ann get-from-state
(t/All [a b]
[(t/Option (t/Map b a)) (t/Option (t/Coll b)) -> (t/Option a)]))
(defn get-from-state [state keys]
(t/loop [acc :- (t/Option a) nil
keys :- (t/Option (t/Coll b)) keys]
(if-not (and state (seq keys))
acc
(recur (get state (first keys))
(rest keys)))))
(ann get-from-state2
[(t/Option (t/Map t/Any t/Any))
(t/Option (t/Coll t/Any)) -> (t/Option t/Any)])
(defn get-from-state2 [state keys]
(t/loop [acc :- (t/Option Any) nil
keys :- (t/Option (t/Coll t/Any)) keys]
(if-not (and state (seq keys))
acc
(recur (get state (first keys))
(rest keys)))))
(ann get-from-state-orig
[(U t/Any (t/Map t/Any t/Any))
(t/Option (t/Coll t/Any)) -> (t/Option t/Any)])
(defn get-from-state-orig [state keys]
(if (or (nil? state) (empty? keys) (not (map? state)))
state
(recur (get state (first keys))
(rest keys))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment