Fork me on GitHub
#code-reviews
<
2020-12-29
>
phronmophobic20:12:13

looks pretty good. a few thoughts: • it seems like most-duis could be written in terms of most-prevalentmost-prevalent receives a file and immediately calls load-json. I would separate the data processing from the data loading. it's very common to want to load data from different sources and reuse the data processing functions • calculate_prevalence should probably be calculate-prevalencesort-by requires loading the full data set into memory. since you only need the 10 most prevalent values, most-prevalent could be re-written to keep at most 10 values in memory so that a larger than memory data set could be processed

roelof21:12:46

oke, and how could I do the last point

roelof21:12:33

the challemge said only to display the 10 most

roelof21:12:43

the given dataset is some 18 thousand entries

phronmophobic21:12:50

I think rewriting it so that the memory consumption is O(1) rather than O(n) is a good exercise.

phronmophobic21:12:27

if no solution comes to mind, I would try first figuring out 1. how to find the maximum element of a lazy sequence with O(1) memory 2. how to find the 2 largest elements ... 3. finally, how to find the n largest elements ...

roelof21:12:39

may I have then hints how to do so

roelof21:12:51

jus learning clojure for a week

🎉 3
phronmophobic21:12:54

the same techniques would apply in just about any language

roelof21:12:37

oke, give me then time to find the answer to the first question

roelof21:12:07

right now no idea how I can find that out

phronmophobic21:12:28

are you familiar with reduce?

roelof21:12:18

yep. I have learned that

roelof21:12:50

like (reduce + [1 2 3 4 5])

roelof21:12:36

to add up all items of a collection

roelof21:12:05

but it late here so if you do not mind im heading to bed

phronmophobic21:12:51

no problem. have a good night

roelof21:12:27

if you still wants , we can tomorrow talk on this when it's not so late here

roelof18:12:21

do you have time to learn me what you mean

phronmophobic18:12:44

I can answer a few questions

roelof18:12:14

you were talking I could make my code better

roelof18:12:43

step 1 was to find a answer to this question

roelof18:12:48

how to find the maximum element of a lazy sequence with O(1) memory

roelof18:12:01

and you were talking about reduce

roelof18:12:13

but then I needed to sleep

roelof18:12:34

do you mean I have to change map to reduce ?

phronmophobic18:12:24

the map in most-prevalent is fine

phronmophobic18:12:59

maybe just try try to write a function, find-max that takes a sequence as an argument and returns the maximum value

phronmophobic18:12:46

with respect to most-prevalent , the goal is to replace:

(sort-by :prevalance)
      (take-last 10)
eventually with something like:
(map :prevalence)
 (find-largest-elements 10)

roelof18:12:49

do I now need to use reduce or can I just use the method max ?

phronmophobic18:12:07

max would work, but the next goal is to try and find-max to be able to return the two largest elements rather than just the largest

roelof18:12:59

oke, I was thinking

(defn find-max [sequence] 
  (reduce max sequence)) 
   
untested

phronmophobic18:12:51

constant memory, and if given a lazy sequence,can process larger than memory data sets

phronmophobic18:12:31

now, can you improve the function to find the two largest elements?

roelof18:12:06

if I may use a helper function

👍 3
roelof18:12:32

