Fork me on GitHub
#beginners
<
2020-07-25
>
bnstvn05:07:12

is there a variant of map in core or so which runs till the longest sequence ends? (maybe with an extra function which provides for the missing values, or just supporting mapping functions with all of the arities?)

andy.fingerhut05:07:21

I am nearly certain there is no such variant of map in core.

bnstvn05:07:49

thanks! must be easy to write one for myself

seancorfield05:07:47

@UGSM2S2CS I'm curious as to what the mapped function should be passed in the case of a shorter sequence argument "running out"?

seancorfield05:07:32

(long-map (fn [a b] ???) [1 2 3] [:a :b :c :d]) -- what would a be when b is passed :d?

dpsutton05:07:09

Also watch out if one of the sequences is infinite. I imagine no core function exists because of these boundary conditions.

bnstvn06:07:12

not sure really, maybe just a nil or provided default value? (suppose the sequences are homogenous) anyway, just learning this, and the “right padding all vectors with default” was pretty ugly so thought im missing something

seancorfield06:07:22

I guess I'd push back and ask what problem you're trying to solve? How have you gotten into a situation where this is the function you think you need? @UGSM2S2CS

seancorfield06:07:16

How many arguments to map do you have? Do you know in advance which are the shorter sequences (and why)?

bnstvn06:07:04

trying to sum up vector of vectors eg. (apply map + [[1 2 3 4] [10 20 30]])

seancorfield06:07:30

(FWIW, I don't think I've ever needed this function in a decade of production Clojure so I'm genuinely curious)

bnstvn06:07:31

and wanted 11 22 33 4 instead of 11 22 33

bnstvn06:07:01

yes, im probably missing something 🙂

seancorfield06:07:48

apply map + doesn't look very idiomatic so can you take a step back and explain what problem you're trying to solve?

seancorfield06:07:32

(I'm out for the evening, but if you answer while I'm gone, I'll respond when I'm back)

bnstvn08:07:40

Thank you. Actually not even sure how I got here, but what I got is a list of varying-length int vectors where I wanted to sum the rows.

(summ [[1 2 3 4 5] [6 7 8 9] [10 11 12]]) ;=> [17 20 23 12 5]

bartuka12:07:18

one way I would approach this problem (without understanding better the actual context) is to explicitly fix the sizes of the arrays

(defn summ [args]
  (let [longest (apply max (map count args))
        fix-size (fn [arr] (concat arr (repeat 0)))]
    (apply map + (map #(if-not (= longest (count %)) (fix-size %) %) args))))

lemuel16:07:04

Is it worth learning Java before getting into Clojure/ClojureScript? My background is in JS/Ruby/PHP and I have had very little exposure to Java so far

bartuka16:07:44

great advice I once got from here. "Better way to learn Clojure is by learning Clojure" hehehe

jsn16:07:49

@mail188 I think it's good to know at least enough Java to be able to read / write interop code freely. This way you'll have access from your clojure code to many many things that Java and thousands of available Java libraries provide.

✔️ 6
lemuel17:07:52

Thanks 🙂 I’ll give Clojure a go and see where I get. I’m sure it can’t hurt to learn a little Java over time

Giovani Altelino17:07:25

IMO, no need to learn Java, at least in my experience from small projects, you will hardly need to use interop for Clojure, it's mostly for Clojurescript and in this case you will use Javascript which you already know.

Quentin Le Guennec17:07:00

yeah no need to learn java, clojure is a language on its own

seancorfield18:07:36

@mail188 About the only "Java" thing you'll really need to learn to deal with is reading big stack traces 🙂

seancorfield18:07:00

(you will need to learn some of the JVM ecosystem concepts, like classpath, but you can mostly learn those in the context of Clojure alone, as and when they crop up)

lemuel18:07:38

ah I think that’s where I was getting confused (between Java/JVM). Excellent, that means I can keep my focus on Clojure 👍

seancorfield18:07:39

@mail188 A lot of Rubyists have come to Clojure and like it enough to stay 🙂

💯 3
seancorfield18:07:26

(my arc was from C++ to Java to Groovy to Scala to Clojure -- with some ColdFusion on the side along the way)

lemuel19:07:57

Nice journey! I’ve mostly worked with JS on the front-end so quite excited about ClojureScript. I’ve been getting more interested in functional programming but frustrated that the languages I’ve used are too loose/inconsistent

jsn19:07:44

@seancorfield so it was your first lisp then?

seancorfield20:07:29

I did quite a lot of Lisp and FP at university in the early '80s. My PhD was on the design and implementation of FP languages so I wrote a Lisp and used it to write various FP languages on top. But there were no jobs in FP so I did C and assembler professionally (and some COBOL), then started my OOP journey with C++ in '92.

seancorfield20:07:03

I had hoped that Haskell would take the world by storm when it appeared in the early '90s but that turned out to be wishful thinking 🙂

sova-soars-the-sora19:07:27

hello, i have two separate projects (folders separated) that i'd like to share a .cljc file between. i created a symbolic link to the file using ln -S sourcefile newlinknameorlocation and just about everything worked perfectly except... the namespace declaration (ns some.nested.folders ) did not transfer easily, so i don't know how to go about it trying to share one cljc file between two projects

noisesmith19:07:56

how about either making it a third library that both depend on, or giving it a path that works for both projects (and ns to match)

noisesmith19:07:21

a namespace prefix shouldn't be dictated by file path, the file path should be dictated by the namespace prefix

Stuart19:07:09

anyone know how i can sort the following vector

[[\h 1] [\a 1] [\d 2] [\r 2]]
So that it sorts by the second value in each sub vector descending, then the first value in each vector ascending? Best i have so far is (sort-by (juxt second first) xs) But then that returns : ([\a 1] [\h 1] [\d 2] [\r 2]) When I'd like to get back: ([\d 2] [\r 2] [\a 1] [\h 1]) I think I need to use the (sort-by comparater func xs) but I can't get it working over multiple columns

Stuart19:07:30

I thinK I Just need to write my own sorting function dont I?

jsn19:07:33

(defn cmp [[a0 a1] [b0 b1]] (let [c (compare b1 a1)] (if (zero? c) (compare a0 b0) c)))

👍 3
sova-soars-the-sora19:07:51

@noisesmith like making it its own project and then importing it to the other 2?

noisesmith19:07:11

right, that's the principled solution, the other suggestion is a hack that would at least work (as would load-file with an explicit file path)

Stuart19:07:00

And I use that as the comparer function to sort not sort-by, cheers!

Stuart19:07:24

So If I'm understanding that, compare returns 0, 1 or -1. You compare the second item in the second vector with the second item in the first vector, if that's zero (i.e. they are the same, compare the first items) And you use compare x y and compare y x to do > or <

jsn20:07:14

except it's not 1/-1, can be any number, I think user=> (compare \a \c) -2