Fork me on GitHub
#clojure-spec
<
2016-08-18
>
magnetophon08:08:13

I have a spec for a sorted set of positive integers in a wide range (say 0 to 1000000). I want those to be keys in a map. how do I spec/def that map? (I'm pretty new to clojure, so I hope I'm using the right terms here)

magnetophon08:08:19

In the normal use-case, there will only be up to a few dozen keys in the map.

nha12:08:43

(Not sure if this is the right place to ask) When using clojure.spec with ClojureScript, is it able to figure out which keys are being used in order to minimize the build size ?

alexmiller12:08:23

@magnetophon: does map-of satisfy?

alexmiller13:08:39

@nha I don’t know, but I would guess not. @dnolen would know.

dnolen13:08:21

@nha Dead Code Elimination (DCE) is purely a Google Closure thing

dnolen13:08:36

still eliminating keys is hardly going to save you anything at all - so I wouldn’t be all that worried about it

dnolen13:08:26

keywords are subject to constant optimization, i.e. single allocation and global usage

dnolen13:08:29

this also aids DCE

alexmiller13:08:48

I think probably the question is whether the spec definition and registration for an unused key can be eliminated

nha13:08:13

It was unclear, yes that is what I meant.

dnolen13:08:32

ah ok not currently, since it’s stored dynamically in an atom

nha13:08:23

Ah I see. It may not be an intended use, I just spec'ed some server-side functions and would like to reuse some of these for validation/coercion on the client side.

dnolen13:08:43

@nha I don’t see what that has to do with your question

nha13:08:52

Well embedding the validation/coercions fns for my DB (for instance) is not something I would like to have on my frontend code.

dnolen13:08:05

so split those apart

dnolen13:08:10

we’re not going to automate any of that stuff for you

nha13:08:20

Ok separate namespaces then.

nha13:08:08

Get it, thanks for your quick answers 🙂

magnetophon15:08:47

@alexmiller it partly does, but can spec ensure that each element in the set has one in the map, and the other way around?

magnetophon15:08:45

@alexmiller maybe I should be using other data structures altogether. I'm learning closure while designing this thing.

alexmiller15:08:46

to create a spec that makes assertions about a set and a map together, you will either need to spec a data structure that combines them or if you are passing them both as args together, that spec could be on the fdef args of that function

alexmiller15:08:56

both are possible, I’m just not sure what you’re doing from the description

magnetophon16:08:46

@alexmiller Thanks! Do you have an example of the combined data structure way of doing it? The guide explains how to combine structures with s/keys, but I didn't see how to assert anything about the combination.

alexmiller16:08:36

You're talking about both a map and a set so those would have to be combined into something else to talk about them together

alexmiller16:08:51

Or maybe I totally misunderstand what you mean

alexmiller16:08:14

You can combine arbitrary specs with s/and

magnetophon16:08:06

@alexmiller I'll push some code and link you to it. one sec.

magnetophon16:08:24

@alexmiller https://github.com/magnetophon/openloop/blob/master/src/openloop/states.clj#L101 is the sorted set, and #L115 is the map. how do I spec that each element has a corresponding element in the other structure?

alexmiller17:08:33

That question only makes sense in some context where both are present

alexmiller17:08:41

Either both parts of the same data value or both args in a call to the same function

magnetophon17:08:57

@alexmiller you mean, I need to define a data-structure (or function, but let's go with DS for now) that combines both, right?

alexmiller17:08:49

Where is the code that's going to use these things together? What does that need? It feels like you are doing things that are disconnected from the actual problem you are solving. Specs don't exist in a vacuum.

magnetophon17:08:41

There is no code yet. I'm brainstorming on how to model my problem by writing specs for the needed data structures. I'm learning clojure at the same time. Am I going about this the wrong way?

magnetophon17:08:20

I have a proof of concept working. now I want to add a ton of features, but I'm trying to do so in an organized way, that won't lead to too many re-writes.

magnetophon17:08:44

I thought if I have the data-structures and the states of the FSM that will be the brain of the thing, the rest of the code will be relatively easy.

magnetophon17:08:15

@alexmiller Does that make sense?

magnetophon18:08:43

I'm manipulating audio on disk into repeating loops in RAM. The data-structure in question is supposed to represent where in the disk-file each part of the RAM loop came from. I thought I'd use a sorted set for the indexes in the loop where I switch to another part of the source file, so that when I want to replace part of the audio with something else, I can easily find out which nodes to replace. The map contains, for each section, the rest of the data needed to construct the actual audio (so IOW: where does the audio come from for this section, how do I crossfade it, etc). I hope I'm making myself clear.

seancorfield18:08:12

@magnetophon: It may help to think about whether the "sorted set of indexes" is just an implementation detail of you algorithm or whether it’s an inherently important data structure in its own right?

seancorfield18:08:59

In other words, do you derive the set from the main data structure "internally", just for that piece of work, or are you passing it around "externally" along with the main data structure?

seancorfield18:08:00

If the answer is "internal", then I don’t think you need to spec it, although you may spec functions that get passed both the set and the main DS (per Alex’s comment about spec’ing via fdef).

seancorfield18:08:53

If the answer is "external", then the best solution is to pass around a higher level DS that contains both the sorted set and the "main" DS as elements — and have the spec for that higher level DS (since then it can check that the set element is consistent with the other DS element).

seancorfield18:08:57

Does that help?

magnetophon18:08:14

@seancorfield thanks. Needed some time to process that. I think the sorted set is an implementation detail. I might not even need it at all.

magnetophon18:08:01

I thought I'd need it for performance, but I think I won't: I will be adding and removing indexes regularly, and for deciding which ones to remove or update I'll have to look at the value of the indexes. In practice there will be at most a few hundred indexes, so It'll be quick enough to go over them.

magnetophon18:08:20

@alexmiller and @seancorfield Many thanks for your time and insight!

bbrinck19:08:35

I've got a variable length vector (some elements are required, some are optional). I'm using cat to create a spec to check it, which works well. But when I want to generate examples, cat generates lists. Is there a way spec a vector this way?

bbrinck20:08:50

I can achieve what I want with with-gen, but I believe it requires that I always have two specs - one for the list, as defined with cat and then another to wrap the default generator

alexmiller20:08:10

Yeah, this is a common problem (regex syntax in vector) and a reasonable workaround

alexmiller20:08:30

We have talked about creating vcat for this particular case

bbrinck20:08:18

@alexmiller Glad to know I'm not totally missing something 😄. Thanks for the help. vcat would be very useful in my application.

seancorfield20:08:02

Can’t you just use coll-of with :into [] for that?

seancorfield20:08:50

Ah, you’re talking about heterogeneous vectors, not homogeneous...

alexmiller21:08:44

Yeah. Comes up a lot in macro syntax.