`

(defn top-two [[big1 big2 :as acc] x]
  (cond
    (> x big1) [x big1]
    (> x big2) [big1 x]
    :else acc))

(defn find-two-max [sequence]
  (reduce top-two [0 0] sequencel))

phronmophobic18:12:59

looks like there's a bug

roelof18:12:39

then I have to test it

roelof18:12:51

wrote it right of my head

roelof18:12:02

this seems to work

(ns clojure.examples.hello
(:gen-class))

 (defn top-two [[big1 big2 :as acc] x]
  (cond
    (> x big1) [x big1]
    (> x big2) [big1 x]
    :else acc))

(defn find-two-max [sequence]
  (reduce top-two [0 0] sequence))

(print (find-two-max '(1 2 3 )))

roelof18:12:25

but it given (3 2) as answer and not (2 3)

phronmophobic18:12:51

try (top-two [1 2] 2)

roelof18:12:56

gives [ 2 1] as answer

phronmophobic18:12:03

the answer should be [2 2]

roelof19:12:49

then I think I need to make another arm to the cond

roelof19:12:26

(defn top-two [[big1 big2 :as acc] x]
  (cond
    (= x big2) [big2 big2 ]  
    (> x big1) [x big1]
    (> x big2) [big1 x]
    :else acc))

(defn find-two-max [sequence]
  (reduce top-two [0 0] sequence))

(print (top-two [1 2] 2))
gives now the right answer

roelof19:12:56

but this is not good when I want to have a variable number I think

phronmophobic19:12:01

there's still an issue:

(top-two [1 3] 2) ;; [2 1]

roelof19:12:37

oops more to investigate

roelof19:12:54

hmm, still in the dark why this is not working

phronmophobic19:12:50

(defn top-two [[big1 big2 :as acc] x]
  (cond
    (> x big1) [x big1]
    (> x big2) [big1 x]
    :else acc))
the structure is fine, but you should double check the returns

phronmophobic19:12:13

which cond branch is the test case taking?

roelof19:12:18

this one (> x big1) [x big1]

roelof19:12:41

and it schould return x and big2

👍 3
roelof19:12:11

yep, then it doing fine

phronmophobic19:12:41

ok, now how would you extend find-two-max to find-n-max?

roelof19:12:08

that I find difficult

roelof19:12:30

the only way I see it , it to use a collection that holds the highest numbers

😁 3
roelof19:12:05

but then it is possible that too much or too little numbers are in it because it is not fixed

roelof19:12:32

and I do not see how I can change the arms of the cond then

roelof19:12:00

so right now im thinking I do not have enough xp to solve this one

phronmophobic19:12:12

that's ok. sometimes it takes a little bit of time

phronmophobic19:12:18

to come up with an answer

phronmophobic19:12:34

especially if it's not similar to other problems you've worked on

roelof19:12:57

I think I need some sort of loop

roelof19:12:31

I can destruct in a parts because I do not know how many items the collection is

roelof19:12:42

or some sort of recursion

roelof19:12:58

I can destruct in a the first and the rest

roelof19:12:30

sorry, I do not see it

roelof19:12:19

get the feeling im close but miss a few pieces or do not know how the pieces fit together

phronmophobic19:12:18

it might make it easier if acc was sorted

roelof19:12:24

oke, acc is the outcome we wanted. Right?

roelof19:12:08

and I have to look what is the acc is when the parameter is a collection

phronmophobic19:12:16

I was referring to acc in top-two

roelof19:12:40

now I m totally confused

phronmophobic19:12:37

I guess top-two would need to be top-n

roelof19:12:06

I was thinking you wanted this :

(defn top-n [colllection x] ....] 

roelof20:12:24

maybe this better : (def topn [collection :as acc x] ......)

roelof20:12:40

if so, then I still do not see how the arms of the collection should be

roelof20:12:09

yep. the arms of the cond where I have to check so I can find the highest n numbers

phronmophobic20:12:30

so acc is the collection we're accumulating into that will contain the top n items. if acc was sorted, is there a way to check if a new value, x, should be added, replace a value, or not added?

roelof20:12:02

lets say we have now (1 2 3) and we have the number 5

roelof20:12:32

then we schould have (2 3 5) right ?

👍 3
roelof20:12:49

and if we have (2 3 5) and we have 1 nothing have to change

roelof20:12:08

so the middle is never changed

roelof20:12:34

thinking aloud to see if I can figure out how to do it in code

roelof20:12:26

no, I see how things schould be but not how to make it work in code

roelof20:12:01

It looks like im overthinking things

roelof20:12:33

hmm, I see a pattern

roelof20:12:40

when I have (1 2) and the number 3 I can compare it to the 2 and keep the 2 and add the 3

roelof20:12:06

when I have (1 5 9) and the number 8 I can compare it to the last in the collection and that is not true so nothing is changed then I can compare it to the second last one and that is true so replace that number with the given number

roelof20:12:39

so i looks I have to make a reversed loop for comparising

roelof20:12:07

when I have (1 5 7) and a 8 I can compare it again with the last one that is true so I replace it with the 8

phronmophobic20:12:17

there often built in data structures that will remain sorted as you add values. I can't think of one for clojure off the top of my head though, but there's probably something if I looked hard enough

roelof20:12:59

but then still I have to delete one item and add a item

phronmophobic20:12:10

I was thinking of something simple like: (->> (conj acc x) (sort) (take n))

phronmophobic20:12:46

it's not the most efficient implementation, but since acc is small, it's probably fine

roelof20:12:25

oke, but it could be very very big when someone wanted to hold the highest 50 or 100 or 1000 items

phronmophobic20:12:49

right. it depends on the use case

phronmophobic20:12:41

if I wanted to prepare for that use case, I would probably look for a data structure on https://www.clojure-toolbox.com/ (under data structures) to see if there's already an efficient data structure for that purpose

phronmophobic20:12:08

I would also investigate sorted-map

phronmophobic20:12:16

and then run benchmarks

roelof20:12:01

but I think we are going to far down the rabbit hole

roelof20:12:22

I can live with a collection of 1 - 20 items now

roelof20:12:04

oke, so now arms of a cond just the code you let me see

roelof20:12:30

I have to try if that could work

👍 3
roelof20:12:33

hmm. not what I expect when trying this in a online repl

(ns clojure.examples.hello
(:gen-class))

(defn top-n [[collection :as acc] x n]
  (->> (conj acc x) (sort) (take n)))

(print (top-n [1 2 ] 2 2))

roelof20:12:55

gives [1 2] where I expect [2 2]

roelof20:12:24

and when i do this :

(ns clojure.examples.hello
(:gen-class))

(defn top-n [[collection :as acc] x n]
  (->> (conj acc x) (sort) (take n)))

(print (top-n [1 2 ] 2 1))
I get 1 where I expect 2

roelof20:12:43

so or I do something wrong or the code is not working

roelof20:12:41

or do we have to add a reverse after the sort

👆 3
roelof20:12:38

yep, this is working

roelof20:12:52

(ns clojure.examples.hello
(:gen-class))

(defn top-n [[collection :as acc] x n]
  (->> (conj acc x) 
       (sort) 
       (reverse) 
       (take n)
       (reverse)
       ))

(print (top-n [1 5 9 ] 8 2))
gives me a (8 9)

roelof20:12:06

which is good and I like to see it this way

roelof20:12:38

so I can use this method/function to do what you wanted to do with replacing code

roelof20:12:48

I will try it tomorrow .

roelof20:12:55

Thanks for the lessons

phronmophobic20:12:03

have a good night 😄

roelof20:12:09

maybe clojure is a nicer language then I thought

roelof20:12:25

but a totally other one then ruby or c# or haskell

phronmophobic20:12:53

yep, it's definitely in a unique position compared to other languages

roelof20:12:19

I hope i will once be so profient that I can do AOC or exercism challenges

roelof20:12:30

and begin learning web development

roelof20:12:44

I have two projects in mind

roelof20:12:43

Can clojure work properly on Windows or can I better use linux for it

roelof20:12:48

if I get it working , I will begin with the brave book

roelof20:12:52

is too much for a beginner like me

roelof20:12:16

sorry, still one question about your feedback

roelof20:12:25

you wrote this :

I would separate the data processing from the data loading. it's very common to want to load data from different sources and reuse the data processing functions

roelof20:12:56

but I do it , file is only the filename. the loading and parsing to json is done in the load-json method

phronmophobic20:12:53

right, but most-prevalent is doing both loading and processing

phronmophobic20:12:04

and there's no way to do just the data processing

phronmophobic20:12:31

so if you had a different data source, you'd have to copy most of the code from most-prevanent

roelof21:12:43

sorry, I miss you

roelof21:12:26

you mean if I want to do the same with a file which do not have the same fields

roelof21:12:22

so you were talking about this part

(map (fn [county]
             {:county     (fips (fips-code county)),
              :prevalance (calculate_prevalance county field-name)
              :report-count (field-name county)
              :population  (:county_population county)
              }))
      (sort-by :prevalance)
and I thought you were talkimng about this part
(->> file
      load-json

roelof21:12:52

if so, I have to think how to solve that

roelof22:12:46

maybe a seperate method for that part

roelof09:12:35

chips, not working

roelof09:12:04

(defn top-n [[collection :as acc] x n]
  (->> (conj acc x) 
       (sort) 
       (reverse) 
       (take n)
       (reverse)
       ))

(defn most-prevalent
  "Given a JSON filename of UCR crime data for a particular year, finds the
  counties with the most DUIs."
  [file field-name]
 (->> file
      load-json
      (map (fn [county]
             {:county     (fips (fips-code county)),
              :prevalance (calculate_prevalance county field-name)
              :report-count (field-name county)
              :population  (:county_population county)
              }))
      (map :prevalance)
      (top-n 10)
      (reverse)))


(clojure.pprint/print-table (most-prevalent "2008.json" :auto_thefts))
Error:
; Execution error (ArityException) at ground-up.chapter7/most-prevalent (form-init17204185006522729914.clj:70).
; Wrong number of args (2) passed to: ground-up.chapter7/top-n

roelof22:12:58

Can you help me figure out what I did wrong ?

phronmophobic22:12:32

you need a reduce somewhere

phronmophobic22:12:51

you need to have a find-top-n that uses reduce and top-n

phronmophobic22:12:30

and you'll want find-top-n to have the collection as the last argument so that it works with ->> in most-prevalent

roelof22:12:05

oke, I will think about that next year

😉 3
roelof22:12:09

in 2 min i s here 2021

🎉 3
phronmophobic23:12:09

happy new year over there!

roelof23:12:26

thanks you too

phronmophobic23:12:27

i've still got 9 more hours of 2020 😕

roelof23:12:24

here it is now 00:04 so 2020 is over with a lot of corona lockdowns and so on

roelof23:12:32

I hope 2021 will be better

🤞 3
roelof23:12:27

think im given up on this : Thought about this :

(def find-top-n [n]
  (reduce ((top-n n))))

roelof23:12:46

but I need then another argument. the collection which I do not have

roelof23:12:10

`

