Fork me on GitHub
#beginners
<
2017-06-04
>
drewverlee00:06:41

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…

noisesmith00:06:35

you don't use map, it uses map and you use some other function to get the right row for each number

drewverlee00:06:28

@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

noisesmith00:06:45

sure, there are functions that work like this

noisesmith00:06:48

map isn't one of them

noisesmith00:06:36

I'm trying not to just solve this for you 😄

gmercer00:06:27

beginners are going to assume

reduce
would only make things smaller 😉

vitruvia00:06:04

any opinions on 4clojure vs exercism for practice? or maybe do both?

eggsyntax01:06:20

@vitruvia can't compare directly, since I haven't tried exercism, but I can say that 4clojure is terrific.

vitruvia02:06:27

thanks @eggsyntax

curlyfry10:06:54

@vitruvia I recommend sorting by difficulty on 4clojure

vitruvia10:06:17

@curlyfry thanks!

vitruvia14:06:34

wow I just realized my function reverses the list somehow, lol

noisesmith15:06:15

(sequence nil) is weird

drewverlee15:06:28

@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.

noisesmith15:06:28

just use () - it's allowed

noisesmith15:06:06

@drewverlee map is not implemented in terms of reduce; the problem you shared is best solved with iterate, reduce, or loop, in that order

noisesmith15:06:36

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

drewverlee15:06:54

@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.

noisesmith15:06:56

@drewverlee right you can implement anything that works on a collection and isn't lazy using reduce

noisesmith15:06:03

but clojure's map is lazy, so it can't use reduce

noisesmith15:06:21

why would your solution use map?

noisesmith15:06:41

I don't see how map would help solve that problem

drewverlee15:06:32

@noisesmith I was referring back to http://www.4clojure.com/problem/97, in case the conversations got overlapped.

noisesmith15:06:16

no, I know exactly which problem you are talking about, map won't help with that one at all

drewverlee15:06:21

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]])

noisesmith15:06:52

sure, right, that's demonstrating how your function responds to each numeric argument

noisesmith15:06:02

it doesn't mean map would help implement your function

noisesmith15:06:47

@vitruvia your code reverses the input because lists add to the front when you conj

noisesmith15:06:59

if you used a vector instead of a list it wouldn't do that

vitruvia15:06:50

thanks again!

amonks15:06:23

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?

weavejester15:06:48

If you want polymorphism, a protocol is often a good solution, @amonks

weavejester15:06:40

I’d write the protocol as low-level as possible, so using callbacks rather than something like core.async

weavejester15:06:09

Then I’d write functions that could translate the low-level protocols into channels or promises or whatever.

amonks15:06:54

awesome, thanks a lot!

weavejester15:06:07

(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))

weavejester15:06:25

Something like that for core.async.

weavejester15:06:59

If you have something with a callback, you can always translate it into channels, or Manifold’s deferreds, or whatever.

amonks16:06:18

that's kinda where I was leaning, and it feels great to get confirmation 🙂

amonks16:06:36

btw I'm a huge fan of Integrant

weavejester16:06:45

Ah, thanks 🙂

amonks16:06:06

I was working on a javascript port a while back, I never quite finished but I'll ping you if I do 😉

drewverlee16:06:26

@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

noisesmith16:06:09

returns nil here

drewverlee16:06:58

yep, your right. sorry about that.

cpoix18:06:08

hello, I wonder why (take 5 (into [:a] (range))) never returns, but (take 5 (cons :a (range))) is OK ?

john18:06:17

@drewverlee @noisesmith my solution uses a map

noisesmith18:06:32

cpoix: vectors are eager.

cpoix18:06:07

hmm, ok. Thanks @noisesmith

etemtezcan18:06:38

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 ?

noisesmith18:06:00

the answer is almost always "use a hash-map"

noisesmith18:06:15

later, if a hash map isn't good enough, there are easy upgrades depending on what you need

etemtezcan18:06:05

each day will also be a hash-map?

noisesmith18:06:41

if it has multiple values associated yes, likely a vector of hashmaps under some key

etemtezcan18:06:21

İ see. thanks.

vitruvia21:06:44

Is clojurescript's syntax the same as clojure's?