Matrix transposition in clojure
The usual solution is (defn transpose [m] (apply mapv vector m))
The usual solution is (defn transpose [m] (apply mapv vector m))
(:color my-car) is fairly standard. There are a few reasons for this, and I won’t go into all of them. But here’s an example. Because :color is a constant, and my-car is not, hotspot can completely inline the dynamic dispatch of color.invoke(m), which it can’t do with m.invoke(color) (in some java pseudo-code). That gets even … Read more
Zip is when you take two input sequences, and produce an output sequence in which every two elements from input sequences at the same position are combined using some function. An example in Haskell: Input: zipWith (+) [1, 2, 3] [4, 5, 6] Output: [5, 7, 9] The above is a more generic definition; sometimes, … Read more
reduce and apply are of course only equivalent (in terms of the ultimate result returned) for associative functions which need to see all their arguments in the variable-arity case. When they are result-wise equivalent, I’d say that apply is always perfectly idiomatic, while reduce is equivalent — and might shave off a fraction of a … Read more
In Prolog’s terms, condA is “soft cut” a.k.a. *->, where A *-> B ; C is like (A, B ; not(A), C), only better ; whereas condU is “committed choice”, a combination of once and a soft cut so that (once(A) *-> B ; false) expresses (A, !, B) (with the cut inside): condA: A *-> … Read more
The difference is that for builds a lazy sequence and returns it while doseq is for executing side-effects and returns nil. user=> (for [x [1 2 3]] (+ x 5)) (6 7 8) user=> (doseq [x [1 2 3]] (+ x 5)) nil user=> (doseq [x [1 2 3]] (println x)) 1 2 3 nil … Read more
Since Java varargs are actually arrays, you can call vararg functions in Clojure by passing an array. You could convert a Clojure seq (maybe by using Clojure’s variety of variable argument functions) into an array: (TestClass/aStaticFunction (into-array Integer [(int 1),(int 2)])) or (defn a-static-function-wrapper [& args] (TestClass/aStaticFunction (into-array Integer args)) Or make an array and … Read more
You might like to read this paper by Richard Gabriel. It is a summary of the issues that the Lisp community were discussing in Lisp1 vs Lisp2. It’s a bit dense and slow moving in the first few sections, but is much easier to read by the time you get past section 5. Basically, Lisp1 … Read more
There are several reasons why one should not use EVAL. The main reason for beginners is: you don’t need it. Example (assuming Common Lisp): EVALuate an expression with different operators: (let ((ops ‘(+ *))) (dolist (op ops) (print (eval (list op 1 2 3))))) That’s better written as: (let ((ops ‘(+ *))) (dolist (op ops) … Read more
You can use mapcat and an inner map like this: user=> (mapcat (fn [as b] (mapv #(vector % b) as)) [[1 2] [3 4]] [5 6]) ([1 5] [2 5] [3 6] [4 6])