Fork me on GitHub
#clojure
<
2017-01-05
>
qqq01:01:45

what is the mechanics behind the #'handler trick in reloading handlers w/ o restarting the webserver?

hiredman01:01:34

#' is var quote

hiredman01:01:51

so #'handler gets the named var, not the value of the var

hiredman01:01:20

vars are the mutable cells that hold named global values

qqq01:01:28

since this is an extra layer of indirection, where does the 'deref' come in?

qqq01:01:40

how is it that we can pass "named var" instead of "value of war", and all goes well ?

hiredman01:01:03

if you invoke a var, it automatically derefs itself and attempts to call its value as a function

hiredman01:01:22

which is why you can use them as ring handlers like that

qqq01:01:42

I get the mechanics now; thanks for explaining.

qqq01:01:32

I see it used everywhere, but not which namespace it comes from.

hiredman01:01:26

I dunno that I've ever seen it

hiredman01:01:42

all kinds of other wrap this and that, but no wrap-ring-handler

hiredman01:01:52

google finds https://github.com/ztellman/aleph/issues/14 which leads me to believe it is part of aleph, for making ring handlers work with aleph

qqq01:01:39

@hiredman: I found it in some gist, (similar, but not this exact one): https://gist.github.com/ibdknox/665304/8a8a276444e217ab429c26b9372e6ae345b1d10e#file-alephpost-clj-L15 removing it works, so I'll just not use it

seancorfield04:01:32

map calls seq on its argument so you don't need to.

seancorfield04:01:11

(and you wouldn't use map with println -- I assume that's just by way of example?)

seancorfield04:01:47

I probably wouldn't bother creating a function for it, I'd just do (map (partial map some-func) my-collection)

neupsh04:01:40

@seancorfield yes, println is just a quick function i tried in repl 🙂, i actually have some other stuff to do beside just 'nested-mapping' to arguments,

neupsh04:01:04

i will remove the seq call

neupsh04:01:15

thanks a lot

qqq05:01:12

Does anyone have a guide on setting up clojure on AWS? I can run everything fine locally, but I'm not having a nice time dealing with AWS, and I feel like some poor clojure soul has already dealt with this problem.

tbaldridge05:01:06

AWS is pretty huge, you'll probably need to be more detailed.

tbaldridge05:01:26

But in general I normally install java, and then upload a uberjar to AWS.

tbaldridge05:01:36

AWS EC2 that is.

qqq06:01:52

@tbaldridge: Agreed. Upon further reflection, my real issue is at the "persisting clojure data structures to database" layer, not so much with AWS. I have no problem setting up EC2 or AuroraDB. My issue is "how do I store my server side stuff to AuroraDB", which is really a "Clojure <-> DB" issue, and not so much an EC2 issue.

seancorfield06:01:31

@qqq Isn't Aurora a MySQL clone? So regular clojure.java.jdbc should do the trick there. Or you could use DynamoDB (and Faraday, the Clojure library)?

yogidevbear07:01:14

@qqq you might need to look at your security groups setup on AWS

val_waeselynck07:01:11

@pesterhazy thanks :) do you have a particular use case in mind for supdate?

yogidevbear07:01:41

Although I'm not too sure if that matters for an app sitting inside the same network as the DB, but you might need to add you EC2 instance's IP to your security group for the DB to allow communications

qqq07:01:18

@qqq: Yeah, Aurora has both MySQL and PostGres layers. However, I was mostly focused on client side, and did not think out my server side at all before trying to upload to EC2.

qqq07:01:05

@yogidevbear: As it turns out, I was using Lightsail, which was in it's own "shadow VPS" not visible to the rest of AWS without lots of heavy reconfig. I have since decided to not use AWS.

yogidevbear07:01:00

Glad you figured out the issue simple_smile

pesterhazy07:01:13

@val_waeselynck , one example would be an etl job where data types need to be coerced for some attributes

val_waeselynck07:01:41

@pesterhazy I see. You should probably also consider Schema for this particular use case

pesterhazy07:01:12

What is your use case?

pesterhazy07:01:38

Even just updating only if the key exists is useful enough

pesterhazy07:01:53

I call that update-some in my code

val_waeselynck07:01:25

in my case, it's mostly involved in the process of transforming Datomic entities into data structures (pull doesn't quite work for what my client expects).

val_waeselynck07:01:35

It's similar to coercion yeah

val_waeselynck07:01:57

optional nested update is useful too

pesterhazy07:01:55

I need that too

pesterhazy07:01:32

