This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-09-19
Channels
- # announcements (41)
- # babashka (25)
- # beginners (11)
- # calva (15)
- # clj-kondo (34)
- # clojure (25)
- # clojure-france (2)
- # clojurescript (69)
- # conjure (1)
- # cursive (23)
- # datalog (3)
- # datomic (4)
- # deps-new (2)
- # emacs (31)
- # helix (1)
- # hoplon (1)
- # lsp (8)
- # luminus (17)
- # malli (5)
- # meander (1)
- # nrepl (7)
- # off-topic (1)
- # polylith (6)
- # portal (3)
- # reitit (15)
- # shadow-cljs (1)
- # xtdb (16)
Hello! kind of noob question:
I need a function that should receive a vector of sequential int numbers and map to another vector that will have 1
s if the number changed comparing with previous element and 0
if not.
example:
(is (= [0 0 0 1 0 1 0 0 0 1 0 0]
(foo [0 0 0 1 1 2 2 2 2 3 3 3])))
I managed to find a solution, but it doesn't looks that easy/fast 😅
Does anyone knows if there is a better/simpler way?my solution:
(defn foo [input]
(->> input
(map-indexed (fn [i e]
(if-let [prev-e (nth input (dec i) nil)]
(if (= e prev-e)
(loop [j (->> i dec dec)]
(if-let [other-e (nth input j nil)]
(if (= other-e prev-e)
(recur (dec j))
0)
0))
1)
e)))
vec))
I'm not sure how to proper deal with the special case of the first value, but something like that is helpful to you?
(defn foo [input]
(->> (cons (first input) input)
(partition 2 1)
(mapv (fn [[prev curr]]
(if (= prev curr) 0 1)))))
EDIT: hack to force a pair of the first item, so [] produces []Here are a couple of meh solutions...
(defn foo [c]
(reduce-kv (fn [a k v]
(cond
(= k 0) [0]
(= (nth c (dec k)) v) (conj a 0)
:else (conj a 1)))
[]
c))
and
(defn foo [c]
(->> (reduce (fn [{:keys [acc prev]} v]
(if (= v prev)
{:acc (conj acc 0) :prev v}
{:acc (conj acc 1) :prev v}))
{:acc [] :prev (first c)}
c)
:acc))
(defn problem
[nums]
(mapv #(= (nth nums %) (nth nums (dec %)))
(range 1 (count nums))))
returns booleans for all adjacent pairsFor comparing consecutive elements, I like using map
's extra args (map my-compare xs (next xs))
here's an implementation of foo:
(defn foo [xs]
(->> (map not= xs (next xs))
(map {true 1
false 0})
(cons 0)))
Damm, I didn't know about that extra map arg, thank you so much all!
I'll later replace mine with which the one that fits better for my case
A bit from a mathematical angle, this is a discrete derivative of the sequence Practically, you can map (comp abs compare) on the signal + shifted signal
it's basically @U7RJTCH6J’s solution
(cmd)user=> (let [signal [0 0 0 1 1 2 2 2 2 3 3 3] shifted-signal (rest signal)]
(map (comp #(Math/abs %) compare) signal shifted-signal))
(0 0 1 0 1 0 0 0 1 0 0)
which is missing a leading 0
jira + clojure: do you have any opinions what a good way to interact with jira is ? (use case is creating tickes atm)
Hi! I have a question: 1. Is there a way to fully describe a nested spec? When I describe a spec it stops at the top level, is there a method that describes recursively or something?
If I understand correctly your question you should find the answer here https://clojure.wladyka.eu/posts/form-validation/
Also, is Schema still used these days? It seems much better in terms of actually describing what the data will look like
@ggfpc12495 Schema is still very much alive and used, although you don't hear about it a lot in the "news"
Malli (#malli) has a similar way of representing specifications as data, if you are into that.
there are sonar plugins for Clojure that use things like clj-kondo and I think also nvd-checker type stuff
Is there a way to use expresso to solve a set of simultaneous equations where it's smart enough not to use superfluous equations that reintroduce unknowns? Example mortgage payment calculator:
(def mortgage-equations
[(x/ex (= monthly-rate (/ rate 12)))
(x/ex (= number-of-payments (* years 12)))
(x/ex (= interest-factor (** (+ 1 monthly-rate) number-of-payments)))
(x/ex (= total-payment (* principal interest-factor)))
;; If I add these two superfluous equations I get a result with unknowns in
;; it; if I remove them, I get the number I want
(x/ex (= monthly-pi (/ total-payment number-of-payments)))
(x/ex (= monthly-piti (+ monthly-pi monthly-property-tax monthly-insurance)))])
(apply x/solve 'total-payment
(x/ex (= rate 235E-4))
(x/ex (= years 30))
(x/ex (= principal 1E6))
mortgage-equations)
;; => #{{total-payment (* -360N (+ monthly-property-tax monthly-insurance (- monthly-piti)))}}
;; was expecting a number
I'm sure there's an answer involving reordering but that seems brittle 🙂Please try to encapsulate code in tripled backticks or use shift + ctrl + alt + c
to insert a code block. It's more readable that way 🙂 https://slack.com/intl/en-fr/help/articles/202288908-Format-your-messages