Fork me on GitHub
Michaël Salihi10:01:32

Hi eveybody ! Which is the best way to create reusable component using Reagent & co. Eg. React have CSS module to import CSS in an component scope like :

import React from 'react';
import './SearchBar.css';
import PlacesAutocomplete from 'react-places-autocomplete';


I recently started using my own dev mainspace to open the repl with and have a few "startup" functions I use often. However, it seems that when I change my code and close/open the repl, my changes are not applied. Seems I need to use lein clean between for the changes to be active. Guessing there is some caching happening but my googlefu is not helping me


@victorbjelkholm429 how to you start your repl? dev should work like every other namespace, it’s just a naming convention.


@ordnungswidrig with lein repl. Then in project.clj I have :repl-options { :init-ns }


And when you restart you don’t see the changes?


if I do a change, then I close the repl and open it with lein repl, the function (for example) haven't got the new changes. Only if I do lein clean between close/open

Ian Fernandez14:01:53

Hi, how can I change this :

{:a 1 :v 3 :d 4}
To this:
:a 1 :v 3 :d 4

Ian Fernandez14:01:02

I'm trying to pass into a function:

(defn funct [arg & {:as ks}]
  (merge arg ks))

Ian Fernandez14:01:06

but the input does not accept maps

(funct args {:a 1 :b 3})
(funct args :a 1 :b 3)


one thing to question is if you need to? It just merges an arg with other options you pass it. But if you already have a map just call merge itself

Ian Fernandez14:01:28

I have another manipulations on args before the merge


fair enough.


(unless the definition of funct is reduced just for this example )


(apply funct args (apply concat {:a 1 :b 2})) to apply a map as keyword arguments

👌 5
Ian Fernandez14:01:19

There is no way to do something on funct to do this:

(funct args {:a 1 :v 3})


I mean, you can change func to something like this:

(defn funct [arg {:as ks}]
  (merge arg ks))
;; or
(defn funct [arg & [{:as ks}]]
  (merge arg ks))

❤️ 5

why are you using {:as ks}? It is the same as just ks

Ian Fernandez14:01:03

I'm learning map destructuring


note it is & {:as ks}


the only reason I use :as is if I'm using destructuring to parse out smaller parts of an input, but I still need access to the entire thing...

{:keys [a b c] :as m}


ah misread your comment sorry

Ian Fernandez14:01:44

I'm updating a map that came from a db and inserting in db again

Ian Fernandez14:01:58

but clojure doesn't interoperate in this db

Ian Fernandez14:01:28

and, I doing some changes in args (that is the map that I took from the db)

Ian Fernandez14:01:19

There is no way to do something on funct to do this:

(funct args {:a 1 :v 3})


you can change the signature of funct (if that is your function)


& {:as ks} feels outdated to me. Unless its on an API for a human I would try to change that (if possible)

Ian Fernandez14:01:35

That solved for me

Ian Fernandez14:01:04

There's a reference to learn more about destructuring?


There are a few, but I have this one favourited

Ian Fernandez14:01:49

thanks =D I'll study more, destructuring is beautiful


Hi guys! I'm looking for a way to map an interval of numbers to a value... example:

when the argument goes...

from 0 to 4999
the result should be 10

from 5000 to 9999
the result should be 20

and so on...
This is what I got:
(defn mapping []
  {[00, 10] 0
   [11, 20] 1
   [21, 30] 2})

(defn lower_bound [item]
  (first (key item)))

(defn upper_bound [item]
  (second (key item)))

(defn is_between [item, n]
  (<= (lower_bound item) n (upper_bound item)))

(defn get_value_by_range [n]
    #(when (is_between % n) (val %)) 
Is there a better way?


why is mapping a function?


also, small thing, 00 is legal and is 0, but 08 is illegal, and 010 is = 8


consider: (defn is-between [[lower upper] n] (<= lower n upper)) - then the upper_bound and lower_bound functions don't need to exist


and foo_bar is not idiomatic clojure naming, we'd use foo-bar normally


(let [n 17 r {[0 10] 1 [11 20] 2 [21 30] 3}] (some (fn [[[l h] v]] (when (< l n h) v)) r))


So is_between can be simplified by using (< lower something higher)


And destructuring replaces the helper functions


(def mapping
  {[00, 10] 0
   [11, 20] 1
   [21, 30] 2})

(defn is-between [[[lower upper] val], n]
  (when (<= lower n upper) val))

(defn get-value [n]
    #(is-between % n) 


thanks for the feedback 😄 wasnt expecting all of that


😄 - another small thing (more of a style point) - when is often used for side effects, as it's like if but allows multiple forms


you can use and to indicate a similar meaning


(in this and similar use cases), but and emphasizes a chaining of "truthy" state (and (<= lower n upper) val) - val, if condition is truthy


I see, I will keep that in mind


(just a word of caution there: (when cond val) yields nil when cond is falsey but (and cond val) returns falsey so they may have different values)


yeah - it's being used in a context where nil/false do the same thing, but that's a good point