I did this experiment in the REPL:
(sort ["maa" "ácw" "ijl" "aez" "jkl"])
I got this:
("aez" "ijl" "jkl" "maa" "ácw")
The right answer is this:
("ácw" "aez" "ijl" "jkl" "maa")
Is there a way to sort Latin strings in Clojure natively? If not why not?
Look I didn't ask how to sort this, I know I can pass the seq through a pipe and substitute the non ANSI chars.
You can use the Collator class, on the REPL:
=> (import java.util.Locale)
java.util.Locale
=> (import java.text.Collator)
java.text.Collator
=> (def collator (Collator/getInstance (Locale. "pt_BR")))
#'user/collator
=> (sort collator ["maa" "ácw" "ijl" "aez" "jkl"])
("ácw" "aez" "ijl" "jkl" "maa")
In this example I'm using the Brazilian Locale. You need to change this locale to the one you want to use. There is a list of supported locales here.
Related
In the Om Next Quick Start, they use #js and #uuid. What does the pound symbol here mean?
Link: https://github.com/omcljs/om/wiki/Quick-Start-(om.next)#components-with-queries--mutations
Snippets:
#js:
(defui Counter
static om/IQuery
(query [this]
[:count])
Object
(render [this]
(let [{:keys [count]} (om/props this)]
(dom/div nil
(dom/span nil (str "Count: " count))
(dom/button
#js {:onClick
(fn [e] (om/transact! this '[(increment)]))}
"Click me!")))))
#uuid:
(om/from-history reconciler
#uuid "9e7160a0-89cc-4482-aba1-7b894a1c54b4")
Commonly found in EDN and ClojureScript this use of # is called the tagged literal. Look at this example,
user=> (java.util.Date.)
#inst "2014-05-19T19:12:37.925-00:00"
When we create a new date it is represented as a tagged literal, or in this case a tagged string. We can use Clojures read-string to read this back (or use it directly)
user=> (type #inst "2014-05-19T19:12:37.925-00:00")
java.util.Date
(read-string "#inst \"2014-05-19T19:12:37.925-00:00\"")
#inst "2014-05-19T19:12:37.925-00:00"
user=> (type (read-string "#inst \"2014-05-19T19:12:37.925-00:00\""))
java.util.Date
A tagged literal tells the reader how to parse the literal value. Other common uses include #uuid for generating UUIDs and in the ClojureScript world an extremely common use of tagged literals is #js which can be used to convert ClojureScript data structures into JavaScript structures directly.
Courtesy: The Weird and Wonderful Characters of Clojure
Let's call this function "dynamic-define".
Basically I want to write a macro or lambda that works like this:
$ (dynamic-define "variable" 123)
$ variable
$ => 123
I tried it:
(define-syntax dynamic-define
(syntax-rules ()
((_ string <body>)
(eval `(define ,(string->symbol string) <body>)))))
and it works as expected but doesn't seem to be a good solution.
I tried to use without eval like this:
(define-syntax dynamic-define
(syntax-rules ()
((_ string <body>)
(define (string->symbol string) <body>))))
But when I try to use I get this error:
Error: invalid syntax (define (string->symbol "variable") 123)
What should I do?
(PLEASE: edit this question to fit native English and erase this line please)
You're running against a fundamental problem: you cannot do a real dynamic define in a "clean" way, hence your attempt using eval. Note that the two solutions above are both not dynamic -- all they give you is the ability to write "foo" instead of foo. As I said elsewhere, using eval is usually bad, and in this case it's worse since it's eval that is used from a macro which makes it very weird. This is in addition to having an implicit quasi-quote in your macro that makes things even more confusing. You could just as well define the whole thing as a plain function:
(define (dynamic-define string <body>)
(eval `(define ,(string->symbol string) ,<body>)))
If you really need this to work, then it's best to take a step back and think about what it is that you need, exactly. On one hand, the above solutions are not dynamic since they require using the macro with a string syntax
(dynamic-define "foo" 123) ; instead of (define foo 123)
but how would it look to have a real dynamic definition that takes in a string? You'd probably expect this to define b:
(define a "b")
(dynamic-define a 123)
but this only becomes more confusing when you consider how it interacts with other bindings. For example:
(define y 123)
(define (foo s)
(define x 456)
(dynamic-define s 789)
(+ x y))
Now, assuming that dynamic-define does what you want it to do, think about what you'd get with (foo "x") and (foo "y"). Worse, consider (foo "s") and (foo "+"). And even worse, what about
(foo (random-element '("x" "y" "s" "+" "define")))
? Clearly, if this dynamic-define really does some dynamic definition, then there is no sense that you can make of this code without knowing ahead of time what name foo will be called with. Without being able to make such sense, compilation goes out the window -- but much more importantly, correctness (or generally, the meaning of your code) dies.
In my experience, this kind of pattern is much better handled with hash tables and similar devices.
using define-macro
#> (define-macro (dynamic-define varstr val)
`(define ,(string->symbol varstr) ,val))
#> (dynamic-define "variable" 123)
#> variable
123
using syntax-case
#> (define-syntax dynamic-define
(lambda (stx)
(syntax-case stx ()
((k varstr val)
(with-syntax ([var (datum->syntax-object
#'k
(string->symbol (syntax-object->datum #'varstr)))])
#'(define var val))))))
#> (dynamic-define "variable" 123)
#> variable
123
How to add a common prefix/suffix to a list of strings?
For example:
From ("abc" "123" "xy")
To ("pre_abc" "pre_123" "pre_xy")
Try
(mapcar (lambda (c) (concat "pre_" x)) '("abc" "123" "xy"))
In Emacs without third-party libraries you use mapcar function to apply a function to every element of the list, as #sds have shown. However if you program in Elisp heavily, i recommend installing 2 third-party libraries, dash.el for list manipulation and s.el for string manipulation. These libraries introduce a huge amount of auxiliary functions with a consistent naming scheme. dash.el also have anaphoric versions for many functions, which makes code even less verbose. So to add a prefix for every string in a list you could use --map and s-prepend (s-append for suffixes):
(--map (s-prepend "x" it) '("a" "b" "c")) ;; => ("xa" "xb" "xc")
The operation to prepend is (concatenate 'string <strings ...)
So you could achieve your goal with
(cl-loop
for item in '("abc" "123" "xy")
collect (concatenate 'string "pre_" item))
A more general way to compose/generate strings would be to use format
using format would be
(cl-loop
for item in '("abc" "123" "xy")
collect (format nil "pre_~S" item))
Note using cl-lib package
I just started to learn Erlang, and really like their list comprehension syntax, for example:
Weather = [{toronto, rain}, {montreal, storms}, {london, fog}, {paris, sun}, {boston, fog}, {vancounver, snow}].
FoggyPlaces = [X || {X, fog} <- Weather].
In this case, FoggyPlaces will evaluate to "london" and "boston".
What's the best way to do this in Ruby?
For example, an Array like (very common, I believe):
weather = [{city: 'toronto', weather: :rain}, {city: 'montreal', weather: :storms}, {city: 'london', weather: :fog}, {city: 'paris', weather: :sun}, {city: 'boston', weather: :fog}, {city: 'vancounver', weather: :snow}]
The best I got 'til now is:
weather.collect {|w| w[:city] if w[:weather] == :fog }.compact
But in this case, I have to call compact to remove nil values, and the example itself is not that readable as Erlang.
And even more, in the Erlang example, both city and weather are atoms. I don't even know how to get something that makes sense and looks good like this in Ruby.
First off, your data structures aren't equivalent. The equivalent Ruby data structure to your Erlang example would be more like
weather = [[:toronto, :rain], [:montreal, :storms], [:london, :fog],
[:paris, :sun], [:boston, :fog], [:vancouver, :snow]]
Secondly, yes, Ruby doesn't have list comprehensions nor pattern matching. So, the example will probably be more complex. Your list comprehension first filters all foggy cities, then projects the name. Let's do the same in Ruby:
weather.select {|_, weather| weather == :fog }.map(&:first)
# => [:london, :boston]
However, Ruby is centered around objects, but you are using abstract data types. With a more object-oriented data abstraction, the code would probably look more like
weather.select(&:foggy?).map(&:city)
which isn't too bad, is it?
I'm a novice in clojure and java.
In order to access a Java field in Clojure you can do:
Classname/staticField
which is just the same as
(. Classname staticField)
(correct me if I'm wrong)
How can I access a static field when the name of the field in held within a variable? i.e.:
(let [key-stroke 'VK_L
key-event KeyEvent/key-stroke])
I want key-stroke to be evaluated into the symbol VK_L before it tries to access the field.
In this case you might want to do something like the following:
user=> (def m 'parseInt)
#'user/m
user=> `(. Integer ~m "10")
(. java.lang.Integer parseInt "10")
user=> (eval `(. Integer ~m "10"))
10
If you feel like it's a bit hack-ish, it's because it really is such. There might be better ways to structure the code, but at least this should work.
I made a small clojure library that translates public fields to clojure maps using the reflection API:
(ns your.namespace
(:use nl.zeekat.java.fields))
(deftype TestType
[field1 field2])
; fully dynamic:
(fields (TestType. 1 2))
=> {:field1 1 :field2 2}
; optimized for specific class:
(def-fields rec-map TestType)
(rec-map (TestType. 1 2))
=> {:field1 1 :field2 2}
See https://github.com/joodie/clj-java-fields
Reflection is probably the proper route to take, but the easiest way is
(eval (read-string (str "KeyEvent/" key-stroke)))
This will just convert the generated string into valid clojure and then evaluate it.
You can construct the right call as an s-expression and evaluate it as follows:
(let [key-stroke 'VK_L
key-event (eval `(. KeyEvent ~key-stroke))]
....do things with your key-event......)
However, if what you are trying to do is test whether a particular key has been pressed, you may not need any of this: I usually find that it is easiest to write code something like:
(cond
(= KeyEvent/VK_L key-value)
"Handle L"
(= KeyEvent/VK_M key-value)
"Handle M"
:else
"Handle some other key")