Fork me on GitHub
#clojure-losangeles
<
2020-05-14
>
nate02:05:26

Tonight's meetup was fun, thanks to all who attended!

โ 1
๐ 1
bocaj02:05:39

Did you record again? It's a bit early (putting kids to bed) and I enjoyed watching the last meeting after the fact

nate02:05:52

Yeah, it was recorded.

esp102:05:56

Ah, sorry i missed it! For some reason I was thinking it was going to start at 7:30 instead of 6 ๐

esp102:05:42

I even did the homework - hereโs the solution I came up with:

``````(def vowels
#{\a \A
\e \E
\i \I
\o \O
\u \U})

(defn nmin
"nil-safe min function: does not throw NullPointerException if any args are nil.
Returns min of a and b if both are non-nil.
If a or b is nil, returns the other non-nil value.
If both a and b are nil, returns nil."
[a b]
(if a
(if b
(min a b)
a)
b))

(defn nearest-vowels
"Given a string, returns a sequence indicating the distance from the nearest vowel of each character position."
[s]
(->> s
; initial pass over input string -> reversed seq of distance from previous vowel
(reduce (fn [rev-seq c]
(conj rev-seq
(if (get vowels c)
0
(when-let [d (first rev-seq)]
(inc d)))))
'())
; final pass over reversed seq: accumulate distance to next vowel (= distance from previous 0), compare -> seq of distance from closest vowel
(reduce (fn [[res-seq
d-next]
d-prev]
(let [d-next (if (= d-prev 0)
0
(when d-next
(inc d-next)))
d (nmin d-prev d-next)]
[(conj res-seq d)
d-next]))
['()
nil])
(first)))``````

nate03:05:20

Very cool @esp1 !

Mario C.16:05:34

I took a regex recursive route on this but wasn't happy with it. I'd love to see the solutions everyone came up with

Mario C.16:05:49

I saw a very clever solution and was like "how didn't I think of that!"

nate18:05:06

we worked through making a faster solution during the meetup