Hi folks, I created the library: https://github.com/dawran6/emoji. Currently the emoji.core ns is about 100 loc. Feel free to drop your comments here or on the Github issues. Thank you! 😄
I don't think that would make a significant difference? There's only 2 levels of lazy sequence.
It might be a great chance for me to learn about transducers. I'll give it a try. Thanks @vincent.cantin , @dominicm
@dawran6 I recommend you this blog post serie on transducers: https://vincent.404.taipei/clojure/build-your-own-transducer-part1/
@dominicm transducers remove all the cost of the laziness and also remove intermediate buffering as well. For a library, it is often desirable.
Appreciate! I came across your blog post just a while ago. Now is a good time to practice it 🙂
I submitted this issue https://github.com/dawran6/emoji/issues/6
Feel free to ask in #code-reviews if you have any question on transducers, remember to tag me.
Absolutely!
or tag me in the issue if needed (@green-coder)
What would be a better way of doing this?
(defn filter-trip
[trip]
(cond
(= nil (:id trip)) false
(= nil (:tripData trip)) false
(= nil (:tenantId trip)) false
(= nil (-> trip :tripData :startLocation)) false
(= nil (-> trip :tripData :endLocation)) false
(= nil (-> trip :tripData :segments)) false
(= nil (:device trip)) false
(= nil (-> trip :device :id)) false
((contains? #{"BAD" "UNUSABLE"} (:accuracy trip))) false
:else trip))
I feel it's a bit clunky
@dharrigan Maybe this?
(defn legit-trip?
[{{:keys [startLocation endLocation segments]} :tripData
{:keys [id]} :device
:as trip}]
(and startLocation endLocation segments id
(every? trip [:tenantId :id])
(not (#{"BAD" "UNUSABLE"} (:accuracy trip)))))
Your fn seems to be a predicate
But if you want the trip to be returned in the truthy case (for use with keep
maybe), then you can wrap the and
expr in an if and return it.
nice, thank you - definitely looks sweeter 🙂
Both these versions return truthy even if :accuracy
isn't present in trip
. Is that intentional?
@dharrigan An alternative version, if the prev. one has too much destructuring for your taste 😄
(defn legit-trip?
[trip]
(and (not (some #(nil? (get-in trip %))
[[:id]
[:tenantId]
[:device :id]
[:tripData :startLocation]
[:tripData :endLocation]
[:tripData :segments]]))
(not (#{"BAD" "UNUSABLE"} (:accuracy trip)))))
ah, that was an oversight, if accuracy is not present, it should be false
I always um and ah about destructuring in function arguments - sometimes I like, sometimes...not so sure 🙂
@dharrigan A little destructuring is probably best. Here's one with "a little" destructuring
(defn legit-trip?
[{:keys [tripData device] :as trip}]
(not (or (some #(nil? (% trip)) [:id :accuracy :tenantId])
(some #(nil? (% tripData)) [:startLocation :endLocation :segments])
(nil? (:id device))
(#{"BAD" "UNUSABLE"} (:accuracy trip)))))
I'll stop tagging you now. Sorry if noise 😅
It's okay - really appreciate helpful feedback, I like to learn 🙂
Thanks and have a nice day!