Search a list of records: Difference between revisions

m (→‎AppleScriptObjectiveC: Corrected header level to group under AppleScript.)
Line 650:
Khartoum-Omdurman
4.58
</pre>
 
 
=={{header|Clojure}}==
In order to not rely on the input ordering of the data records, this example adds an "idx" value to the records, for purposes of being able to report the answer to the queries which rely on the original ordering. The data records are then re-sorted to be ordered by city name. This helps to make it clear that the query relying on sort-by-population is properly handled in the query itself.
 
<lang Clojure>(def records [{:idx 8, :name "Abidjan", :population 4.4}
{:idx 7, :name "Alexandria", :population 4.58}
{:idx 1, :name "Cairo", :population 15.2}
{:idx 9, :name "Casablanca", :population 3.98}
{:idx 6, :name "Dar Es Salaam", :population 4.7}
{:idx 3, :name "Greater Johannesburg", :population 7.55}
{:idx 5, :name "Khartoum-Omdurman", :population 4.98}
{:idx 2, :name "Kinshasa-Brazzaville", :population 11.3}
{:idx 0, :name "Lagos", :population 21.0}
{:idx 4, :name "Mogadishu", :population 5.85}])
 
(defn city->idx [recs city]
(-> (some #(when (= city (:name %)) %)
recs)
:idx))
 
(defn rec-with-max-population-below-n [recs limit]
(->> (sort-by :population > recs)
(drop-while (fn [r] (>= (:population r) limit)))
first))
 
(defn most-populous-city-below-n [recs limit]
(:name (rec-with-max-population-below-n recs limit)))</lang>
 
{{out}}
<pre>
(city->idx records "Dar Es Salaam") ; 6
 
(most-populous-city-below-n records 5.0) ; "Khartoum-Omdurman"
 
(->> (sort-by :idx records)
(drop-while #(not (clojure.string/starts-with? (:name %) "A")))
first
:population) ; 4.58
</pre>
 
Anonymous user