So you start from a pull result and transform into the output format?

val_waeselynck07:01:53

no i start with entities

pesterhazy08:01:17

I guess pull is a drop in replacement for entities

pesterhazy08:01:03

If you know ahead of time what you need

val_waeselynck08:01:04

not really. In my view when you pull, you need to know everything you need to pull, whereas with entities you can split the work

pesterhazy08:01:34

Ya I really like entities too

val_waeselynck08:01:04

I mostly use those for my business logic

pbaille09:01:24

hello, how can I create a regex from a str?

pbaille09:01:45

found it 're-pattern

h.elmougy09:01:05

(re-pattern s)

h.elmougy09:01:33

example from docs (re-find (re-pattern "\\d+") "abc123def")

qqq10:01:51

Is there a more up to date take on http://clojure-doc.org/articles/tutorials/basic_web_development.html ? (something to just walk through setting up web servers properly -- everything I have done so far has been very adhoc)

kokos10:01:33

is there a filter fn for maps that would that take a map and a predicate and returns a map with keys for which the values that satisfy the predicate? something like:

(filter-map {:a 1 :b 2 :c -1} #(> % 0))
returning {:a 1 :b 2}

bfabry11:01:50

@kokos there's not, the normal way to do it would be (into {} (filter [[k v]

kokos11:01:08

perfect, thank you

miikka11:01:21

Almost everybody has some utility library around with that function. Medley, Useful, Potpuri, …. Specter can do it, too. Weird that Plumbing does not seem to have it.

tankthinks12:01:58

@qqq consider using AWS Elastic Beanstalk … it’s AWS PaaS, aka version of Heroku

tankthinks12:01:44

you can create a full stack with load balancer to EC2s, to an RDS instance with the correct security groups

tankthinks12:01:16

You can follow AWS’s Java tutorial and simply provide the uberjar output from lein

tankthinks12:01:46

I usually box up all of my deployment artifacts into Docker Images instead of installing stuff directly to EC2, and Elastic Beanstalk can do Docker as well … so you have that option too

qqq12:01:36

@tankthinks https://github.com/weavejester/lein-beanstalk <-- have you used this? this is actually the optimal situation: 1. put aws keys in some config file; 2. run "lein deploy" 3. it works

mchampine13:01:19

@qqq A friend pointed me to https://www.skyliner.io/deploy/clojure , which looks good for easy and secure deploy of Clojure on AWS, and is apparently written in Clojure too,

qqq13:01:08

@mchampine : looking into it now -- apparently lein-beanstalk hasn't been updated for 3 years,

qqq13:01:53

@mchampine : wait what? this is a web service, not a lein plugin ?

qqq13:01:07

so I need to sign up / create an account with them in order for me to efficiently deploy to aws ?

mchampine13:01:20

@qqq Yeah, but it seems to have enough value to be worth it. Free for personal projects. This seems to cover its main features. https://blog.skyliner.io/the-happy-genius-of-my-household-2f76efba535a#.e6k1gm3ix

mchampine13:01:47

I have not used it yet - just read the blog etc.

borkdude13:01:36

Is it possible to entirely redefine a class with proxy?

borkdude13:01:52

patch so to speak

tankthinks13:01:11

I have an unfortunate amount of experience with Elastic Beanstalk and AWS … I wrote a bash wrapper around AWS’s eb command line tool to expose most of its configuration via command line flags so the user doesn’t need to learn Cloud Formation

tankthinks13:01:39

we did this for work and were considering OSing it but weren’t sure if there was a need

codefinger13:01:51

@qqq in addtion, there’s Heroku itself, which has some good resources (warning: i wrote most of them) https://devcenter.heroku.com/articles/getting-started-with-clojure#introduction

codefinger13:01:39

if you want deployment with lein (as opposed to git push) there’s https://github.com/heroku/lein-heroku

joelkuiper14:01:24

I’m running into a weird problem with cheshire and cljs-ajax

joelkuiper14:01:51

cheshire returns newlines in the string (not escaped) breaking the cljs-ajax

joelkuiper14:01:23

not sure what would be the appropriate fix here, making sure I escape the newlines before serialising?

joelkuiper14:01:54

e.g. I get a json like

{”string”: “foo
bar baz
“, 
“field”: “value”} 

joelkuiper14:01:00

which is not valid json from Cheshire

mpenet14:01:09

odd, cheshire properly escapes newlines for me

joelkuiper14:01:21

unicode thing maybe?

joelkuiper14:01:34

fixed it by setting :escape-non-ascii to true

mpenet15:01:51

never knew about this one, nice find

juliobarros18:01:18

Is there a way to pretty print (nested maps) with sorted keys to make it easier to visually scan?

sophiago19:01:04

@juliobarros there are number of ways to do that, depending on what result you want. maybe look at pprint or print-table?

sophiago19:01:48

can anyone give me some rough heuristics for collection size to justify using the reducers library?

bfabry19:01:44

@sophiago less about size and more about type of work and number of cores available

sophiago19:01:16

by my understanding it returns ordered data, but the predicates need to be associative...i.e. not dependent on previous calculations, is that right?

bfabry19:01:32

the transformations need to be associative yes

sophiago19:01:51

i'm not certain yet if that'll work for me. i suppose that means anything involving convolution is off the table, which i think would likely account for most of my longer sequences in the case i'm considering

tbaldridge19:01:53

I'd say though that core.async's pipelines + transducers have replaced anything I would have used reducers for.

sophiago19:01:54

@tbaldridge ah, really? core.async is certainly more flexible, but reducers have the benefit of being able to easily swap out for sequential functions

tbaldridge19:01:01

Sounds like you're doing something like image processing/large matrices?

sophiago19:01:24

sort of...automatic differentiation

sophiago19:01:37

so generally i benefit from lazy-seqs, but wanted to experiment with a parallel implementation for some hpc people i have in mind to test it

sophiago19:01:48

i think i'm unlikely to use core.async, at least at the moment, due to the added complexity. plus having to make sure the output is ordered

sophiago19:01:42

but i think a decent amount of the functions could be broken up and parallelized if there turn out to be use cases that require very long sequences to be output at once

tbaldridge19:01:09

yeah, I don't recommend core.async in your case, I spoke before I asked what you were working on.

qqq19:01:30

@sophiago: this might be a heretical, but if you want automatic differentiation, checkout python/tensorflow, the whole speech there is: 1. you write the forward pass 2. tensorflow automatically calcualtes the backward pass 3. it even takes care of the GPU for you clojure, at the moment, really lacks anything comparable

sophiago19:01:28

@qqq why would you need both forward and reverse mode?

sophiago19:01:52

and doesn't tensorflow use matrices?

qqq19:01:46

because generally the use case is: 1) you define the model (like some neural network) by writing the forward pass; 2) then you want to train the parameters on some data set -- which you do via stochastic gradient descent + back propagation (which is what requires the derivatives); the closest libraries I have found in clojure are deeplearning4j and https://github.com/uncomplicate/neanderthal but imho, none even comes close to tensorflow

