Fork me on GitHub
#beginners
<
2019-01-16
>
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';

victorb11:01:35

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

ordnungswidrig11:01:47

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

victorb11:01:19

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

ordnungswidrig11:01:35

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

victorb12:01:11

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})
only
(funct args :a 1 :b 3)

dpsutton14:01:05

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

dpsutton14:01:07

fair enough.

dpsutton14:01:21

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

enforser14:01:41

(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})
?

enforser14:01:02

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
enforser14:01:37

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

Ian Fernandez14:01:03

I'm learning map destructuring

dpsutton14:01:16

note it is & {:as ks}

enforser14:01:55

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...

i.e. 
{:keys [a b c] :as m}

dpsutton14:01:57

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})
?

dpsutton14:01:04

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

dpsutton14:01:05

& {: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?

enforser14:01:53

There are a few, but I have this one favourited https://gist.github.com/john2x/e1dca953548bfdfb9844

Ian Fernandez14:01:49

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

czem22:01:18

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]
  (some 
    #(when (is_between % n) (val %)) 
    (mapping)))
Is there a better way?

noisesmith22:01:42

why is mapping a function?

noisesmith22:01:05

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

noisesmith22:01:34

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

noisesmith22:01:00

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

ordnungswidrig22:01:57

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

ordnungswidrig22:01:37

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

ordnungswidrig22:01:57

And destructuring replaces the helper functions

czem22:01:40

(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]
  (some 
    #(is-between % n) 
    mapping))

czem22:01:03

thanks for the feedback 😄 wasnt expecting all of that

noisesmith22:01:44

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

noisesmith22:01:09

you can use and to indicate a similar meaning

noisesmith22:01:24

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

czem22:01:04

I see, I will keep that in mind

seancorfield23:01:05

(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)

noisesmith23:01:05

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