Skip to content
Browse files

Experimental: another `loc-tree` strategy

  • Loading branch information...
1 parent 015cfbf commit e3843d6b3fd63d38b6b2e781361f96bad83bc6c8 @ptaoussanis committed May 4, 2014
Showing with 37 additions and 27 deletions.
  1. +19 −14 src/taoensso/tower.clj
  2. +18 −13 src/taoensso/tower.cljs
View
33 src/taoensso/tower.clj
@@ -360,30 +360,35 @@
(def ^:private loc-tree
(let [loc-tree*
(memoize
- (fn [loc & [unpadded?]]
+ (fn [loc]
(let [loc-parts (str/split (-> loc locale-key name) #"[-_]")
loc-tree (mapv #(keyword (str/join "-" %))
- (take-while identity (iterate butlast loc-parts)))
- loc-tree-padded (into (vec (repeat (- 3 (count loc-tree)) nil))
- loc-tree)]
- (if unpadded? loc-tree loc-tree-padded))))]
- (encore/memoize* 40000 nil ; Also used runtime by translation fns
+ (take-while identity (iterate butlast loc-parts)))]
+ loc-tree)))
+ loc-primary (memoize (fn [loc] (peek (loc-tree* loc))))
+ loc-nparts (memoize (fn [loc] (count (loc-tree* loc))))]
+ (encore/memoize* 80000 nil ; Also used runtime by translation fns
(fn [loc-or-locs]
(if-not (vector? loc-or-locs)
- (loc-tree* loc-or-locs :unpadded) ; Build search tree from single locale
- (->> loc-or-locs ; Build search tree from multiple desc-preference locales
- (mapv loc-tree*)
- (apply encore/interleave-all)
- (filterv identity)
- (encore/distinctv)))))))
+ (loc-tree* loc-or-locs) ; Build search tree from single locale
+ ;; Build search tree from multiple desc-preference locales:
+ (let [primary-locs (->> loc-or-locs (mapv loc-primary) (encore/distinctv))
+ primary-locs-sort (zipmap primary-locs (range))]
+ (->> loc-or-locs
+ (mapv loc-tree*)
+ (reduce into)
+ (encore/distinctv)
+ (sort-by #(- (* 10 (primary-locs-sort (loc-primary %) 0))
+ (loc-nparts %)))
+ (vec))))))))
(comment
(loc-tree :en-US) ; [:en-US :en]
(loc-tree [:en-US]) ; [:en-US :en]
(loc-tree [:en-GB :en-US]) ; [:en-GB :en-US :en]
(loc-tree [:en-GB :en :en-US]) ; [:en-GB :en-US :en]
- (loc-tree [:en-GB :fr-FR :en-US]) ; [:en-GB :fr-FR :en-US :en :fr]
- (loc-tree [:en-US :fr-FR :fr :en :DE-de]) ; [:en-US :fr-FR :de-DE :en :fr :de]
+ (loc-tree [:en-GB :fr-FR :en-US]) ; [:en-GB :en-US :en :fr-FR :fr]
+ (loc-tree [:en-US :fr-FR :fr :en :DE-de]) ; [:en-US :en :fr-FR :fr :de-DE :de]
(time (dotimes [_ 10000] (loc-tree [:en-US :fr-FR :fr :en :DE-de]))))
(defn- dict-inherit-parent-trs
View
31 src/taoensso/tower.cljs
@@ -38,25 +38,30 @@
(def my-dict-inline (tower-macros/dict-compile {:en {:a "**hello**"}}))
(def my-dict-resource (tower-macros/dict-compile "slurps/i18n/utils.clj")))
-(def ^:private loc-tree ; Crossover (direct)
+(def ^:private loc-tree
(let [loc-tree*
(memoize
- (fn [loc & [unpadded?]]
+ (fn [loc]
(let [loc-parts (str/split (-> loc locale-key name) #"[-_]")
loc-tree (mapv #(keyword (str/join "-" %))
- (take-while identity (iterate butlast loc-parts)))
- loc-tree-padded (into (vec (repeat (- 3 (count loc-tree)) nil))
- loc-tree)]
- (if unpadded? loc-tree loc-tree-padded))))]
- (encore/memoize* 40000 nil ; Also used runtime by translation fns
+ (take-while identity (iterate butlast loc-parts)))]
+ loc-tree)))
+ loc-primary (memoize (fn [loc] (peek (loc-tree* loc))))
+ loc-nparts (memoize (fn [loc] (count (loc-tree* loc))))]
+ (encore/memoize* 80000 nil ; Also used runtime by translation fns
(fn [loc-or-locs]
(if-not (vector? loc-or-locs)
- (loc-tree* loc-or-locs :unpadded) ; Build search tree from single locale
- (->> loc-or-locs ; Build search tree from multiple desc-preference locales
- (mapv loc-tree*)
- (apply encore/interleave-all)
- (filterv identity)
- (encore/distinctv)))))))
+ (loc-tree* loc-or-locs) ; Build search tree from single locale
+ ;; Build search tree from multiple desc-preference locales:
+ (let [primary-locs (->> loc-or-locs (mapv loc-primary) (encore/distinctv))
+ primary-locs-sort (zipmap primary-locs (range))]
+ (->> loc-or-locs
+ (mapv loc-tree*)
+ (reduce into)
+ (encore/distinctv)
+ (sort-by #(- (* 10 (primary-locs-sort (loc-primary %) 0))
+ (loc-nparts %)))
+ (vec))))))))
(defn make-t ; Crossover (modified)
[tconfig] {:pre [(map? tconfig) ; (:dictionary tconfig)

0 comments on commit e3843d6

Please sign in to comment.
Something went wrong with that request. Please try again.