Fork me on GitHub

If I have a collection of collections [[1 2 3] [4 5 6]] How do I get that into just [1 2 3 4 5 6] I've tried

(map #(conj [] %) collection)
(map #(into [] %) collection) 
But those end up not quite doing what I want.


If that helps, remember that map gives you full control of the current item in a collection, so you can’t cross that limit and try to perform and operation that involves multiple index elements at once.. :) anything you return in your anonymous function will replace the current element at the current index you’re looping over and that’s it :)

👍 4
Alex Miller (Clojure team)03:12:11

(into [] cat [[1 2 3] [4 5 6]])

🤯 4

Thank you! and I think this is the first time I've used a transducer directly. I still haven't wrapped my mind around them.



user=> (reduce into [[1 2 3] [4 5 6] [7 8 9]])
[1 2 3 4 5 6 7 8 9]

🤯 8

I've been learning Clojure for about a month now. I'm at a point now where, when programming Java at work, I'm getting quite frustrated not having a REPL to quickly asses my logic :face_with_cowboy_hat:

👍 12
Adrian Smith11:12:02

Are there any plans to support jdk 13? Or does it already work?


Fwiw, I’ve used 13 since it came to Homebrew and haven’t had any problems.


@sfyire Im using Clojure 1.10 with jdk 13


no problems so far

Adrian Smith12:12:20

Thank you, I was guiding someone else through the process but discovered the jdk wasn't opening due to apple security constraints, at first it didn't appear in security and privacy

Adrian Smith12:12:31

but later on it was in there, and making an exception for it worked


Could anyone point me to a youtube video or something about how to TDD with the repl using cursive. meaning I would like to convert somehow my manual testing in the repl to a test.


