Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

Hello all, I am back with another question I am stuck on.

I am at a point where my program has two components that I am trying to combine.

One is where I created a legitimate query that I would like to fuzz. For example my program randomly creates the following map:

{:select [:*], :from [:myTable], :join [[:myTable :a1]]}

The second portion takes the table and column names and creates a file, like tables.txt, with a different permutation of the string on every line.

I am stuck on how to combine them.

So I have two lists taken by reading the (long) files.

table = ("my�able" "+my+my+myyTa�me" "myT+/+///v+/v/le" ...
columns = ("name" "nAmme" ...)

and a map:

{:select [:*], :from [:myTable], :join [[:myTable :a1]]}

which after replacing the values will go to a normalization function and be executed.

So how can I iterate through both lists and replace the values in a functional/clojure type way?

for example

{:select [:*], :from [:myTable], :join [[:myTable :a1]]}
{:select "name", :from "my�able", :join [[:myTable :a1]]}
{:select "nAmme", :from "+my+my+myyTa�me", :join [[:myTable :a1]]}
and so on until I iterate through every item in the lists.

I'm open to any suggestions!


1 Answer

+1 vote
edited by

clojure.core/map accepts a function and a variable number of seq args, and will process the seqs by mapping the function to the next element drawn from each sequence, e.g.

user>(map + [1 2 3] [3 4 5])
(4 6 8)    

So you can just use it directly with a function that builds the map query thing:

(def table ["my�able" "+my+my+myyTa�me" "myT+/+///v+/v/le"])
(def columns ["name" "nAmme"])
(defn process  [table col]
    {:select col :from table :join [[:myTable :a1]]})`

You will get a lazy sequence of results (in this case, the query maps):

user> (map process table columns)
({:select "name", :from "my�able", :join [[:myTable :a1]]}
 {:select "nAmme", :from "+my+my+myyTa�me", :join [[:myTable :a1]]})

This would work well with functions that generate e.g. random sequences of
tables and columns to allow you derive a (potentially infinite) lazy sequence
of query maps. There are other options using reduce, transducers, iterate, even
loop, but I think this is pretty straightforward, for the example given at least.