logo
down
shadow

Why there is no not-nil? in Clojure?


Why there is no not-nil? in Clojure?

Content Index :

Why there is no not-nil? in Clojure?
Tag : clojure , By : Marc
Date : November 18 2020, 03:49 PM

Hope this helps In clojure I noticed functions with logical opposites like [[if x if-not] [every? x not-every]]. I wonder why [not-nil?] is not included, is it on purpose or because it is too trivial. , There is some? since Clojure 1.6:
clojure.core/some?
([x])
Returns true if x is not nil, false otherwise.

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Roundtripping xml in Clojure using clojure.xml/parse and clojure.xml/emit


Tag : xml , By : Webtoad
Date : March 29 2020, 07:55 AM
should help you out I've checked the source quickly and clojure.xml/emit-element (which gets called by clojure.xml/emit) makes no effort whatever to encode any characters as XML entities; in fact, it lets attribute values straight through. I guess this means clojure.xml is quite limited in its usability; you should use clojure.contrib.lazy-xml instead. My apologies for not mentioning it in the answer to your first question on XML emitting, I didn't realise stuff like this would happen.
With clojure.contrib.lazy-xml, you can do the following:
user> (lazy-xml/emit
       (lazy-xml/parse-trim
        (java.io.StringReader. "<foo bar=\"&apos;&quot;&quot;&apos;\"/>")))
<?xml version="1.0" encoding="UTF-8"?><foo bar="'&quot;&quot;'"/>

How to prevent Clojure exception: clojure.lang.LazySeq cannot be cast to clojure.lang.IFn


Tag : clojure , By : xguru
Date : March 29 2020, 07:55 AM
Any of those help I am not entirely sure what you're after, but I can see why your code fails - like you said, trimmed-cols is not a function. Wouldn't this simply work?
I'm using :_dummy keys for columns that should be skipped. You can use as many as you like in the column-names vector: zipmap will merge them into one, which is removed by dissoc.
(defn- column-map
  "Produce map with column labels given raw data, return nil if not enough columns"
  [cols]
  (let [trimmed-cols (map str/trim cols)
        column-names [:fixture-type :division :home-team :_dummy :away-team]]
    (if (>= (count cols) (count column-names))
      (dissoc (zipmap column-names trimmed-cols) :_dummy)
      nil)))

(column-map ["L" "   Premier " "  Chelsea  " "v" "\tArsenal  "])
; => {:away-team "Arsenal", :home-team "Chelsea", :division "Premier", :fixture-type "L"}

clojure.lang.LazySeq cannot be cast to clojure.lang.IFn nested for loops clojure


Tag : development , By : GunnarHafdal
Date : March 29 2020, 07:55 AM
around this issue The problem is ((for ....)) That says evaluate the (for ....) form, and then take that result and execute it. The (for ...) is returning a LazySeq, instead of a function.
(apply map + [[1 0 1] [0 1 0] [0 0 1]])
;; (1 1 2)
(apply mapv + [[1 0 1] [0 1 0] [0 0 1]])
;; [1 1 2]

How to load single clojure file with dependencies using Clojure REPL (La Clojure)?


Tag : intellij-idea , By : Steven Weber
Date : March 29 2020, 07:55 AM
it fixes the issue What leads to the error is that classpath (the one used to start Clojure REPL) doesn't include the clojure.tools.logging library. Test.clj requires it, so REPL, upon loading it, fails to find the required package, hence the error.
I'm not sure how you start the REPL, but the rest of the answer assumes that this is a regular REPL in a terminal (not from the IntelliJ IDEA itself; if the REPL is started from IntelliJ IDEA - I would say it is the misconfiguration of that last one, because it is supposed to start REPL with the correct project classpath - first thing to check would be the project configuration).
(defproject my-project "0.0.1-SNAPSHOT"
  :dependencies [[org.clojure/tools.logging "0.3.1"]])
$ lein repl 
...
user=> (load-file "Test.clj")
#'test.Test/addx
user=> (test.Test/addx 13 9 20)
42
$ java -cp /path/to/clojure.jar:/path/to/tools.logging-0.3.1.jar clojure.main

How to get rid of clojure/lang/RT.aset and clojure/lang/RT.intCast in clojure array processing?


Tag : arrays , By : afarouk
Date : March 29 2020, 07:55 AM
should help you out How are you benchmarking this code? I recommend using something like criterium or at least doing many executions before comparing times. Things like the checkcast should get optimized out by the JIT when it's warm enough. I also recommend using latest JVM, -server, and -XX:+AggressiveOpts.
Generally I find it's better to not try to force Clojure to use ints in loops - instead embrace longs as the loop counter, use (set! *unchecked-math* true), and let Clojure downcast the longs to ints when indexing into the array. While this seems like extra work, my impression on modern hardware/JVM/JIT is that the difference is a lot less than you expect (as you're mostly working with 64-bit integers anyways). Also, it looks like you are carrying size as a loop variable but it never changes - maybe you were doing that to avoid type mismatch with i, but I would just let size (as a long) prior to the loop and do long increments and compares on i instead.
(set! *unchecked-math* :warn-on-boxed)

(def ^long ^:const size 65536)

(defn get-zero-complex-array []
  {:re (double-array size)
   :im (double-array size)})

(defn multiply-complex-arrays [a b]
  (let [a-re-array (doubles (get a :re))
        a-im-array (doubles (get a :im))
        b-re-array (doubles (get b :re))
        b-im-array (doubles (get b :im))
        res-re-array (double-array size)
        res-im-array (double-array size)
        s (long size)]
    (loop [i 0]
      (if (< i s)
        (let [a-re (aget a-re-array i)
              a-im (aget a-im-array i)
              b-re (aget b-re-array i)
              b-im (aget b-im-array i)]
          (aset res-re-array i (- (* a-re b-re) (* a-im b-im)))
          (aset res-im-array i (+ (* a-re b-im) (* b-re a-im)))
          (recur (inc i)))
        {:re res-re-array :im res-im-array}))))

(defn compute []
  (let [res (loop [i 0 a (get-zero-complex-array)]
              (if (< i 30000)
                (recur (inc i) (multiply-complex-arrays a a))
                a))]
    (aget (get res :re) 0)))
shadow
Privacy Policy - Terms - Contact Us © scrbit.com