This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-11-21
Channels
- # beginners (165)
- # boot (9)
- # cider (9)
- # cljs-dev (5)
- # cljsjs (5)
- # clojars (4)
- # clojure (207)
- # clojure-brasil (1)
- # clojure-greece (3)
- # clojure-poland (2)
- # clojure-russia (6)
- # clojure-spec (85)
- # clojure-taiwan (1)
- # clojure-uk (53)
- # clojurescript (96)
- # community-development (2)
- # cursive (4)
- # datomic (14)
- # emacs (41)
- # events (2)
- # hoplon (184)
- # leiningen (1)
- # nginx (1)
- # off-topic (16)
- # om (7)
- # onyx (63)
- # pedestal (27)
- # planck (17)
- # protorepl (3)
- # rdf (9)
- # re-frame (62)
- # reagent (7)
- # ring-swagger (5)
- # schema (2)
- # spacemacs (5)
- # test-check (25)
- # untangled (93)
- # yada (3)
I've noticed core.reducers map does not let you map over multiple collections. How would you achieve this then: (r/map + (range 100) (range 100)) ?
Well there's no benefit of doing one op over 100 elements with reducers anyways
You'd have to map vec over the inputs to combine them before the reducer op
@alexmiller Thanks, I was just using as an example. Is this inherently a limitation of the way reducers work, if so, can you explain that a little?
@misha, @fellshard : it's not a macro -- it's just quasiquoting and splicing, there's no defmacro anywhere
@spacepluk do you know for sure that it even generates code?
@didibus reducers don’t currently support it. they ultimately end up grouping the source collection into segments, and running reduce on them (then a combine function on those results). Being reduce-based (like transducers), the story for iterating over multiple coll sources is limited. Transducers do have an impl of this but it’s not really used.
I now about compojure.route.file and compojure.route.resources . How can I say "I want localhost:9009/dogecoin" to map to "<PROJECT_DIR>/foobar" ? route/resources seems only good for mapping localhost:9009/ to resources/public
(defn fibtill [n] (take n (map first (iterate ( fn[ [a b] ] ( + b (+ a b))) [0 1]))))
but now this error message : IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:542)
@roelofw I think your inner fn signature needs to be [a b] not [[a b]]. It isn’t passed a sequence, it is passed a Long (e.g. 0 and then 1).
@roelofw (also note that it is idiomatic to have a space between the ‘n’ and the ‘[’ but I assume that was a cut/paste error)
When I do that , I see another error message :
ArityException Wrong number of args (1) passed to: runner/fibtill/fn--1885 clojure.lang.AFn.throwArity (AFn.java:429) `ArityException Wrong number of args (1) passed to: runner/fibtill/fn--1885 clojure.lang.AFn.throwArity (AFn.java:429)
yes, that inner fn will only be called with one argument, first 0 and then 1
https://clojuredocs.org/clojure.core/iterate states iterate calls an fn that accepts a single argument
oh I see, I said you needed it to be [a b] - doh - not enough coffee 🙂
When I do
(defn fibtill [n] (take n (map first (iterate ( fn [ [a b] ] ( + b (+ a b))) [0 1]))))
I see now this error : CompilerException java.lang.RuntimeException: Unable to resolve symbol: in this context, compiling:(C:\Users\rwobb\AppData\Local\Temp\form-init3477763366039765302.clj:1:1)
yes, your inner fn is provided with, and can only accept ‘a'
if you want ‘a’ to be 0 and ‘b’ to be 1 in your example then you probably want to pass a sequence of vectors
e.g. (defn...... [[0 1]])
also when I do fn [a b] I see this error :
CompilerException java.lang.RuntimeException: Unable to resolve symbol: in this context, compiling:(C:\Users\rwobb\AppData\Local\Temp\form-init3477763366039765302.clj:1:1)
@colin.yates I did that but then I get the first error and that is what I want that a = 0 and b = 1 on the first time
@roelofw - let me play at the repl
should be something like
(take 10 (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1])))
basically, your function needs to be "closed" in that what it takes in it returns something similar, as you are gonna call your function on that again
first step is to understand the early steps I guess, decompose what the iterate call does without the map first and go from there
@roelofw I’ve been leading you up the garden path - I misread the '(’ :-). (def fib (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1])))
is what you want
so because your iterate fn takes a sequence it must return a sequence
you aren’t doing that, you are returning a single number (+ b (+ a b)).
so the second invocation is passed a single number
I think that was missing I had to make the calculate the part into a [] and make two numbers
I’m not ashamed to admit that after 4 or so years writing Clojure full time this is the first time I have had cause to look at iterate
😉
> Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects So reading this immediately i know that the output of f must "look like" (have the same type) as its input
@colin.yates how do you make the fib numbers then
the iterate is return a sequence of vectors (e.g. [[0 1] [1 2] [2 3] [3 4]] - NOT the real fib numbers obviously), so mapping first turns that into [0 1 2 3]
I learn a lot about clojure by doing the 4clojure challlenges. More then doing the clojure koans
getting familiar with passing around and destructuring maps and sequences is a huge part of it I find.
(checkout clojure.type or Prismatic’s schema)
@colin.yates is this a remark for me ?
yep. I found describing the structures I am expecting and exporting save my bacon a huge amount of times
oke, I found a little bit about Prismatic schema when reading about validating on web apps
@roelofw I don’t know what has happened to my fingers and brain today - I mean clojure.spec - sigh.
I hope the day is not too long for you @colin.yates
@colin.yates I read the spec guide and it looks very interrested
also I hope to dig into partial and apply . I use now a lot '( fn [x] ..... ) ' and saw that some people write the same with partial , comp and apply
@roelofw yep, they make for some really elegant code. Also get comfortable with the threading operators (-> and —>)
slightly less used is juxt
but when you need it, it is great 🙂.
right, I had better go and continue breaking my app
@colin.yates what app are you making if I may know
@roelofw it is commercial software unfortunately.
Hello everybody. I need to add some lib (in jar file) to my classpath for android project. Unfortunately, placing jar to new 'resources' folder and adding :resource-paths in project.clj doesn't help
oke, and you use clojure as development language ? @colin.yates ?
I do, both for the back and front end. I have been experimenting with Elm a bit as well.
Actually, I have a problem with Jsoup library on android. The error message is "IllegalStateException Could not read resource entities-xhtml.properties. Make sure you copy resources for org.jsoup.nodes.Entities org.jsoup.nodes.Entities.load (Entities.java:289)". It happens when I invoke Jsoup/connect. I now, that the problem is, that my apk doesn't have entities-xhtml.properties (this file is inside jsoup.jar)
and do you use something like luminus or do you use seperate libraries ? @colin.yates ?
@roelofw I wanted to learn the guts of it so I started with Compojure and ring and built everything up from there. If you are using it on both back and front then I highly recommend you look at the EDN infrastructure. So much quicker than JSON.
I'm creating a zip file from a folder on disk. I'm currently using this form to return the zip file, but it saves it to disk which is completely uneccessary
can anyone point me in a direction to just stream the zip file without committing it to disk?
also, running into issues of where the folder "generated" is on my local vs the deployed site
@dpsutton - can you not use a https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html
@colin.yates thanks and go back to work before the boss gets angry
@dpsutton you need to have a new thread that writes to the ZipOutputStream and use a PipedOutputStream, then you can return the other end and avoid filling memory.
@roelofw - that’s one of the benefits of being the chief techie 😉
there's some management to do in case the client drops the connection and you don't get to write all of the zip file, in which case you should kill the thread
@colin.yates then you do not have a problem. I hope when I learned a lot I also can be a proffesional clojure programmer but at this moment no clojure jobs in the Netherlands
I think [piped-input-stream](https://github.com/ring-clojure/ring/blob/master/ring-core/src/ring/util/io.clj#L11) will help you.
no, I think that's the whole idea.. so you would do like :body (piped-input-stream (fn [ostream] (ZipOutputStream. ostream) ; your stuff
that function would then run in a future that writes into the inner PipedOutputStream while ring gets the corresponding PipedInputStream
@dpsutton - you could do a lot worse than consume everything that guy has written 🙂
pff , clojure is hard. Why is my function call not right, I see this error message :
java.lang.Long cannot be cast to clojure.lang.IFn,
on this code : (ns exercises.core)
(defn my-max
"Finding the max of a seq without using max"
[s]
(reduce (fn [acc n ] (if (n > acc) n acc )) 0 s)
)
(my-max ( 1 2 3))
(1 2 3) is a function calling form, where the reader will call the first argument providing the remaining arguments. You need [1 2 3]
or '(1 2 3) if you want a literal list
ah - @dpsutton beat me to it 🙂
chips, I have to change my code, I thought I had read a seq but it is a number of inputs:(
I have a question about transducers: > The resulting sequence elements are incrementally computed. These sequences will consume input incrementally as needed and fully realize intermediate operations. This behavior differs > from the equivalent operations on lazy sequences. Why is that different from lazy sequences?
undoubtedly there's a shorter answer to your question but the talk is a good one if you have the 45 minutes at some time in the future
i think you're conflating transducers themselves vs transducers in the lazy-sequence context
if you had: (sequence cat [(range 10) (range 10) (range 10)])
, when you try to take an element at a time, the cat transducer might will pump all 10 items from one of the ranges all at once
if you had (mapcat identity [(range 10) (range 10) (range 10)])
and took an item, the mapcat would only take the minimum amount necessary from the range
s
things based on reduce are generally push-model (push all these elements through the reduction), things based on lazy-seqs are pull-model
hi, i'm new to clojure, any suggestions other than emacs for a windows editor with repl?
but i'll put out the opinion that emacs is well worth learning and is a lovely clojure environment. but don't let anyone bully you into learning it, and there will be quite a bit of time to pick it up
@dpsutton i may setup emacs, i tried to get it running for ocaml on windows and was a nightmare. worked fine on linux though
but i run it for work and clojure on windows and its not terrible but i much prefer my fedora box at home
another emacs fan but a huge +1 for Cursive - it runs on IntelliJ which is excellent. If I was looking for the lowest barrier to entry that is the route I would go
we use Jetbrains software in .NET, the resharper package. Nothing but high praise for their engineering
@genec If you have any Cursive related questions, ping me in #cursive and I’ll see what I can do.
Yes, it does. You can get a free non-commercial licence for Cursive for playing around.
@cfleming @colin.yates thanks for the info, intellij/Cursive looks like the simplest way to get setup. I'm looking to move some of my work from FSharp to Clojure but don't want to get bogged down in the Clojure setup just yet.
@genec Cool, if you’re used to an IDE Cursive is probably the lowest-friction starting point. You can always switch to emacs later if you decide you want to.
@genec I use cursive and I love it. I did all the clojure koans and some 4clojure problems
anyone who can point me to a good compojure tutorial so I can start learning to make web apps.
@roelofw this isn’t a bad crash course: https://learnxinyminutes.com/docs/compojure/
I read so many times, avoid use global states
on the example below, I'm using cassandra as global
can you enumarate why this is so bad ?
(defn create
[receipt]
(cassandra/insert receipt))
what if you had two cassandra instances to talk to?
Anyone have recommendations for the best way to architect long-running Clojure threads and scale them out in a way that is not brittle? I could achieve what I want via actors (Akka), but I'd rather stick to Clojure, core.async, and use channels and go-blocks if possible. Essentially looking for something to scale out threads with a bunch of go-blocks running inside, and then kill/restart/etc them as needed. Currently doing some of this via stream processing, but for various reasons there are a lot of drawbacks I don't want for a part of my use-case.
I use component with core.async, but this doesn't solve scaling things out across machines in any way.
Looking more for Akka but with core.async. I'm pretty certain nothing like this exists, but asking just in case.
Really I just need to know things like if the thread is alive/dead, kill the threads on remote machines, etc. I can do this manually and it works, but is very brittle and feels like a reinvention of things that exist but have years of work in them.
onyx purports to be distributed processing for clojure, but I've not used it. I don't think the CSP model (core.async's model) is appropriate for distributed processing personally
So perhaps the answer might be something more general JVM related that can be used easily from Clojure. I'm able to do what I'm doing a lot easier with core.async than actors, which is one reason I don't just use Akka. For instance I don't want the sender/receiver coupling.
I'm using Onyx which is what I was referring to with stream processing. There are some drawbacks for my use case in that I'd end up with a lot to do micro-managing jobs, order is an issue even with windowing, and a few other things.
I use stream processing for a lot of things, but this part of my use-case is exactly what I need to move out of stream processing as there's a bit of use-case mismatch.
dunno sorry. I've done distributed processing as streams/mapreduce, and I've done CSP on single machines. I've not seen anything like distributed CSP
Thanks. Nor have I really. Some rumblings inside the Golang space about exactly that. So I'm left with being overly clever in my stream processing and having to remove some functionality, or transitioning some code to use actors and Scala. Anyway, I think part of the problem is by nature core.async is flexible and not tied to distributed implementations like actors typically are, but it would be nice to have something one day.
good point @alexmiller . but on my real usecase I don't have
what really bother me is change my function signature to receive the connection or a map with all my deps
@oliv in my experience, many systems grow to have more than one of something in the fullness of time. The other major problem with globals is that those functions are hiding their true dependencies. I would far rather know about my dependencies than have one fewer parameter.
you can still easily mock your db either way so that’s not a differentiator
Hey everyone, looking for some help here. Been banging my head against the wall for a couple hours now, lol
(defrecord TestRecord [^Double field-1
^String field-2
^BigInteger field-3
^BigInteger field-4])
(def v ["123.45" "I am a String!" "1234567890" ""])
How do I cast the data in v
to the type-hinted types in TestRecord
?
Obviously this is just a mockup, but I'm reading in from a tsv.> The other major problem with globals is that those functions are hiding their true dependencies
good point. I see the hiding
as a good thing. ( maybe too much oo bias ? )
@oliv as Rich has said, “encapsulation is folly” :)
@mystickphoenix it’s a hack, but you could use read-string
read-string will read each string into Clojure data
if you (map read-string v)
you’re pretty close
but really, you probably want something that a) models the expected type of each field and b) applies a type-specific conversion
using things like Double/parseDouble
etc
nope, they’re just containers
those type hints aren’t even used by the record
the fields will all be type Object
they would be used if you inlined methods into the defrecord to resolve java interop
Ah, okay. That makes more sense. Great, thanks @alexmiller, at least I have a direction to go in now. Appreciate the help!
how do I write something out (fast) so I can read it back via edn/read or edn/read-string ?
wow that was a frustrating little ride https://github.com/technomancy/leiningen/issues/1477
@mystickphoenix Not sure what your larger goal is, but if you just want maps with some type safety, what about using something like Schema (if you aren't familiar with it)? Coercisions + validation might be useful for you here. There are some other libs that deal with transforming data from untyped sources with some conversions and into types, especially from CSVs, but I can't think of the names off the top of my head. https://github.com/plumatic/schema