This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-12-04
Channels
- # adventofcode (100)
- # announcements (7)
- # architecture (1)
- # aws (14)
- # beginners (209)
- # calva (30)
- # cider (5)
- # cljdoc (2)
- # cljs-dev (37)
- # cljsrn (2)
- # clojure (133)
- # clojure-dev (20)
- # clojure-finland (1)
- # clojure-italy (10)
- # clojure-nl (19)
- # clojure-spec (56)
- # clojure-uk (49)
- # clojurescript (57)
- # clojurex (8)
- # core-async (2)
- # core-logic (1)
- # cursive (38)
- # data-science (19)
- # datomic (28)
- # devcards (3)
- # duct (8)
- # emacs (28)
- # figwheel (1)
- # figwheel-main (31)
- # fulcro (2)
- # jobs (1)
- # kaocha (1)
- # klipse (2)
- # mount (6)
- # nrepl (43)
- # off-topic (20)
- # pathom (3)
- # pedestal (1)
- # re-frame (15)
- # ring-swagger (1)
- # shadow-cljs (47)
- # spacemacs (19)
- # sql (20)
- # tools-deps (58)
- # unrepl (13)
- # vim (5)
here is what i've tried so far
(reduce (fn [public-connections profile]
(if (:visibility profile)
(conj public-connections (:id number)))
[]
profiles))
the else
clause of your if expression should return the unchanged map, right now it's returning nil
public-connections
here is your accumulator value, you have to explicitly pass it to the next iteration
(reduce
(fn [public-connections profile]
(if (:visibility profile)
(conj public-connections (:id number))
public-connections)
[]
profiles))
(reduce
(fn [public-connections profile]
(if (:visibility profile)
(conj public-connections (:id number))
public-connections))
[]
profiles)
Is there guidelines on the largest size of a data structure used in an atom? Besides memory constraints?
If it fits in memory, you can put it "inside" an atom. An atom is just a pointer and a little bit of Java object overhead, and takes very little memory on its own.
while learning Clojure I seem to get back to basic questions on how to deal with data structures like sets, maps, and so on. Is there a cheat-sheet or a good introduction? What resources would you recommend?
(reduce + (map :copies-sold [book1 book2 book3])) Just adding the sum total of all books sold. How is this evaluated internally?. Do we need to worry about how many times the collection is internally looped ?
and by this does it extend to any level of nesting ?
map , folter , reduce all used in the same expression
or we can just assume that clojure will do better than if you were to loop and just write our map, filter and reduce
@nikola.kasev Have you seen this cheatsheet? https://clojure.org/api/cheatsheet That page links to this one which has tooltip variants showing function docs when you hover the mouse cursor over symbols: https://jafingerhut.github.io/
Look for the sections on collections
(depending on what you're actually looking for)
I have heard many people like the book "Clojure for the Brave and True" as an introduction to Clojure, too (free on-line I believe)
@gayathrik map
is lazy, so it will walk the collection "on demand" as reduce
walks through the data. That said, an intermediate sequence is constructed and then consumed so, technically, you are walking two sequences.
If you use the transducer form of this, you won't get that intermediate sequence (transduce (map :copies-sold) + 0 [book1 book2 book3])
@seancorfield thanks. Will read up on transduce.
You mean like this: (use-fixtures :once your-fixture)
I've created this file
(ns nukr.test-config
(:require [nukr.profiles-database :refer :all]
[taoensso.timbre :as timbre :refer [info]]))
(def nu-maflany {:id (swap! id-seq inc)
:firstname "Maflany"
:lastname "Traore"
:visibility true
:connections #{}})
(def nu-teca {:id (swap! id-seq inc)
:firstname "Teca"
:lastname "Alves"
:visibility true
:connections #{}})
(defn up-database
(info "Setting database")
(swap! profiless assoc :id nu-teca)
(swap! profiless assoc :id nu-maflany))
(defn down-database
(info "Resetting database")
(reset! profiless (hash-map))
(reset! id-seq 0))
(defn once-fixture [f]
(up-database)
(f)
(down-database))
(use-fixtures :once once-fixture)
If I have interpreted the docs correctly, when you call use-fixtures
in a namespace with tests, each test in that namespace is wrapped in your fixtures
Either each individually (`:each`) or all at once (`:once`)
if I have a namespace with tests in which I would like to use these fixtures, lets say some_tests.clj
, I would at the top, but below the requires, call use-fxtures
yes, in this case, it will be ran once before all tests in the namespace
Here is a blog by Stuart Sierra (author of clojure.test
) on a typical way that fixtures are used: https://stuartsierra.com/2016/05/19/fixtures-as-caches
ooooooooook thank you very much @lennart.buit 🙏
Not the best blogpost ever to explain them, but the first few paragraphs are helpful
to me it looks like somwhat overpowered switchcase which takes a function as it's argument and then bunch of results mapping to another functions taking the same argument as original dispatch function
You can only extend switch case if you own the code. If a library author uses a multimethod for extension points you can extend the behaviour without forking the library.
So it shines most when you care about having an open, extendable system. It's a bit less important if you own all the code and the things you need to switch on are very finite - in that case as you say, a cond
or case
will probably do the job.
Hi! How would I do a sort on a string so that it doesn’t default to alphabetical order, but rather in order of appearance? i.e. “Hello” -> “Hello” instead of “eHllo”.
@felicityfmn would it be a pain to provide another example? I haven't quite got what you meant from just that one
so, something like "Leeeeeeroy"
in problem 30 of 4clojure, when I do (sort (set (str/split "Leeeeeeroy")))
it comes out as "\e \L\o \r \y"
and I want it to be "\L \e \r \o \y"
i can't understand what is going on, i tried long time to understand but keep making no sense to me
after getiing those corresponding profile, i pass them to mapcat
which is supposed togaeher each profile connection and give me back a big array with all the ids
ah, @felicityfmn I think you'll need something different from a sort - you want to go through the string removing duplicates rather than sorting the whole thing
I’ll get to it then
(I'm guessing because it's a problem question you can't use dedupe
...)
(apply str (dedupe "Leeeeeroy"))
=> Leroy
😄
i never heard of dedupe
let me try
there are no restrictions on it in the problem so it might just work
@quieterkali would it be a problem to put your code for get-profiles-connections
and get-profile!
- might help spot the problem
(defn get-profile-connections
"Take a profile and return corresponding profile connections"
[profile]
(info "Calling get-profile-connections")
(let [{connections :connections} profile]
(into [] connections)))
(defn get-profile!
"Return profile by id"
[id]
(info "Calling get-profile! with ID: ", id)
(@profiles id))
hmm should work..
@quieterkali have you got some example failing data?
hmm, it’s giving me an "unable to resolve symbol: dedupe in this context"
there are no notes in the question saying dedupe is disabled?
(sometimes they do that, forcing you to reimplement).
Maybe it's an older version of clojure
dedupe
came in in 1.7
shame, it is useful and it works in my repl
@danieleneal moment please
still there @danieleneal?
[
{
"id": 7,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": []
},
{
"id": 1,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": [
4,
6,
3,
2,
5
]
},
{
"id": 4,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": [
1,
3
]
},
{
"id": 6,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": [
1,
3
]
},
{
"id": 3,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": [
1,
4,
6,
5
]
},
{
"id": 2,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": [
1
]
},
{
"id": 11,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": []
},
{
"id": 9,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": []
},
{
"id": 5,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": [
1,
3
]
},
{
"id": 10,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": []
},
{
"id": 8,
"firstname": "string",
"lastname": "string",
"visibility": true,
"connections": []
}
]
1 and 1 are friends. i want to give some friend sugestion to 2 base on 1 friends. so i take id 1 friends and i look up there friend one by one and see the number which high repetion, so that one is suppose to be the fisrt on the list
so you call your mapcat function with all of 1's friends
so
4,
6,
3,
2,
5
is the inputthen you'd expect 1,3,1,3,1,4,6,5,1,1,3
as the output from the mapcat
what are you getting instead?
ok let me try
(def profiles
[{"id" 7, "firstname" "string", "lastname" "string", "visibility" true, "connections" []}
{"id" 1, "firstname" "string", "lastname" "string", "visibility" true, "connections" [4 6 3 2 5]}
{"id" 4, "firstname" "string", "lastname" "string", "visibility" true, "connections" [1 3]}
{"id" 6, "firstname" "string", "lastname" "string", "visibility" true, "connections" [1 3]}
{"id" 3, "firstname" "string", "lastname" "string", "visibility" true, "connections" [1 4 6 5]}
{"id" 2, "firstname" "string", "lastname" "string", "visibility" true, "connections" [1]}
{"id" 11, "firstname" "string", "lastname" "string", "visibility" true, "connections" []}
{"id" 9, "firstname" "string", "lastname" "string", "visibility" true, "connections" []}
{"id" 5, "firstname" "string", "lastname" "string", "visibility" true, "connections" [1 3]}
{"id" 10, "firstname" "string", "lastname" "string", "visibility" true, "connections" []}
{"id" 8, "firstname" "string", "lastname" "string", "visibility" true, "connections" []}])
(defn get-profile [id]
(first (filter #(= (get % "id") id) profiles)))
(defn get-connections [id]
(-> (get-profile id)
(get "connections")))
(defn connection-frequencies [id]
(let [connections (get-connections id)]
(mapcat get-connections connections)))
this works for me
(connection-frequencies 1) => (1 3 1 3 1 4 6 5 1 1 3)
you could then use `frequencies in the last function too
(defn connection-frequencies [id]
(let [connections (get-connections id)]
(frequencies (mapcat get-connections connections))))
You've got the right shape of things - I'm just not sure where in your code there is probably some small mistake.... difficult to tell without being in front of it!
you can try maybe taking my code above which works for the sample data - and then changing get-profile to work with however you have them
I'm guessing it's coming from some kind of api?
it might be something simple like a mix up of keywords/strings ....
good luck!
sorry I couldn't help really
@danieleneal thank very much for your help 🙂
@danieleneal it was working the whole time
hahahah
Curious why there's no pipe
function in clojure, but there is a comp
? Thread macros aren't always the right fit.
Can you elaborate a bit please? If you’re thinking of pipes plus auto curried functions, the reason is it’s difficult/impossible to do with multi-arity and variadic functions
I'm only really worried about unary functions, though if comp handles something more, great
I guess one reason pipe isn’t in core might be it’s trivial to write, as you have shown 🙂
Sure, I'm just curious why comp would be in the standard library, when pipe wouldn't, since it's more readable. I mean, that's the whole idea behind threading macros
This is my guess: In cases where you have all the functions written out in front of you, threading works just as well: (-> x f g h) or (fn [x] (-> x f g h)) In the case where you are pipe-ing down a seq of functions, you lose the clarity pipe brings anyway
another thing to consider here, is that since transducers chain a transducing context rather than the data itself when first called, the transducer (comp (map f) (map g))
behaves like (map (comp g f) ...)
- reversing the effective order
Ohh @noisesmith that's interesting. I've noticed the same pattern with connect
ing multiple react components with react-redux (in javascript) — it's actually more intuitive to compose those with comp
than with pipe
, since you move down the tree in reverse the order of composition.
I bet a similar mechanism goes on under the hood (but I don't know react-redux at all)
Yeah, it's all about higher-order components, so you're creating a data structure by composing decorators, so the nesting reflects the composition
^^ this pattern shows up a lot in mathematics too. If you ever hear category theory people talk about duality, this direction reversal is what they mean
oh, awesome - thanks for that info
I have a function f with two parameters and a list like this ((1 2) (2 3))
. I'd like to map over the list and call the function. I do (map (fn [[one two]] (f one two)) <the list>)
. Is there a better way to apply the function for each pair in the list?
you could replace (fn [[one two]] (f one two))
with (partial apply f)
I'll let you decide which is clearer
regarding naming conventions, is it a convention to name functions with side-effects with a !
as a suffix? For example, swap!
uses it, and I was thinking specifically about API calls such as create-txt-record
might be better off being create-txt-record!
instead to signal the side-effect
it's a loose convention
there's also the io!
macro, which you can use to wrap code that does side effects, it gives an error if your code runs in a transaction (as transactions should be able to retry safely)
@noisesmith how does this work? partial is when a function is partially applied, but in my case I have all parameters
Rereading this, another thing I can point out is that you don't actually have all the parameters when that form is evaluated. map
doesn't accept a body of code to run, it needs some function (compared to eg. for which does use a body of code instead of a function arg)
partial returns a function, you can use it in place of fn, if your args will be tacked to the end of a function call
so we use apply to spread the input coll into args for f
and we use partial to create a function ready to apply f to some coll
(partial apply f)
is another way of saying (fn [& args] (apply apply f args))
or (fn [arg] (apply f arg))
for the one arg case only
user=> (apply + 1 2 3 [4 5 6])
21
Hi all, I'd like to practice pair programming. If you're interested in doing a 45min call with me, we can work on advent of code or a problem from exercism. I'll be on west coast US time. Email me at <mailto:[email protected]|[email protected]>.
@noisesmith got it, thanks a lot