“Не мог разрешить символ” ошибка

Подобный вышеупомянутому, но я думаю, что лучшая версия (немного изменена) от "perldoc-f readdir":

opendir(DIR, $somedir) || die "can't opendir $somedir: $!";
@dots = grep { (!/^\./) && -f "$somedir/$_" } readdir(DIR);
closedir DIR;
5
задан Matt Fenwick 3 February 2012 в 18:28
поделиться

2 ответа

I don't know why you're having problems compiling this with :gen-class but I wouldn't be surprised if eval had something to do with it. eval is usually a bad idea. One thing to try (completely untested) is to use ` (backquote) instead of ' (quote) so that your symbols are namespace-qualified. Don't know if that'd help or not.

Probably better to get rid of eval though. If you turn your random-character functions into infinite lazy seqs via repeatedly you can do it this way:

(defn- random-letter [] (repeatedly #(char (+ (rand-int 26) 97))))
(defn- random-digit  [] (repeatedly #(rand-int 10)))
(defn- random-password
  "Returns an 8-character password consisting of letters and digits as follows: aa1aa1aa"
  []
  (apply str
         (mapcat (fn [[n f]] (take n (f)))
                 [[2 random-letter]
                  [1 random-digit]
                  [2 random-letter]
                  [1 random-digit]
                  [2 random-letter]])))
7
ответ дан 13 December 2019 в 22:10
поделиться

In the top handful of lines, I'm having a bit of trouble following the syntax. In particular, why all the quotes in line 7? Delayed evaluation of all those expressions is probably not helping you. I would guess that the quoted '(random-letter) is spoiling your fun.

You can probably write simpler code while eschewing eval. I'm going to go try it in the REPL, I hope to be back soon with an improved version.

EDIT:

OK, this works:

(apply str (interpose (random-digit) (repeat 3 (apply str (repeat 2 (random-letter))))))

...and it doesn't need anything from clojure.contrib :)

The str function will mung any arguments together into a string. If the arguments are in a list, you smuggle str inside the list by using apply.

As you said, Clojure is cool!

EDIT:

Here's a function that generates a random string of alphas and numerics in accordance with a String specification:

(apply str (map (fn [c] (if (= c \a) (random-letter) (random-digit))) "aanaanaa")))

It deviates a little bit from your spec but I think it's pretty cool.

3
ответ дан 13 December 2019 в 22:10
поделиться