Apply a key as a function to its value [duplicate] - clojure

This question already has an answer here:
using clojure symbol function to make indirect function call
(1 answer)
Closed yesterday.
Suppose you have a peculiar map where each key might represent a Clojure core function:
{:inc 1 :dec 2 :identity "three"}
What would you write to apply each key (as a function) to its own value?
Output should yield something like:
(2 1 "three")
This code fails to produce what I expect:
user=> (def mm {:inc 1 :dec 2 :identity "three"})
user=> (map #((symbol (first %)) (get % 1)) mm)
(nil nil nil)

Wrapping the symbol with resolve solves this problem:
(resolve (symbol (...)))
More detailed explanation here: https://stackoverflow.com/a/63208807/4903731
Any of the following produce the desired output:
(def mm {:inc 1 :dec 2 :identity "three"})
(map (fn [%] ((resolve (symbol (key %))) (val %))) mm)
(map #((resolve (symbol (key %))) (val %)) mm)
(map #((resolve (symbol (first %))) (get % 1)) mm)

Related

Return an else value when using recur

I am new to Clojure, and doing my best to forget all my previous experience with more procedural languages (java, ruby, swift) and embrace Clojure for what it is. I am actually really enjoying the way it makes me think differently -- however, I have come up against a pattern that I just can't seem to figure out. The easiest way to illustrate, is with some code:
(defn char-to-int [c] (Integer/valueOf (str c)))
(defn digits-dont-decrease? [str]
(let [digits (map char-to-int (seq str)) i 0]
(when (< i 5)
(if (> (nth digits i) (nth digits (+ i 1)))
false
(recur (inc i))))))
(def result (digits-dont-decrease? "112233"))
(if (= true result)
(println "fit rules")
(println "doesn't fit rules"))
The input is a 6 digit number as a string, and I am simply attempting to make sure that each digit from left to right is >= the previous digit. I want to return false if it doesn't, and true if it does. The false situation works great -- however, given that recur needs to be the last thing in the function (as far as I can tell), how do I return true. As it is, when the condition is satisfied, I get an illegal argument exception:
Execution error (IllegalArgumentException) at clojure.exercise.two/digits-dont-decrease? (four:20).
Don't know how to create ISeq from: java.lang.Long
How should I be thinking about this? I assume my past training is getting in my mental way.
This is not answering your question, but also shows an alternative. While the (apply < ...) approach over the whole string is very elegant for small strings (it is eager), you can use every? for an short-circuiting approach. E.g.:
user=> (defn nr-seq [s] (map #(Integer/parseInt (str %)) s))
#'user/nr-seq
user=> (every? (partial apply <=) (partition 2 1 (nr-seq "123")))
true
You need nothing but
(apply <= "112233")
Reason: string is a sequence of character and comparison operator works on character.
(->> "0123456789" (mapcat #(repeat 1000 %)) (apply str) (def loooong))
(count loooong)
10000
(time (apply <= loooong))
"Elapsed time: 21.006625 msecs"
true
(->> "9123456789" (mapcat #(repeat 1000 %)) (apply str) (def bad-loooong))
(count bad-loooong)
10000
(time (apply <= bad-loooong))
"Elapsed time: 2.581750 msecs"
false
(above runs on my iPhone)
In this case, you don't really need loop/recur. Just use the built-in nature of <= like so:
(ns tst.demo.core
(:use demo.core tupelo.core tupelo.test))
(def true-samples
["123"
"112233"
"13"])
(def false-samples
["10"
"12324"])
(defn char->int
[char-or-str]
(let [str-val (str char-or-str)] ; coerce any chars to len-1 strings
(assert (= 1 (count str-val)))
(Integer/parseInt str-val)))
(dotest
(is= 5 (char->int "5"))
(is= 5 (char->int \5))
(is= [1 2 3] (mapv char->int "123"))
; this shows what we are going for
(is (<= 1 1 2 2 3 3))
(isnt (<= 1 1 2 1 3 3))
and now test the char sequences:
;-----------------------------------------------------------------------------
; using built-in `<=` function
(doseq [true-samp true-samples]
(let [digit-vals (mapv char->int true-samp)]
(is (apply <= digit-vals))))
(doseq [false-samp false-samples]
(let [digit-vals (mapv char->int false-samp)]
(isnt (apply <= digit-vals))))
if you want to write your own, you can like so:
(defn increasing-equal-seq?
"Returns true iff sequence is non-decreasing"
[coll]
(when (< (count coll) 2)
(throw (ex-info "coll must have at least 2 vals" {:coll coll})))
(loop [prev (first coll)
remaining (rest coll)]
(if (empty? remaining)
true
(let [curr (first remaining)
prev-next curr
remaining-next (rest remaining)]
(if (<= prev curr)
(recur prev-next remaining-next)
false)))))
;-----------------------------------------------------------------------------
; using home-grown loop/recur
(doseq [true-samp true-samples]
(let [digit-vals (mapv char->int true-samp)]
(is (increasing-equal-seq? digit-vals))))
(doseq [false-samp false-samples]
(let [digit-vals (mapv char->int false-samp)]
(isnt (increasing-equal-seq? digit-vals))))
)
with result
-------------------------------
Clojure 1.10.1 Java 13
-------------------------------
Testing tst.demo.core
Ran 2 tests containing 15 assertions.
0 failures, 0 errors.
Passed all tests
Finished at 23:36:17.096 (run time: 0.028s)
You an use loop with recur.
Assuming you require following input v/s output -
"543221" => false
"54321" => false
"12345" => true
"123345" => true
Following function can help
;; Assuming char-to-int is defined by you before as per the question
(defn digits-dont-decrease?
[strng]
(let [digits (map char-to-int (seq strng))]
(loop [;;the bindings in loop act as initial state
decreases true
i (- (count digits) 2)]
(let [decreases (and decreases (>= (nth digits (+ i 1)) (nth digits i)))]
(if (or (< i 1) (not decreases))
decreases
(recur decreases (dec i)))))))
This should work for numeric string of any length.
Hope this helps. Please let me know if you were looking for something else :).
(defn non-decreasing? [str]
(every?
identity
(map
(fn [a b]
(<= (int a) (int b)))
(seq str)
(rest str))))
(defn non-decreasing-loop? [str]
(loop [a (seq str) b (rest str)]
(if-not (seq b)
true
(if (<= (int (first a)) (int (first b)))
(recur (rest a) (rest b))
false))))
(non-decreasing? "112334589")
(non-decreasing? "112324589")
(non-decreasing-loop? "112334589")
(non-decreasing-loop? "112324589")

Conditional composition operator

I'd like to know if there exists some built-in function composition operator in clojure allowing me to rewrite something like:
(def first-or-identity #(if (sequential? %) (first %) (identity %)))
into a shorter:
(def first-or-identity (if-composition sequential? first identity)
--
The use case would be to be able to write something along those lines:
(def eventbus-pub
(async/pub eventbus (if-composition sequential? first identity)))
Thanx!
You can do this with a function:
(defn if-composition [tester truer falser]
(fn [x]
(if (tester x) (truer x) (falser x))))
For example,
(map
(if-composition even? #(quot % 2) #(inc (* 3 %)))
(range 10))
;(0 4 1 10 2 16 3 22 4 28)
It's worth while making the last argument identity by default:
(defn if-composition
([tester truer] (if-composition tester truer identity))
([tester truer falser]
... ))
For example,
(map (if-composition odd? #(* 2 %)) (range 10))
;(0 2 2 6 4 10 6 14 8 18)
Now we can write your example as
(def first-or-identity (if-composition sequential? first))

fine tune range`s laziness [duplicate]

This question already has answers here:
How do I avoid Clojure's chunking behavior for lazy seqs that I want to short circuit?
(3 answers)
Closed 8 years ago.
This code:
(first (map (fn [d]
(apply * (repeat d 10)))
(range)))
yealds an integer overflow exception, while this code:
(first (map (fn [d]
(apply * (repeat d 10)))
(range 0 1)))
yealds 1.
Both codes should yeald 1 but for some reason the laziness of range has a strange behaviour. It seems to get chuncks of data instead of only one at a time. Is it possible to make range behave in the desired way?
range is chunked, as a performance optimization. There are a few ways to fix this including using (iterate inc 0) or unchunk (there are a few versions, this one is copied from math.combinatorics)
(defn unchunk
[s]
(lazy-seq
(when (seq s)
(cons (first s)
(unchunk (rest s))))))
user=> (first (map (fn [d]
(apply * (repeat d 10)))
(unchunk (range)))
1

Clojure multimethod dispatching on functions and values

I have a function that returns the indexes in seq s at which value v exists:
(defn indexes-of [v s]
(map first (filter #(= v (last %)) (zipmap (range) s))))
What I'd like to do is extend this to apply any arbitrary function for the existence test. My idea is to use a multimethod, but I'm not sure exactly how to detect a function. I want to do this:
(defmulti indexes-of ???)
(defmethod indexes-of ??? [v s] ;; v is a function
(map first (filter v (zipmap (range) s))))
(defmethod indexes-of ??? [v s] ;; v is not a function
(indexes-of #(= v %) s))
Is a multimethod the way to go here? If so, how can I accomplish what I'm trying to do?
If you want to use a multimethod it should be on the filter function, which is the one changing according to the existence test type.
So
(defmulti filter-test (fn [value element]
(cond
(fn? value) :function
:else :value)))
(defmethod filter-test :function
[value element]
(apply value [element]))
(defmethod filter-test :value
[value element]
(= value element))
(defn indexes-of [v s]
(map first (filter #(filter-test v (last %)) (zipmap (range) s))))
Consider the JVM doesn't support first-class functions, or lambdas, out of the box, so there's no "function" data type to dispatch on, that's the reason the fn? test.
None the less the predicate solution proposed by noisesmith is the proper way to go in this situation IMO.
(defmulti indexes-of (fn [v _]
(if (fn? v)
:function
:value)))
(defmethod indexes-of :function
[f coll]
(keep-indexed (fn [i v] (when (f v) i)) coll))
(defmethod indexes-of :value
[v coll]
(indexes-of (partial = v) coll))
How about something simpler and more general:
(defn index-matches [predicate s]
(map first (filter (comp predicate second) (map vector (range) s))))
user> (index-matches even? (reverse (range 10)))
(1 3 5 7 9)
user> (index-matches #{3} [0 1 2 3 1 3 44 3 1 3])
(3 5 7 9)
thanks to a suggestion from lgrapenthin, this function is also now effective for lazy input:
user> (take 1 (index-matches #{300000} (range)))
(300000)

Calling a Function From a String With the Function’s Name in Clojure

How could I call a function with a string? e.g. something like this:
(call "zero?" 1) ;=> false
Something like:
(defn call [^String nm & args]
(when-let [fun (ns-resolve *ns* (symbol nm))]
(apply fun args)))
A simple answer:
(defn call [this & that]
(apply (resolve (symbol this)) that))
(call "zero?" 1)
;=> false
Just for fun:
(defn call [this & that]
(cond
(string? this) (apply (resolve (symbol this)) that)
(fn? this) (apply this that)
:else (conj that this)))
(call "+" 1 2 3) ;=> 6
(call + 1 2 3) ;=> 6
(call 1 2 3) ;=> (1 2 3)