(defn most-prevalent
  "Given a JSON filename of UCR crime data for a particular year, finds the
  counties with the most DUIs."
  [file field-name]
 (->> file
      load-json
      (map (fn [county]
             {:county     (fips (fips-code county)),
              :prevalance (calculate_prevalance county field-name)
              :report-count (field-name county)
              :population  (:county_population county)
              }))
      (map :prevalance)
      (find-top-n 10)
      (reverse)))

phronmophobic23:12:16

(def find-top-n [n coll]
  (reduce (fn [acc x] (top-n acc x n)
           coll)))

phronmophobic23:12:31

I haven't tested, but I think something like that should work

roelof23:12:54

nope, it does not work

roelof23:12:58

; Execution error (ArityException) at ground-up.chapter7/find-top-n (form-init4053567356004708660.clj:58).
; Wrong number of args (1) passed to: clojure.core/reduce

phronmophobic23:12:38

that's what I get for not typing into a repl

phronmophobic23:12:51

(def find-top-n [n coll]
  (reduce (fn [acc x] (top-n acc x n))
           coll))

phronmophobic23:12:55

do those parens match?

phronmophobic23:12:26

i'm blind without my repl

roelof23:12:20

Im going to sleep,

roelof23:12:41

no a wierd error message :

; Execution error (UnsupportedOperationException) at ground-up.chapter7/top-n (form-init704639222239290302.clj:49).
; nth not supported on this type: Float

roelof23:12:13

we can look at it tomorrow or later

roelof23:12:23

really time to sleep here . it's now 00:38

phronmophobic23:12:59

ok, have a good night

roelof23:12:05

maybe you have your repl back

roelof23:12:18

happy new year to you

roelof16:01:22

if you have time and have a repl could you help me with this annoying one

roelof20:01:04

can we work on ther code which is still not working

roelof20:01:13

or are you busy or do not have a repl

phronmophobic20:01:38

I can answer some questions

phronmophobic20:01:00

what's the code look like now?

roelof20:01:19

(ns ground-up.chapter7 (:require [cheshire.core :as json] [clojure.pprint]))

(defn load-json
  "Given a filename, reads a JSON file and returns it, parsed, with keywords."
  [file]
  (json/parse-string (slurp file) true))


(def fips
  "A map of FIPS codes to their county names."
  (->> "fips.json"
       load-json
       :table
       :rows
       (into {})))


(defn fips-code
  "Given a county (a map with :fips_state_code and :fips_county_code keys),
   returns the five-digit FIPS code for the county, as a string."
  [county]
  (str (:fips_state_code county) (:fips_county_code county)))

(defn calculate_prevalance
  [county field-name]
  ( if (zero? (:county_population county))
         0
    (float (/ (field-name county) (:county_population county)))))
    

(defn most-duis
  "Given a JSON filename of UCR crime data for a particular year, finds the
  counties with the most DUIs."
  [file]
 (->> file
      load-json
      (map (fn [county]
             {:county     (fips (fips-code county)),
              :prevalance (calculate_prevalance county :driving_under_influence)
              :report-count (:driving_under_influence county)
              :population  (:county_population county )
              }))
      (sort-by :prevalance)
      (take-last 10)
      (reverse)))

(clojure.pprint/print-table (most-duis "2008.json"))

(defn top-n [[acc] x n]
  (->> (conj acc x) 
       (sort) 
       (reverse) 
       (take n)
       (reverse)
       ))

(defn find-top-n [n coll]
  (reduce (fn [acc x] (top-n acc x n))
           coll))


(defn most-prevalent
  "Given a JSON filename of UCR crime data for a particular year, finds the
  counties with the most DUIs."
  [file field-name]
 (->> file
      load-json
      (map (fn [county]
             {:county     (fips (fips-code county)),
              :prevalance (calculate_prevalance county field-name)
              :report-count (field-name county)
              :population  (:county_population county)
              }))
      (map :prevalance)
      (find-top-n 10)
      (reverse)))


(clojure.pprint/print-table (most-prevalent "2008.json" :auto_thefts))

roelof20:01:48

and it produces this error :

; Execution error (UnsupportedOperationException) at ground-up.chapter7/top-n (form-init704639222239290302.clj:49).
; nth not supported on this type: Float

roelof20:01:08

but we are not using nth anywhere

phronmophobic20:01:12

if you type *e

phronmophobic20:01:17

it should print the full stack trace

phronmophobic20:01:05

lots of sequence functions use nth under the hood

phronmophobic20:01:41

oh, I think I see it:

(defn top-n [[acc] x n]
  (->> (conj acc x) 
       (sort) 
       (reverse) 
       (take n)
       (reverse)
       ))

phronmophobic20:01:51

[acc] should just be acc

roelof20:01:24

nope, then I get another error :

; Execution error (ClassCastException) at ground-up.chapter7/top-n (form-init3018563272993570646.clj:50).
; class java.lang.Float cannot be cast to class clojure.lang.IPersistentCollection (java.lang.Float is in module java.base of loader 'bootstrap'; clojure.lang.IPersistentCollection is in unnamed module of loader 'app')

phronmophobic20:01:17

it seems like the issue is in top-n. do you have a guess as to what might be causing the error?

roelof20:01:51

not complete

roelof20:01:16

it looks like we are using something as a float where the compiler wants a collection

roelof20:01:41

thinking now to print acc x and n

👍 3
roelof20:01:57

to see what they exactly contains

roelof20:01:44

2.861667E-41.8142852E-4 nil10

roelof20:01:51

so x seems to be nill

roelof20:01:55

wonder why

phronmophobic20:01:45

this bug is also kinda tricky

phronmophobic20:01:04

it's because reduce isn't called with a an initial state

phronmophobic20:01:33

(defn find-top-n [n coll]
  (reduce initial-val 
          (fn [acc x] (top-n acc x n))
           coll))

roelof20:01:38

oke, I saw that coll in the find-top-n was also not right

phronmophobic20:01:50

with initial-val being the starting value for the reduce state

phronmophobic20:01:06

do you know what the initial val should be?

roelof20:01:31