sophiago19:01:16

i think you're very confused about what i'm talking about

sophiago19:01:38

i'm writing a dsl for automatic differentiation, not machine learning

qqq19:01:38

if the end goal is to write a DSL rather than ML, yeah, everything I said is unrealted

sophiago19:01:38

machine learning just has nothing whatsoever to do with what i'm talking about

qqq19:01:04

@tankthinks @codefinger : thanks for the insights on (1) AWS Beanstalk and (2) Heroku. I've actually decided to go with Google Cloud Platform / Go. Upon further reflection, I actually want the server side to be dead simple and lightweight (in fact, it should do nothing more than (1) routing websockets and (2) checking permissions before hitting the database).

tankthinks19:01:58

@qqq … do you just want couchdb then?

dottedmag20:01:03

When is it useful?

rgorrepati20:01:56

In core async/take!, why does the doc say “Asynchronously takes a val”, when the default is to run take! on the current thread?

rgorrepati20:01:49

@dottedmag, that function is to coerce the given value to java’s number(https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html)

tbaldridge20:01:57

@rgorrepati because it doesn't actually return anything...it may choose to call your callback later on a different thread

tbaldridge20:01:56

@rgorrepati it's an optimization that you can turn off via a flag to that function

dottedmag20:01:33

@rgorrepati I can't find a case when the return value is different from the passed. Either I get a ClassCastException or the same value back.

rgorrepati20:01:53

@tbaldridge ok.. was a little confused. Thanks..

dottedmag20:01:04

@rgorrepati I have looked at the implementation, and it's just a cast, so it does not produce anything useful.

tbaldridge20:01:24

It's a cast from a primitive to a non-primitive type.

tbaldridge20:01:01

or to a wrapped type, and it also inlines, so there's that, but since the compiler does most conversion automatically for you, it's mostly non-useful.

dottedmag20:01:00

@tbaldridge So it's useful if you've got a function with a primitive number type hint, and still would like to call some Number method, correct?

