Fork me on GitHub
#beginners
<
2019-12-06
>
ScArcher03:12:42

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)
and
(map #(into [] %) collection) 
But those end up not quite doing what I want.

Lu07:12:25

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
ScArcher03:12:47

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.

andy.fingerhut06:12:30

Also

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

🤯 8
Gulli09:12:40

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?

Mattias11:12:39

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

sudakatux12:12:34

@sfyire Im using Clojure 1.10 with jdk 13

sudakatux12:12:39

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

sudakatux12:12:16

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.

salam16:12:39

@U04V70XH6 recorded his repl-driven development workflow to fix a bug using atom (chlorine) and rebl (https://www.youtube.com/watch?v=UFY2rd05W2g). 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 (https://cursive-ide.com/userguide/repl.html#interaction-with-the-editor) but you should be able to find them in the intellij idea settings.

sudakatux19:12:43

Thank u verry much

sudakatux12:12:05

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

sudakatux12:12:07

This is kind of what im looking for

sudakatux12:12:13

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

sudakatux12:12:36

BTW i got this video from this slack

sudakatux12:12:31

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?

Gerome14:12:02

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:

enforser14:12:47

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

ben14:12:45

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

enforser14:12:21

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

👍 8
enforser14:12:32

Could also avoid having to count with:

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

tschady16:12:18

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

tschady16:12:24

as usual, most of these are XY problems (at least when i ask them). <https://en.wikipedia.org/wiki/XY_problem>

👍 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?

ben16:12:00

sounds kind of similar to https://github.com/clojure/test.check idk if your use case is also similar?

ScArcher16:12:51

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

ScArcher16:12:43

(filter even? (range 1 10))

Michael J Dorian16:12:42

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

dpsutton16:12:33

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

dpsutton16:12:02

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

dpsutton16:12:52

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.

dpsutton16:12:39

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?

noisesmith20:12:00

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

noisesmith20:12:16

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.

noisesmith21:12:07

do you mean import or require? these are different things

noisesmith21:12:18

import doesn't support :as

Eamonn Sullivan21:12:34

I meant require. Sorry, JavaScript habits

noisesmith21:12:00

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

Gulli21:12:35

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

hiredman21:12:26

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

hiredman21:12:52

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

Gulli21:12:39

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

hiredman21:12:57

that is the 'or nil'

noisesmith21:12:10

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

andy.fingerhut21:12:26

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.

Gulli21:12:40

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

andy.fingerhut21:12:23

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.

andy.fingerhut21:12:50

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

Gulli21:12:25

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

andy.fingerhut21:12:27

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.

andy.fingerhut21:12:00

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

Gulli21:12:26

Interesting

andy.fingerhut21:12:40

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

andy.fingerhut21:12:14

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

noisesmith21:12:02

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

noisesmith21:12:24

also in cl () is just a sugar for NIL

andy.fingerhut21:12:18

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

andy.fingerhut21:12:42

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: https://github.com/matthiasn/talk-transcripts/tree/master/Hickey_Rich

bfabry21:12:25

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)

bfabry21:12:35

which afaik is how most clojure functions behave

andy.fingerhut22:12:12

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

💯 4
andy.fingerhut22:12:07

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: <https://clojuredocs.org/clojure.core/if>

bfabry23:12:32

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

bfabry23:12:32

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