Fork me on GitHub
#clojure
<
2017-04-04
>
lincpa03:04:28

postgresql is portable, my clojure project is portable, using postgresql and R. all developing Work in U disk.

qqq05:04:22

in cljs, when I save a file in src/, a recompile is triggered: in clj, assuming I'm using component / writing reloadable-safe code, is there a way to say: on file save, reload all related modules?

metametadata05:04:43

@qqq IIUYC it's usually implemented using clojure.tools.namespace, e.g. take a look at https://github.com/weavejester/reloaded.repl

qqq05:04:47

@metametameta: looks like I did not explain my problem well.

qqq05:04:09

You have used clojurescript right? It's genrally something like "boot watch cljs" - and then, any time you save a *.cljs file, a recompile is triggered.

qqq05:04:35

I want components to auto-reload whenever I save a *.clj file. Reloaded.repl appears to help writing components, but not do "reload on *.clj file being saved."

metametadata06:04:20

So you want to reload code in the already running app on src changes, right? In CLJS I use Figwheel for it. But when using Clojure you usually have to start REPL first and then invoke a helper function which starts the app, watches for changes in namespaces and reloads them:

lein repl
user=> (reset)

metametadata06:04:02

reloaded.repl lib just allows to not re-implement this same pattern in every project

rauh06:04:15

@qqq if you use cursive you can use "sync files in repl", but nothing automatic "on save" exists unless you write watcher code.

rauh06:04:38

There is ns-tracker.core that can watch file for changes.

thosmos06:04:19

@qqq Here's an example project that does auto-reload of both server-side clojure and client-side CLJS, including auto-restart of the app if specific server files are changed: https://github.com/danielsz/holygrail

Shantanu Kumar06:04:56

Can somebody suggest a way to get the option --no-foo (foo being the flag) to work with clojure/tools.cli library? How should I declare the foo CLI-parsing spec?

qqq06:04:13

@thosmos: noted; thanks!

qqq07:04:02

is there a performance penalty to try-catch blocks .... EVEN WHEN NO EXCEPTION IS THROWN ?

rovanion07:04:26

Can plumatic/schema define specs for macros?

qqq08:04:42

@rauh: I'm confused, what I read on SO says "try is cheap, throw is expensive"; but the blog post you're linking to suggests otherwise

rauh08:04:00

@qqq Using exceptions is neglibile in performance, even faster than using a normal return value for error condition. As long as your exceptions only happen "exceptionally"

rauh08:04:30

So no more than once every 1k...10k calls.

qqq08:04:50

so here is my XY problem:

rauh08:04:54

Using them for control flow and throwing them all the time is abig nono

qqq08:04:01

I'm implementing a database, with optimistic concurrency

qqq08:04:14

there's alost of [:swap old-val new-val]

qqq08:04:27

now, when old-val is out of date, I'm considering throwing an exception to have the entire transaction fail

qqq08:04:34

is this "exceptional case" or is this 'control flow' ?

wei08:04:11

what’s a succinct way to write a function that always returns its first argument?

qqq08:04:11

imagine that a transction = a list of [:swap old-val new-vale] triplets

qqq08:04:18

wei: first

qqq08:04:42

@wei: first (builtin), or, alternatively (fn [x & rst] x)

rauh08:04:48

Well how often do they occur?

qqq08:04:03

actully wait, dumb question on my part

qqq08:04:12

in generall, isn't the cost of setting up a transaction much higher than that of throwin gan exception ?

qqq08:04:33

i.e. the fact that the transaction got rejected and I need to resubmit it -- that should drastically be more expensive than the throw/catch ?

wei08:04:42

@qqq first isn’t quite right, but (fn [x & rst] x) works

qqq08:04:58

@wei: yeah, first returns first leemnt of first arg, not first arg; good call

rauh08:04:34

@wei First is lazier than the desctructing FYI

qqq08:04:49

@rauh: I think @wei's point is that first = wrong

