This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-06-04
Channels
- # aws (3)
- # beginners (63)
- # boot (10)
- # cljs-dev (14)
- # cljsrn (10)
- # clojars (4)
- # clojure (132)
- # clojure-nl (4)
- # clojure-spec (3)
- # clojure-uk (1)
- # clojurescript (206)
- # css (3)
- # datascript (5)
- # lein-figwheel (1)
- # leiningen (1)
- # off-topic (48)
- # re-frame (18)
- # reagent (29)
- # specter (4)
- # untangled (1)
- # yada (1)
in http://www.4clojure.com/problem/97
(= (map __ (range 1 6))
[ [1]
[1 1]
[1 2 1]
[1 3 3 1]
[1 4 6 4 1]])
Confuses me because its implying i should use map but i need the previous row to compute the next one…you don't use map, it uses map and you use some other function to get the right row for each number
@noisesmith understood, but to compute the current row, i need to pervious row, as per the instructions: > adding together adjacent numbers in the row above
sure, there are functions that work like this
map isn't one of them
I'm trying not to just solve this for you 😄
@vitruvia can't compare directly, since I haven't tried exercism, but I can say that 4clojure is terrific.
thanks @eggsyntax
(sequence nil) is weird
@gmercer I know you can grow a collection anyway you want with reduce
but i’m having trouble seeing how it makes sense to use map reduce ...
That seems a bit odd as map is implemented in terms of reduce.
Unless im missing something, most other solutions i saw did the same thing i did, which is just re-compute the entire tree each time just to get one slice of it.
just use () - it's allowed
@drewverlee map is not implemented in terms of reduce; the problem you shared is best solved with iterate, reduce, or loop, in that order
the fact that map gets called on it, and thus causes it to need to recalculate states, is just an unfortunate issue with the problem definition, and not really something your function needs to solve, but you can address it with memoize if you really want to
@noisesmith really? huh, i could have sworn i worked through an exercise where i created map using reduce, i just assume it was. > is just an unfortunate issue with the problem definition, and not really something your function needs to solve, but you can address it with memoize if you really want to Gotcha. I was stuck trying to refactor it to use map without re-doing the computation. I figured i could use memoization, but that seemed out of scope.
@drewverlee right you can implement anything that works on a collection and isn't lazy using reduce
but clojure's map is lazy, so it can't use reduce
why would your solution use map?
I don't see how map would help solve that problem
@noisesmith I was referring back to http://www.4clojure.com/problem/97, in case the conversations got overlapped.
no, I know exactly which problem you are talking about, map won't help with that one at all
I was referring to how the test itself made use of map:
(= (map __ (range 1 6))
[ [1]
[1 1]
[1 2 1]
[1 3 3 1]
[1 4 6 4 1]])
sure, right, that's demonstrating how your function responds to each numeric argument
it doesn't mean map would help implement your function
@vitruvia your code reverses the input because lists add to the front when you conj
if you used a vector instead of a list it wouldn't do that
I'm making a library where one can swap in different key-value stores. I'm thinking they should implement a protocol. The stores might be in memory, or they might be on the network, so the protocol needs to be able to handle asynchronicity, but I don't want to make people design around go blocks if they know their store is local. I'm looking for advice picking a flexible async abstraction. Here's where I've gotten on my own: if the protocol functions take a callback, it doesn't have to block anything, and it's easy enough to wrap it into a promise or channel, so maybe that's the best option? ^ is that sensible? what would you do? What if a kv-store was a specced map of functions rather than a protocol?
If you want polymorphism, a protocol is often a good solution, @amonks
I’d write the protocol as low-level as possible, so using callbacks rather than something like core.async
Then I’d write functions that could translate the low-level protocols into channels or promises or whatever.
(defprotocol KeyValue
(-get-value [store key callback]))
(defn get-value [store key]
(let [ch (a/promise-chan)]
(-get-value store key (fn [value] (a/put! ch value)))
ch))
Something like that for core.async.
If you have something with a callback, you can always translate it into channels, or Manifold’s deferreds, or whatever.
Ah, thanks 🙂
I was working on a javascript port a while back, I never quite finished but I'll ping you if I do 😉
@noisesmith Your solution to re-implementing map is breaking my circuit:
(def my-map
(fn MAP [f s]
(if (empty? s)
nil
(lazy-seq
(cons (f (first s))
(MAP f (rest s))))))
i would expect (my-map inc [])
to return nil
, but it returns []
.
Meanwhile (if (empty? []) nil "hi")
returns nil
returns nil here
yep, your right. sorry about that.
hello, I wonder why (take 5 (into [:a] (range))) never returns, but (take 5 (cons :a (range))) is OK ?
@drewverlee @noisesmith my solution uses a map
cpoix: vectors are eager.
hmm, ok. Thanks @noisesmith
hello. I would like to model a patient record with daily blood measurements. number of days (of measurements will grow when patient is in hospital). how to model such domain ?
the answer is almost always "use a hash-map"
later, if a hash map isn't good enough, there are easy upgrades depending on what you need
each day will also be a hash-map?
if it has multiple values associated yes, likely a vector of hashmaps under some key
İ see. thanks.
this page explains the differences https://github.com/clojure/clojurescript/wiki/Differences-from-Clojure - mostly it's the same