doubt between 1 and 0

phronmophobic20:01:49

what type should acc be?

roelof20:01:19

so I tried

roelof20:01:34

(defn find-top-n [n coll]
  (reduce [0 0] 
          (fn [acc x] (top-n acc x n))
           coll))

phronmophobic20:01:19

it should probably just be []

roelof20:01:24

`; Execution error (ArityException) at ground-up.chapter7/find-top-n (form-init3018563272993570646.clj:58). ; Wrong number of args (2) passed to: clojure.lang.PersistentVector

phronmophobic20:01:42

args in the wrong order

phronmophobic20:01:57

(defn find-top-n [n coll]
  (reduce (fn [acc x] (top-n acc x n))
          [] 
           coll))

roelof20:01:49

; Execution error (IllegalArgumentException) at ground-up.chapter7/eval22508 (form-init3018563272993570646.clj:80).
; Don't know how to create ISeq from: java.lang.Float

roelof20:01:13

(ns ground-up.chapter7 (:require [cheshire.core :as json] [clojure.pprint]))

(defn load-json
  "Given a filename, reads a JSON file and returns it, parsed, with keywords."
  [file]
  (json/parse-string (slurp file) true))


(def fips
  "A map of FIPS codes to their county names."
  (->> "fips.json"
       load-json
       :table
       :rows
       (into {})))


(defn fips-code
  "Given a county (a map with :fips_state_code and :fips_county_code keys),
   returns the five-digit FIPS code for the county, as a string."
  [county]
  (str (:fips_state_code county) (:fips_county_code county)))

(defn calculate_prevalance
  [county field-name]
  ( if (zero? (:county_population county))
         0
    (float (/ (field-name county) (:county_population county)))))
    

(defn most-duis
  "Given a JSON filename of UCR crime data for a particular year, finds the
  counties with the most DUIs."
  [file]
 (->> file
      load-json
      (map (fn [county]
             {:county     (fips (fips-code county)),
              :prevalance (calculate_prevalance county :driving_under_influence)
              :report-count (:driving_under_influence county)
              :population  (:county_population county )
              }))
      (sort-by :prevalance)
      (take-last 10)
      (reverse)))

(clojure.pprint/print-table (most-duis "2008.json"))

(defn top-n [acc x n]
  (->> (conj acc x) 
       (sort) 
       (reverse) 
       (take n)
       (reverse)
       ))

(defn find-top-n [n coll]
  (reduce (fn [acc x] (top-n acc x n))
          [] 
           coll))


(defn most-prevalent
  "Given a JSON filename of UCR crime data for a particular year, finds the
  counties with the most DUIs."
  [file field-name]
 (->> file
      load-json
      (map (fn [county]
             {:county     (fips (fips-code county)),
              :prevalance (calculate_prevalance county field-name)
              :report-count (field-name county)
              :population  (:county_population county)
              }))
      (map :prevalance)
      (find-top-n 10)
      (reverse)))


(clojure.pprint/print-table (most-prevalent "2008.json" :auto_thefts))

phronmophobic20:01:08

it's not showing a line number for the error

phronmophobic20:01:28

is there a way to do something like eval-buffer?

phronmophobic20:01:51

usually that will fix the exception not showing a proper file and line number

roelof20:01:53

no idea if that is possible

roelof20:01:25

is line 80 now the culprit ?

roelof20:01:37

I use vs code with calva

phronmophobic20:01:59

line 80 might be it

phronmophobic20:01:03

what's line 80?

roelof20:01:10

the code gives now

roelof20:01:17

(0.0051150895 0.004174161 0.0036036037 0.0030321407 0.00243309 0.002319513 0.0018750526 0.0016433854 0.0015932024 0.0015475558)
nil

roelof20:01:20

print-table wants this :

Prints a collection of maps in a textual table. Prints table headings

roelof20:01:06

so we loose somewhere the rest of the data

roelof20:01:28

the old code printed the name, the prevelance, population and the number of dui

roelof20:01:52

hmm, I have to think now how we can hold the data

roelof20:01:13

it looks our find-top-n looses it

phronmophobic20:01:36

the extra info is discarded before find-top-n is called

phronmophobic20:01:46

do you see where?

roelof20:01:05

o, I deleted the map but that makes another error message

phronmophobic20:01:02

right, because find-top-n expects a list of comparables (like a list list of numbers)

roelof20:01:09

If I delete the (map : prevelance)``

roelof20:01:23

jthen I see this :

; Execution error (ClassCastException) at java.util.TimSort/countRunAndMakeAscending (TimSort.java:355).
; class clojure.lang.PersistentArrayMap cannot be cast to class java.lang.Comparable (clojure.lang.PersistentArrayMap is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')

phronmophobic20:01:52

you'll need to update find-top-n and top-n to accept a key function to sort with

phronmophobic20:01:59

check out the docs for sort-by

phronmophobic20:01:31

and see if you can think of a way to update find-top-n and top-n to also accept a keyfn

roelof20:01:56

oke, so I have to change (sort) by sort-by ?

phronmophobic20:01:57

that's a good start

roelof20:01:24

then I see this :

({:county TX, Kenedy, :prevalance 0.0051150895, :report-count 2, :population 391} {:county NM, Grant, :prevalance 0.004174161, :report-count 123, :population 29467} {:county OR, Gilliam, :prevalance 0.0036036037, :report-count 6, :population 1665} {:county OR, Sherman, :prevalance 0.0030321407, :report-count 5, :population 1649} {:county TX, Hudspeth, :prevalance 0.00243309, :report-count 8, :population 3288} {:county TX, Hall, :prevalance 0.002319513, :report-count 8, :population 3449} {:county MO, Jackson, :prevalance 0.0018750526, :report-count 1514, :population 807444} {:county TX, Refugio, :prevalance 0.0016433854, :report-count 12, :population 7302} {:county AK, Prince of Wales-Outer Ketchikan, :prevalance 0.0015932024, :report-count 3, :population 1883} {:county MD, Baltimore city, :prevalance 0.0015475558, :report-count 982, :population 634549})
nil

roelof20:01:10

he, the names has changed, not good

roelof20:01:05

it sorts on name not on prevelance

roelof20:01:21

|      :county |  :prevalance | :report-count | :population |
|--------------+--------------+---------------+-------------|
|  AL, Autauga |  2.861667E-4 |            15 |       52417 |
|  AL, Baldwin | 1.8142852E-4 |            32 |      176378 |
|  AL, Barbour | 1.4354926E-4 |             4 |       27865 |
|     AL, Bibb |  9.219989E-5 |             2 |       21692 |
|   AL, Blount | 1.9146418E-4 |            11 |       57452 |
|  AL, Bullock |          0.0 |             0 |       10705 |
|   AL, Butler |  3.988036E-4 |             8 |       20060 |
|  AL, Calhoun |  5.771285E-4 |            67 |      116092 |
| AL, Chambers | 4.6220067E-4 |            16 |       34617 |
| AL, Cherokee |  8.107998E-5 |             2 |       24667 |

roelof20:01:47

when I do this :

(defn top-n [acc x n]
  (->> (conj acc x) 
       (sort-by :prevelance) 
       (reverse) 
       (take n)
       (reverse)
       ))

roelof20:01:39

this is not funny anymore

phronmophobic20:01:48

this list is different:

({:county TX, Kenedy, :prevalance 0.0051150895, :report-count 2, :population 391} {:county NM, Grant, :prevalance 0.004174161, :report-count 123, :population 29467} {:county OR, Gilliam, :prevalance 0.0036036037, :report-count 6, :population 1665} {:county OR, Sherman, :prevalance 0.0030321407, :report-count 5, :population 1649} {:county TX, Hudspeth, :prevalance 0.00243309, :report-count 8, :population 3288} {:county TX, Hall, :prevalance 0.002319513, :report-count 8, :population 3449} {:county MO, Jackson, :prevalance 0.0018750526, :report-count 1514, :population 807444} {:county TX, Refugio, :prevalance 0.0016433854, :report-count 12, :population 7302} {:county AK, Prince of Wales-Outer Ketchikan, :prevalance 0.0015932024, :report-count 3, :population 1883} {:county MD, Baltimore city, :prevalance 0.0015475558, :report-count 982, :population 634549})

phronmophobic20:01:55

and is actually sorted on prevalance

phronmophobic20:01:15

where's the other list coming from?

roelof20:01:41

which other list

phronmophobic20:01:50

the list sorted by names?

roelof20:01:09

I found out make a typo

roelof20:01:37

used : prevelance in place of prevalance

roelof20:01:50

so I think the code is working again

roelof20:01:15

this looks better

roelof20:01:22

|                             :county |  :prevalance | :report-count | :population |
|-------------------------------------+--------------+---------------+-------------|
|                          TX, Kenedy | 0.0051150895 |             2 |         391 |
|                           NM, Grant |  0.004174161 |           123 |       29467 |
|                         OR, Gilliam | 0.0036036037 |             6 |        1665 |
|                         OR, Sherman | 0.0030321407 |             5 |        1649 |
|                        TX, Hudspeth |   0.00243309 |             8 |        3288 |
|                            TX, Hall |  0.002319513 |             8 |        3449 |
|                         MO, Jackson | 0.0018750526 |          1514 |      807444 |
|                         TX, Refugio | 0.0016433854 |            12 |        7302 |
| AK, Prince of Wales-Outer Ketchikan | 0.0015932024 |             3 |        1883 |
|                  MD, Baltimore city | 0.0015475558 |           982 |      634549 |
nil

roelof21:01:09

I only do not know if the data is allright

roelof21:01:14

but it looks well

roelof21:01:18

thanks

😁 3
roelof21:01:42

I hope I did not costs you too much with my stupid questions

roelof21:01:36

and not much people here have the time for feedback

roelof21:01:05

see still something wierd

roelof21:01:25

on prevelance I sayit must be a float

roelof21:01:37

(float (/ (field-name county) (:county_population county)))))

roelof21:01:55

but this is not a float 8.70322E-4

roelof21:01:11

ping any idea why this happens ?

roelof21:01:42

this one does it right :

(defn most-duis
  "Given a JSON filename of UCR crime data for a particular year, finds the
  counties with the most DUIs."
  [file]
 (->> file
      load-json
      (map (fn [county]
             {:county     (fips (fips-code county)),
              :prevalance (calculate_prevalance county :driving_under_influence)
              :report-count (:driving_under_influence county)
              :population  (:county_population county )
              }))
      (sort-by :prevalance)
      (take-last 10)
      (reverse)))

roelof21:01:16

and this one not :

(defn most-prevalent
  "Given a JSON filename of UCR crime data for a particular year, finds the
  counties with the most DUIs."
  [file field-name]
 (->> file
      load-json
      (map (fn [county]
             {:county     (fips (fips-code county)),
              :prevalance (calculate_prevalance county field-name)
              :report-count (field-name county)
              :population  (:county_population county)
              }))
      (find-top-n 10)
      (reverse)
      ))

phronmophobic21:01:29

what makes you think 8.70322E-4 is not a float?

roelof21:01:45

looks to me more scientific

phronmophobic21:01:50

I think there are just fewer responses because it's around the holidays

phronmophobic21:01:08

I think it's just printing it out differently

roelof21:01:26

one does display 0.008 and the other 8e-4

roelof21:01:42

yep, and I wonder why

roelof21:01:48

I want both to display the same

roelof21:01:54

if possible

phronmophobic21:01:31

how it's formatted depends on how you're printing it and what formatter is being used

phronmophobic21:01:54

if you care how it's formatted, you should explicitly format it

roelof21:01:22

so clojure.pprint/print-table can display two things different

phronmophobic21:01:52

> (type (float 8.70322E-4))
java.lang.Float
> (float 8.70322E-4)
8.70322E-4

roelof21:01:36

thanks, I let it be

roelof21:01:02

and tomorrow try to make the challenges of chapter2 of the brave book

roelof21:01:22

Thanks a lot with all the patience with me

roelof21:01:32

and I hope Im a good "student"

roelof21:01:08

why the smile ?

roelof21:01:19

I think im older then you 😛

roelof20:12:57

these are challenges from this page