rauh08:04:11

Oh duh, yes, sorry didn't read properly

zivanovicb08:04:11

`(defn two-params [x y] (str "Two parameters! That's nothing! Pah! I will smoosh them " "together to spite you! " x y)) (two-params "b" "r"`

zivanovicb08:04:46

and that gives me this: CompilerException java.lang.RuntimeException: Unable to resolve symbol: two-params in this context, compiling:(C:\Users\zivan\AppData\Local\Temp\form-init3271026095323065387.clj:1:

qqq08:04:37

why is it `(defn ... instead of (defn ?

zivanovicb08:04:09

I tried to add this snippet as a code in slack

qqq08:04:22

you want three `'s

rauh08:04:13

PSA: There is a slack setting that you can press enter without submitting when entering three `.

zivanovicb08:04:25

Anyone knows why closure can't find that function in that context?

rauh08:04:17

Did you evaluate the fn definition?

lsenjov08:04:00

Are you doing this in a text editor with a repl connection?

lsenjov08:04:30

As rauh said, you'll need to evaluate the defn, or reload the namespace

zivanovicb08:04:50

I have two windows, text editor and repl connection in emacs

zivanovicb08:04:22

I just called main function

qqq08:04:13

for returns a lazy seq right? how do I take the or or the and of a lazy list?

qqq08:04:43

oh, I want this to short circuit, so to read as little of the lazy setq as possible (up to chunking)

lsenjov08:04:09

qqq: You'll want to use some

qqq09:04:09

or -> (some true? ...) and -> hmm, a bit more difficult

bteuber09:04:24

and -> (every? identity ...)

pyr12:04:29

Hi #clojure!

pyr12:04:54

With clojure.test, (use-fixtures :once ...) wraps a test namespace in a set-up and teardown fixture. I wonder if there's a way to do that for all tests that run during lein test

pyr12:04:29

My issue here is that I have a fixture that is long to start and it's no problem for me to have it live accross several tests

pyr12:04:08

I'd say the only issue I see here is that the fact that it relies on a separate -main function would break lein test, but I suppose I could live with that

acron12:04:43

@pyr perhaps there is a way to run the tests via API and then create your own alias which starts the environment prior to running the tests

pyr12:04:37

@acron I'm angling towards this

pyr12:04:49

with an entry in the :aliases map in lein

metametadata13:04:27

@pyr I used global flag and :once fixtures to achieve the goal of initing the db for the whole test suite. So that each test ns has (use-fixtures :once with-database) but it would skip db construction based on global flag value, simple.

metametadata13:04:19

the flag must be set on the first fixture execution

pyr13:04:12

I don't know about this global flag

metametadata13:04:47

ah, it's a global var that you define yourself

pyr13:04:16

ah, understood

pyr13:04:34

for me it's the teardown flag that I need to only run once

pyr13:04:52

but indeed I might be able to play with something like this

metametadata13:04:34

what makes the teardown in your tests so costly?

pyr13:04:55

I start an embedded cassandra server

pyr13:04:06

in the same JVM than my project, for test purposes

pyr13:04:49

both set-up and teardown are time-consuming, and there's no need to do that multiple times

pyr13:04:07

the alias approach should be good enough

metametadata13:04:16

but it will probably run the server in the separate jvm then

metametadata13:04:20

well, dunno, guess it depends on how the alias will be implemented 🙂

pyr13:04:24

@metametadata I meant an alias that wraps a call to run-all-tests, as mentioned in the article above.

casperc13:04:27

I am wondering. What is the best way to “read ahead” from a lazy sequence where the realization (reading from some API for instance) takes the time? I thought seque was the way to go, but it seems that it holds onto head and I am getting OOM errors, that don’t happen without seque.

mpenet14:04:09

I guess you could use partition-all and create chunks that you realize one at a time and consume one by one

pesterhazy15:04:41

note that lazy seqs are chunked so it might realize the seq 32 elements at a time

mpenet16:04:30

depends how they are created

mpenet16:04:36

not all lazy seqs are chunked

kasuko17:04:21

Hey, I have a bit of an advanced question. I was wondering if it is possible to override the local bindings that result from a closure?

kasuko17:04:47

(defn triple-adder-fn [a b] (fn [x] (+ x a b)))

(def triple-adder (triple-adder-fn 1 2))

(triple-adder 3)
;; => 6

(binding ['a 5
          'b 6]
  (triple-adder 3))
;; => 14

kasuko17:04:30

Where the last expression doesn’t actually work but I want to know if there was some way to do it?

kasuko17:04:17

The reason I ask is because in my use-case I don’t actually have control over the triple-adder-fn so I can’t just re-call it.

tbaldridge17:04:52

No that's not possible.

noisesmith17:04:25

with volatile! - are reads from more than one thread safe? - I recall that the point of java's volatile annotation is that it guarantees using the value from memory and not caching the value in a register that could go out of sync with memory, so that would seem to indicate yes...

hiredman18:04:02

what the thread is doing is what determines if it is thread safe

noisesmith18:04:21

let's assume the reads are not used as a basis of further writes to the same volatile

tbaldridge18:04:29

Volatile really should only be used from one thread or when there is a memory barrier between thread switches.

tbaldridge18:04:51

Otherwise I'd use an atom

noisesmith18:04:02

OK - so not even for reads - I heard something about the reads from other threads being needed for transducers but didn't really understand

tbaldridge18:04:01

Right but you don't know when writes will be flushed, so a write to a volatile may be cached until the next barrier

noisesmith18:04:20

so this would come up with transducers on channels, I assume

tbaldridge18:04:31

So this works in core.async because channels transfer controls via locks and the like

noisesmith18:04:47

OK - it's a bit clearer now, thank you very much

urzds18:04:28

I want to ensure that a parameter passed to my function is actually from a certain namespace. Say I want to call (require [my-error-codes :as e]) (validate-error e/MY-ERROR-123) and then do something like (require [my-error-codes :as ex]) (defmacro validate-error [error] (assert (= (namespace error) 'ex)). This code does not work, but I would like to know how to express that properly.

noisesmith18:04:35

urzds vars have namespaces, most values in a namespace will not

urzds18:04:48

What I am actually trying to do is to write some kind of "registry" for error codes and later make sure that all error codes my application passes to its users is present in that registry. The variant with def-ing them all in a central namespace and then checking the namespaces of the error codes leaving the application was the first solution that came to my mind.

noisesmith18:04:02

#'e/MY-ERROR-123 will have a namespace for example

noisesmith18:04:38

e/MY-ERROR-123 (without the var-quote) only has a namespace if you use a type that is namespaced

noisesmith18:04:58

perhaps what you want is to use (or create) a namespaced type here?

urzds18:04:33

@noisesmith What exactly do you mean?

noisesmith18:04:41

wait, not even vars are namespaced

noisesmith18:04:54

urzds to test the namespace of a thing, it needs to be a thing that owns such a property

noisesmith18:04:12

oh - is this why it's a macro, to catch the symbol used?

urzds18:04:20

Yes, that's what I was trying.

urzds18:04:55

But indeed I could also define a type and then create instances of it on the fly... if that's somehow possible. Actually I've written Clojure code for some months now and never had to create any types or classes or things like that...

noisesmith18:04:27

isn't what you really want here data validation?

noisesmith18:04:38

eg. what spec or plumatic/schema offers

urzds18:04:57

hehe, yeah, weeeeellll...

urzds18:04:56

What I am actually trying to do is validate against an s/enum. But I need to find all the enum values, which are scattered throughout the code.

noisesmith18:04:04

haha, something like (->> (all-ns) (map ns-publics) (mapcat keys) (filter valid-error?) (set))

urzds18:04:08

That's why I wanted to mark all possible enum values in some way (in this case by making them symbols from some special namespace), so I can then gather them, construct a set, and use that to construct the schema.

urzds18:04:48

That would work, too. Sounds like an interesting approach.

noisesmith18:04:16

it's basically how clojure.test finds tests

urzds18:04:40

Maybe I should look at clojure.test for inspiration then... 🙂

noisesmith18:04:05

that still seems hacky to me though - but maybe it makes sense for your code

urzds18:04:40

I don't know. How do people usually do this? Ensure that error codes leaving e.g. their HTTP API can be looked up somewhere?

urzds18:04:47

Or do people just throw random strings at their users and don't care about them following any pattern?

qqq18:04:04

(reading manual on MapDB) Hereafter is a simple example. It opens in-memory HashMap, it uses off-heap store and is not limited by Garbage Collection. ^^ I don't get it. How is something IN-MEMORY yet OFF-HEAP and not affected by GC ?

urzds18:04:04

them=the strings

noisesmith18:04:02

urzds libs like liberator and ring-swagger take a different approach, by explicitly describing the error cases (or using the ones that REST documents implicitly) as I understand it

urzds18:04:53

@noisesmith Do you happen to have a link at hand, that describes how they do it?

ordnungswidrig18:04:03

@urzds HTTP has a pletora of status codes to communicate the semantics of an error (https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)

noisesmith18:04:04

@qqq by allocating OS memory outside the vm, I think

noisesmith18:04:43

@urzds I would start with the github pages for liberator or ring-swagger - both are meant to organize a clojure web api

urzds18:04:02

@ordnungswidrig We're using them (actually Liberator does for us), but we want to give more detailed explanations for what went wrong.

dpsutton18:04:04

@qqq > On the other hand, the off-heap store refers to (serialized) objects that are managed by EHCache, but stored outside the heap (and also not subject to GC). As the off-heap store continues to be managed in memory, it is slightly slower than the on-heap store, but still faster than the disk store.

urzds18:04:50

Specifically: When we test the error checking in our Liberator resources, we would like to know we actually triggered the error we intended to, instead of something unrelated.

urzds19:04:44

@noisesmith @ordnungswidrig And in the application UI, that uses the HTTP API, we would like to be able to know what error exactly happened, so we can give more hints to the user.

ordnungswidrig19:04:51

@urzds probably create your own data format for error details. I’ve seen that before and it worked nicely for more API like applications.

ordnungswidrig19:04:18

Beware that the error message presented to the user needs to take into account the context of the user interaction, i.e. the current state on the client. A good HTTP api is stateless so the server resource doesn’t have clue about the context of the user interaction.

urzds19:04:42

E.g. we emit 422 Unprocessable Entity when we get send an object that is somewhat broken. Or if the object is inconsistent in itself. Or if it refers to another object that does not exist. Returning just one error code (422) in this case seems like it will lead to very difficult debugging later.

ordnungswidrig19:04:00

That’s right.

ordnungswidrig19:04:22

Shall we move the discussion to #liberator? This is getting into details.

urzds19:04:43

Sure. Thanks for being so patient with me. 🙂

colliderwriter19:04:16

What's the current recommendation for signal handling? I see some libraries but curious to hear what's actually in use

colliderwriter19:04:43

@noisesmith Thanks, that's an upgrade for me

noisesmith19:04:10

it does use sun.misc, but I'm not sure there's any way around that for handling signals

colliderwriter19:04:05

I had been using an addShutdownHook snippet but it had poor selectivity. This is much better

qqq20:04:49

Does anyone have a PURE CLOJURE solution to the following problem: FILE-BACKED MAP STORE. So I want the following operations: (create-db "file-name.db") (.put db key value) (.transact! db) (.rollback! db) (.close! db) So it's just a kv store, where k/v = clojure types. I need it to support transact! and rollback!. I also need open/close. Lastly, I want this to be pure clojure. Is there any library out there that provides this?

octopuscabbage20:04:18

you want a map zipper

octopuscabbage20:04:33

err not a zipper

octopuscabbage20:04:37

i forget what it’s called

octopuscabbage20:04:44

but it stores it’s diffs in a list

octopuscabbage20:04:48

so you’d have a list like

octopuscabbage20:04:00

[{} {:add {:a 1}} {:add {:a 2} {:add {:b 3}} {:remove :a}]

octopuscabbage20:04:55

and you’d get

octopuscabbage20:04:00

{:b 3} as the output map

octopuscabbage20:04:15

wait do you need that to be an actual db or just in memory store?

octopuscabbage20:04:24

oh you said file backed

octopuscabbage20:04:26

ignore me sorry

urzds20:04:51

@noisesmith @ordnungswidrig Thanks again for your help!

qqq20:04:23

@octopuscabbage : yeah, I have an in memory version working, it's the file-backed part that I'm stuck on

noisesmith20:04:26

@qqq just use a proper database, even if the file based map in pure clojure existed, I wouldn't trust it for correctness

qqq20:04:30

especially since I'm not sure what JVM file IO semantics are

noisesmith20:04:53

we have immutable data structures in memory, we don't have immutable files

qqq20:04:38

it's not clear why algorithms taht work in RAM won't work also work on disk

noisesmith20:04:53

because your file can be accessed by two vms at once

noisesmith20:04:01

because a vm can crash in the middle of using the file

noisesmith20:04:07

a lot of extra issues come up with files

noisesmith20:04:32

it's all possible to manage, there are programs built to manage these issues, they are called databases

qqq20:04:00

suppose I'm an idiot and bent on reinvengint the wheel for educational purposes

qqq20:04:11

where could I. learn how to write a simple java program that handled all this? (and then I can convert it to clojure)

noisesmith20:04:51

you could start by looking at sqlite - it's a small codebase

noisesmith20:04:20

as in, reading their code to see how they designed it

mobileink20:04:28

dealing with a filesystem is not in principle different than dealing with network connections.

qqq20:04:34

https://sqljet.com/ there's actually a pure java implementation

noisesmith20:04:50

@mobileink except for the persistence part

mobileink20:04:24

filesystems break.

sveri20:04:43

One of the problems to solve is to make sure the data is written conistent and persistent to the file system bevor you acknowledge the transaction.

qqq20:04:10

@sveri: yeah, I don't know how to solve that problem

sveri20:04:30

@qqq this is one of the solutions for instance: https://en.wikipedia.org/wiki/Write-ahead_logging

mobileink20:04:59

@sveri and there us no way to do that, even in principle, since io is involved. the best we can do is statistical: minimize the probability of error.

sveri20:04:23

Additionally some filesystems pretend they wrote the data, while they did not, but do so a few seconds later.

qqq20:04:13

is there any reason why these can be done in other languages; but have not been done in clojure?

noisesmith20:04:41

@qqq same reason we don't have a web browser written in clojure - it's a big hard problem and a usable solution represents a lot of work

sveri20:04:03

@mobileink In a pragmatic way, the problem is solved, by using replication, yes, in some very very rare cases, it x different datacenters go down at the same time, it still might fail. But it is possible to make sure the data ist stored persistently

noisesmith20:04:56

interesting, hadn't heard of that thing - I wonder if it works as advertised

qqq20:04:38

the code that I would really love to read -- pure clj implementation of leveldb

mobileink20:04:56

@noisesmith it's possible to arrange things such that you have a high degree of confidence that everythin is hunky-dory. it is not possible to guarantee it, as is the case with truly functional programmer and proof engines, etc.

mobileink20:04:22

practical programming is inherently probabalistic. (sp?)

noisesmith20:04:46

we have good consensus algorithms that work on the network, as long as you allow "quorum could not be reached" as a valid state at least - there are some guarantees possible as long as you reify failure

noisesmith20:04:34

most people don't do it right though, but there's solid theory, and decent implementations out there

noisesmith20:04:08

but yes, distsys is distsys, whether you mean between two processes on one box, process and os, or over the network, etc.

mobileink20:04:20

i don't see how an algorithm could work on a network. fwiw i'm not being intentionally obstreperous, i think this is not only a fascinating topic but one for which we do not yet have widely accepted models.

noisesmith20:04:42

@mobileink you might find Leslie Lamport's work interesting, RAFT for example

noisesmith20:04:26

they are notoriously easy to implement incorrectly, but they work

mobileink20:04:40

thanks, wiil look (someday 😉 ) my go-to guy is Robin Milner. his book on pi calculus completely blew my computational mind.

noisesmith20:04:00

yeah, this all follows up on the pi calculus work

noisesmith20:04:43

@mobileink for a much more intertaining take, there is the jepsen series by aphyr, where various claims about distsys behavior are tested under simulations to expose bugs in widely used databases and messaging systems. Also, jepsen was implemented in clojure https://aphyr.com/tags/jepsen

mobileink20:04:27

fwiw, robert soare is worth a look. most all gis stuff is way over my head but he has interesting things to say about the topic - like, it's really all about what's not computable, or ath like that. his magnum opus came out recently: http://www.springer.com/us/book/9783642319327

mobileink21:04:09

yeah, check out his essays too at http://www.people.cs.uchicago.edu/~soare/Turing/ really eye-opening.

josh.freckleton21:04:50

To those using emacs, I enjoy clojure-align but is there a way to unalign? Ex: collapse all the space between, say, a maps keys and values to a single space?

qqq21:04:27

wow, clojure-align is awesome

josh.freckleton21:04:08

ya, use it on lets, {...}s, everything. I love it with paredit-reindent-defun

josh.freckleton21:04:50

(`M-q` and M-Q for me, super handy)

josh.freckleton21:04:48

original question remains though, can I somehow get a clojure-unalign

qqq21:04:07

if you don't mind mungling some strings, how about s/\s+/ /g it finds adjacent spaces and reduces them to 1 space

qqq21:04:14

I guess you'd have to reindent after that

josh.freckleton21:04:59

do you know of any fn, say in paredit, that would let me limit that to the current s-exp, or something?

qqq21:04:50

perhaps paredit-point-at-sexp-start and paredit-point-at-sexp-end ?

qqq21:04:56

you could use that to define a region

josh.freckleton21:04:21

I'll see what I can do, thanks!

josh.freckleton21:04:41

i think that's enough to roll with!

qqq21:04:44

there goes an afternoon 🙂

josh.freckleton21:04:02

right! I'll have to do it prob this evening

josh.freckleton22:04:49

is there an analog to haskell's maybe? ie https://www.stackage.org/lts-8.8/hoogle?q=maybe or simply (defn maybe [b fab maybea] (if (nil? a) b (fab a))) (i like sticking to the core fns though)

josh.freckleton22:04:22

fnil comes close, but not quite

noisesmith22:04:57

josh.freckleton I think the way I use some-> and some->> scratches a similar itch while not at all being the same thing

noisesmith22:04:10

(short circuiting a chain as calls, as soon as one step is nil)

josh.freckleton22:04:35

ah yes, these! I'd forgotten about em, thanks!

qqq22:04:51

why doesn't merge use transients for hashmaps? 🙂

tbaldridge22:04:42

It could probably, submit a patch

qqq22:04:21

I have a function like this: ` (defn tmap [obj lst] (reduce (fn [tobj [k v]] (if (nil? v)

qqq22:04:27

(defn tmap [obj lst]
  (persistent!
  (reduce
    (fn [tobj [k v]]
      (if (nil? v)
        (dissoc! tobj k)
        (assoc! tobj k v)))
    (transient! obj)
    lst)))

qqq22:04:37

that seems better thought out

Alex Miller (Clojure team)22:04:03

If only someone would do some perf tests, I would screen it 😃