Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Created June 11, 2013 16:09
Show Gist options
  • Save mwmitchell/5758215 to your computer and use it in GitHub Desktop.
Save mwmitchell/5758215 to your computer and use it in GitHub Desktop.
(def s {:P1 {:queen [{:rate [1.0 0.0]}]
:king [{:rate [1.0 0.0] :strate [1.4 0.0]}]}
:P2 {:double []
:king [{:rate [1.0 0.0] :strate [1.4 0.0]} {:rate [1.0 0.0]}]}
:P3 {:double []}})
(into {} (for [[pc rooms] s
[rt rates] rooms
r rates
:let [st (:strate r)]
:when (seq st)]
{pc [{rt {:strate st}}]}))
@leifp
Copy link

leifp commented Jun 11, 2013

;;; similar, keeps same structure, I think
(apply merge-with merge
      (for [[pc rooms] s
            [rt rates] rooms
            :let [st (vec (filter :strate rates))]
            :when (seq st)]
        {pc {rt st}}))

@therrick
Copy link

Very nice. Thanks for the help with this. One problem is that the structure is actually nested one level deeper than I realized--there is a udicode key wrapping the partner codes.

My test structure looks like this...

{"A" {:PRT0 {:double [{:is_direct true
                              :nightly_rate [100.0 0.0]
                              :strikethrough_rate [120.0 0.0]}
                             {:is_direct true
                              :nightly_rate [110.0 0.0]
                              :strikethrough_rate nil}]
                    :queen [{:is_direct true
                             :nightly_rate [40.0 2.0]
                             :strikethrough_rate [50.0 2.0]}
                            {:is_direct true
                             :nightly_rate [60.0 2.0]
                             :strikethrough_rate [80.0 2.0]}]
                    :king [{:is_direct true
                            :nightly_rate [50.0 5.0]
                            :strikethrough_rate nil}]}}
        "B" {:PRT1 {:double [{:is_direct true
                              :nightly_rate [50.0 5.0]
                              :strikethrough_rate nil}]}}}

B and A.PRT0.king should be removed completely. A.PRT0.double should have just one rate and A.PRT0.queen should have two rates.

The code I have at this point looks like this...

(apply merge-with merge
           (for [[udicode pcodes] structured-rates
                 :let [new-pc
                       (apply merge-with merge
                              (for [[pc rooms] pcodes
                                    [rt rates] rooms
                                    :let [st (vec (filter :strikethrough_rate rates))]
                                    :when (seq st)]
                                {pc {rt st}}))]
                 :when (not-empty new-pc)]
             {udicode new-pc}))

Adding that extra level of nesting makes it look a lot less elegant than what you had. Not sure if there's a better way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment