Created
June 11, 2013 16:09
-
-
Save mwmitchell/5758215 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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
commented
Jun 11, 2013
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