Ivan Fedorov20:01:11

Bonsoir! Is there any guide on how to use time macro? Or a guide for benching sql-related and arbitrary code mix? I'm trying to bench a piece consisting of sql query and simple munging. time says that munging takes 90% of the time (like 1200ms), but if I call my-munge function on prepared data it takes just 9ms.

mj_langford20:01:34

Hi, trying to find out the idiomatic way to point projects at dependencies you're not sure you're going to keep. Lets say you forked an OSS project and need to point at your forked branch on github until merged. Is there a "The way" people would do that in leiningen? Should the fork be listed on clojars if temporary like this?

tbaldridge20:01:28

@ognivo are you sure your SQL code isn't returning lazy results?

tcrawley20:01:44

the proper way is to deloy your fork to clojars under the org.clojars.mj_langford group (or whatever your clojars id is)

Ivan Fedorov20:01:02

@tbaldridge suspecting that actually.

tbaldridge20:01:19

try wrapping your sql results in (vec) or something

mj_langford20:01:37

Thanks @tcrawley, I didn't want to do that if temporary forks were considered pollution or something like that

Ivan Fedorov20:01:00

@tbaldridge I'll try that, thanks!

mj_langford20:01:31

btw, if anyone is deploying on a new mac and can't figure out why gpg won't sign your key for the clojars deploy to work, to get rid of the completely usless misleading "Inappropriate ioctl for device" error, you have to: export GPG_TTY=$(tty)

borkdude21:01:54

How do I get rid of a reflection warning in a gen-class when calling the superclass’s method which has been renamed locally by exposes-methods?

schmee22:01:56

how can I rewrite something like this using recur instead of regular recursion?

schmee22:01:00

(defn game-tree [game]
  (if-not (g/game-over? game)
    (into [game] (mapv game-tree (successors game)))
    game))

qqq22:01:49

the complex part looks like the mapv, which you would need to unroll using a stack to keep track of the 'unexectued parts'

qqq22:01:04

suppose (successors game) gave you :a 😛 :c :d :e

qqq22:01:17

in your loop/recur, you'd push :a 😛 :c :d :e onto a stack, then pop them off and exec them one at a time

qqq22:01:25

thus, "simulating the mapv with a stack"

schmee22:01:23

yeah, that’s what I’m doing now to process the tree

schmee22:01:35

but how to I return the actual tree structure?

schmee22:01:18

I can’t think of a way to get the same output using recur that isn’t a complete nightmare

gfredericks22:01:10

@schmee is regular recursion giving you stack errors?

joshjones22:01:42

curious, what does game look like, basically?

schmee22:01:23

joshjones {:board [:- :- :- :- :- :- :- :- :-], :player :o}, I’m using regular tic tac toe to try out the algorithms

schmee22:01:53

gfredericks no, but I’m trying to implement the negamax algorithm so I need to pass context down the call stack

joshjones22:01:15

and successors does something with the :board?

schmee22:01:51

ttt.negamax=> (pprint (advances ttt/new-game))
[{:board [:o :- :- :- :- :- :- :- :-], :player :x}
 {:board [:- :- :- :- :- :- :- :o :-], :player :x}
 {:board [:- :o :- :- :- :- :- :- :-], :player :x}
 {:board [:- :- :- :- :o :- :- :- :-], :player :x}
 {:board [:- :- :- :- :- :- :o :- :-], :player :x}
 {:board [:- :- :- :o :- :- :- :- :-], :player :x}
 {:board [:- :- :o :- :- :- :- :- :-], :player :x}
 {:board [:- :- :- :- :- :o :- :- :-], :player :x}
 {:board [:- :- :- :- :- :- :- :- :o], :player :x}]

schmee22:01:05

it gives all the games reachable from the current position

gfredericks22:01:08

@schmee "pass context down the call stack" meaning something that will get modified and passed back up?

schmee22:01:29

gfredericks yes, I need to keep track of two ints that will get updated as I visit each node

gfredericks22:01:13

@schmee so you don't really need to produce the tree structure as the end result of the computation?

schmee22:01:18

this algorithm is super easy to implement with mutable data, but I’m trying to do it The Functional Way™

gfredericks22:01:32

tree-seq might be helpful

schmee22:01:37

gfredericks no, I don’t really need to, but I really want to 😄

schmee22:01:54

just to learn how to do it

gfredericks22:01:05

in that case my only idea is zippers

schmee22:01:11

alright, I’ll give them a shot, thanks for your time everyone