@U04V70XH6 recorded his repl-driven development workflow to fix a bug using atom (chlorine) and rebl ( it's not intellij idea/cursive-specific (as is the repl-driven development workflow) but it should give you a general sense of what repl- and test-driven workflow feels like. i took a glimpse of the vimeo video you posted and i would say he's mostly just evaluating forms by sending them to the repl. i don't have cursive at the moment to look up the default key bindings for sending forms from the editor to the repl for evaluation ( but you should be able to find them in the intellij idea settings.


Thank u verry much


without the need of copy pasting etc. Its my understanding there is a way. or maybe a flow for doing this


This is kind of what im looking for


I cant see what keys he presses to acheive lots of what he is doing


BTW i got this video from this slack


and he doest convert it to an actual test. would love to do that

Janne Sauvala14:12:25

Hi, I have a vector of [100 0 0 0 0 ...]. I would like to transform it to [100 101 102 103 104 ...]. Any ideas how I could achieve this?


That sounds like a job for reduce

Janne Sauvala14:12:25

I also had a feeling I could do it with reduce. However, I couldn’t figure out a way how to do it :thinking_face:


I don’t really understand what the goal is here - how are the 0's related to the values at the same index of the other vector?

Janne Sauvala14:12:43

The 0's are just initial values


Would something like this work? Seems like you only need the initial value and the length, no?

(let [x (first xs)
      n (count xs)]
   (range 100 (+ x n)))


=> (take (count x) (iterate inc (first x)))
(100 101 102 103 104 105)

👍 8

Could also avoid having to count with:

(reductions (fn [m _] (inc m)) [100 0 0 0])


I like @UFUDSN6BE’s for readability. And you can probably move this upstream to never bother with the initial vector.


as usual, most of these are XY problems (at least when i ask them). <>

👍 4
👀 4
Janne Sauvala17:12:00

Thanks for the replies! @UFUDSN6BE your solution would definitely work. Unfortunately I forgot to give all the specs of the problem. I was looking for a way to use some function to do the transformation. @UCQL6E7PY’s solution is that kind of trick I was looking for.

Janne Sauvala17:12:14

I wanted to find the solution based on reductions because this kind of transformation would be easy to do in imperative/mutating language but I couldn’t figure out how to do it in more functional way.

Janne Sauvala18:12:20

So in a sense @U1Z392WMQ was right pointing on the XY problem 🙂

Michael J Dorian16:12:48

is there a pattern for creating a collection of all numbers that satisfy a predicate?


sounds kind of similar to idk if your use case is also similar?


Could you filter a range? (I'm a beginner too, so you may want to wait for a better answer)


(filter even? (range 1 10))

Michael J Dorian16:12:42

😁 yes, I think that's exactly what I am looking for, thanks!


what collection and what pred?

Michael J Dorian16:12:11

So the predicate is a function that checks weather a square on a game-board is empty and the collection would be coordinates. I think it's as simple as filtering a range for my case though, the only difference would be that I'm technically filtering pairs of numbers.

Michael J Dorian16:12:16

I want to basically pick empty squares from the board at random

Michael J Dorian16:12:06

and I figure it will be nicer code to choose a random element of a vector/set than to use iteration like I would in php or something


something like (->> board-squares (remove filled-in-squares) rand-nth)


if they are sets (rand-nth (set/difference positions filled-in-squares))

Michael J Dorian16:12:27

I see, so this way works with a collection of all squares as a starting point.


yeah. depending on how you are representing your state there are lots of ways. you could operate on the actual representation rather than a proxy like coordinates

Michael J Dorian16:12:47

yeah, the game-boards start out as json files, and I've been interacting with them mostly via coordinates, but the actually data structure is a vector, so that might be cleaner in this case

Eamonn Sullivan20:12:48

Hi all. Probably a dumb question, but I'm trying to use a React navigation-bar component (written in Javascript) that takes two sets of children (left and right) as props. I can't seem to figure this out. Here's the simplest case I could come up with: (defn right [] [:div "Right"]) (defn left [] [:div "Left"]) (defn navigation [] [:div.header_container [:> NavigationHeader {:leftChildren left :rightChildren right}]]) When I try to use this, I get a React error: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. I've tried (left) and [:left] too. What am I missing?


based on that error message, I'd suspect NavigationHeader is undefined


because "Left" and "Right" are strings, so shouldn't get that message

Eamonn Sullivan20:12:38

I've required it the same way I've required other components from the same library: ["@bbc/igm-navigation-header" :refer [NavigationHeader], but maybe this is set up differently. I'll take a look at the source.

Eamonn Sullivan20:12:39

Looks like a normal default export, and no different than the others. It's going to be a typo, probably.

Eamonn Sullivan21:12:13

Changing the import to ["@bbc/igm-navigation-header" :as NavigationHeader] (:as rather than :refer) changes the error message to Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {ns, name, fqn, _hash, cljs$lang$protocol_mask$partition0$, cljs$lang$protocol_mask$partition1$}). If you meant to render a collection of children, use an array instead.


do you mean import or require? these are different things


import doesn't support :as

Eamonn Sullivan21:12:34

I meant require. Sorry, JavaScript habits


with :as you are naming the entire lib, not just one thing in it

Eamonn Sullivan21:12:02

Got it. I needed to wrap left and right with (reagent.core/reactify-component ...)


I think it's a bit inconsistent that 'some' returns either true or nil. Is there are reason why it doesn't return false?


Some actually returns the result of the function you pass it or nil


user=> (some #{:a} [:a])


even? returns true false. (some even? [1 3]) returns nil


that is the 'or nil'


if you need false instead of nil, you probably aren't writing idiomatic clojure code


Inconsistent with predicate functions, most or all of which have a ? at the end of their name? some doesn't, of course, which would have been inconsistent with that function naming convention.


nil works fine in my code, just wondering why this was


There are many Clojure functions that can return nil for which it is an idiom to use them as conditional expressions, with the knowledge that nil is treated the same as false in that context.


Are you wondering about the origin of that practice? Or something more specific to some in particular?


Yeah it makes sense how you put it. So all predicates have true/false then?


I believe all functions in the core Clojure library whose name ends in ? return either true or false, but that naming convention is not enforced anywhere in the language.


Using nil to mean logical false originated with Common Lisp, and one or more flavors of Lisp that influenced Common Lisp before that.




So I should say "came into Clojure from the influence of Common Lisp", not "originated with Common Lisp".


true and false exist in Clojure primarily because of Java interop, according to early Rich Hickey talks on Clojure.


cl has nil but no false, scheme has #f but no nil, we get both false and nil and both act as "falsey" values in conditionals


also in cl () is just a sugar for NIL


From statements he made in some of those talks, it sounds like he would not have introduced true and false at all, if he could have figured out a clean way to do Java interop while converting Java false to/from Clojure nil in both directions, without missing any, but I suspect that would not be possible if nil is also used for Java null


If you are interested in those statements in particular, the early talks I am referring to are mostly from 2008, with transcripts and videos linked from here:


imo if a function returns true it should return false, if it returns an object then it should return nil (the non-presence of an object)


which afaik is how most clojure functions behave


With the sometimes-nagging detail that true and false are objects.

💯 4

And if you get deeply involved in Java interop, there are weirdnesses in both Java (and thus also in Clojure) involving freshly constructed Java Boolean false values treated as logical true in both languages. There be dragons, thankfully which rarely rear their ugly heads: <>


also java serialisation/deserialisation of booleans can yield some pretty surprising weirdness

Alex Miller (Clojure team)23:12:51

Default serialization does the right thing

Alex Miller (Clojure team)23:12:25

It’s usually people trying to be clever that do the wrong thing


yeah I guess it wouldn't have been java serialization. it would've been something weird used by mapreduce or apache beam or something like that