Fork me on GitHub
#beginners
<
2018-10-30
>
lilactown04:10:26

is there something like use but scoped for a body thatโ€™s passed into it?

lilactown04:10:39

e.g.:

;; without macro
(some.namespace/foo)
(some.namespace/bar)

;; with macro
(open 'some.namespace
  (foo)
  (bar))

andy.fingerhut04:10:38

I don't know of anything like that off hand, but you can do require and specify an alias name with :as as short as one character

andy.fingerhut04:10:00

You could also use a let to create names inside its body, e.g. (let [foo some.namespace/foo bar some.namespace/bar] (foo) (bar))

andy.fingerhut04:10:54

I guess it might not be terribly difficult to write a macro that generated let statements like that for you, auto-generating the pairs like foo some.namespace/foo for all public Vars inside of the namespace some.namespace

๐Ÿ‘ 8
Ian Fernandez11:10:19

I'm making some requests on twilio api

Ian Fernandez11:10:43

I'm thinking about that is a problem to not use future / async with that stuff

Ian Fernandez11:10:13

In pratical way how I differentiate the needs for doing async channels or use future on mine requests?

schmee13:10:07

@d.ian.b I usually stay away from async stuff until I have performance or latency issues with the synchronous version

Ian Fernandez14:10:56

I did it, it was a sintax mistake on the another call in http kit, sorry

Ian Fernandez14:10:14

Http kit make the future/deref part of processing

Audrius14:10:01

how to check if number is divisible by 5? :thinking_face:

dpsutton14:10:35

@audrius the usual idea is to modulus by 5 and then look at that value. Are you familiar with modulus and do you know how to look up whether clojure has a version of it?

audrius14:10:11

Different audrius ๐Ÿ˜„

๐Ÿ˜ 4
Joseph Hance14:10:01

@UCQ2HRAA0 one way would be this: (defn div-by5? [x] (= 0 (mod x 5)))

henrik18:10:55

Or even,

(defn div-by5? [x] (zero? (mod x 5)))

Joseph Hance20:10:31

@U06B8J0AJ thanks, learning something new every day!

๐Ÿ‘ 4
manas_marthi14:10:06

I added nrepl to project.clj and ~/.lein/profiles.clj (under :plugins and under {:user {:plugins.. However, when I run lein repl, lein is still using nrepl 0.2.12

manas_marthi14:10:20

how do I get lein to use new nrepl

dpsutton14:10:28

i think there is a plugin to do so

dpsutton14:10:13

i believe the PR to update lein to new nrepl has been accepted there just hasn't been a release ๐Ÿ˜ž

dpsutton14:10:34

oh @manas.marthi run lein nrepl not lein repl (note the Nrepl not repl)

manas_marthi14:10:04

Hi @dpsutton yeah tried it now...

manas_marthi14:10:12

but repl prompt is not showing up

manas_marthi14:10:23

i thought it was slow

manas_marthi14:10:27

but it never showed

dpsutton14:10:38

from the command line you are running lein nrepl?

manas_marthi14:10:18

"nREPL server started on port 65135 on host localhost - <nrepl://localhost:65135>"

manas_marthi14:10:27

but the prompt did not come up

manas_marthi14:10:57

I added the nerepl lien plugin to project file and lein profile as well

manas_marthi14:10:06

as i was not sure how it will pick

dpsutton14:10:50

can you paste the contents of your lein profile?

manas_marthi14:10:02

{:user
  {
   :plugins [
      [lein-ancient "0.6.10"]
      [venantius/ultra "0.5.1"]
      [lein-count "1.0.2"]
      [walmartlabs/vizdeps "0.1.6"]
      [luminus/lein-template "2.9.10.74"]
	  [nrepl "0.4.5"]
      [cider/cider-nrepl "0.18.0"] ;;0.12
	  [refactor-nrepl    "2.4.0"]  ;;2.2
	  [nrepl/lein-nrepl "0.2.0"]
    ]
   }
   :dependencies [
     [cider/cider-nrepl "0.18.0"] ;;0.12
   ]
   :plugins [
	  [nrepl "0.4.5"]
      [cider/cider-nrepl "0.18.0"] ;;0.12
	  [refactor-nrepl    "2.4.0"]  ;;2.2
    ]

manas_marthi14:10:08

oh did not format

manas_marthi14:10:08

added the deps under user as well as global.. random guesses here..

manas_marthi15:10:13

tried

lein nrepl :blocked false
and added nrepl,nrepl-lein, cider-nrepl, refactor-nrepl all to project also.. (not knowing how it works)

manas_marthi15:10:26

I am using jdk10 and clojure 1.10.0-beta4

dpsutton15:10:42

bleeding edge there ๐Ÿ™‚

manas_marthi15:10:08

ok.. let me try clojure 1.8.0

dpsutton15:10:23

i think 9 and 10 are best ignored.

dpsutton15:10:33

8 and 11 are the long terms. not sure if clojure is ready for 11 yet

manas_marthi15:10:49

tried java 8 and clojure 1.8.. no luck ๐Ÿ˜ฅ

manas_marthi14:10:43

looks like it is running in headless mode

TK14:10:59

There are any way to set a precision/round for a float value? For example, I have 34.922. But I want 34.92 I can do

(read-string
  (format "%.2f" 34.922))
But I don't know if is it a best practice to transform into a string and then into a float again

dpsutton15:10:21

at what point do you want that precision? many times just in the UI for display but you want to keep the full precision throughout the calculation.

TK15:10:41

in the UI

dpsutton15:10:02

then you just need the string. UI's are normally all strings right?

๐Ÿ‘ 4
dpsutton15:10:08

why not just do the format

TK15:10:05

@dpsutton In terms of test, is it ok to expect a string instead of a float value?

dpsutton15:10:20

what are you testing?

dpsutton15:10:58

the correct ui output? then the correct output is a string "3.14". If testing calculations it would seem that you want the "real" value

TK15:10:06

Something like this

(defn my-f
  [float-value]
  (format "%.2f" float-value))

(is (= "34.93" (my-f 34.932)))

TK15:10:39

or

(defn my-f
  [float-value]
  (read-string
    (format "%.2f" float-value)))

(is (= 34.93 (my-f 34.932)))

dpsutton15:10:48

then yeah, test your ui formatting function. maybe also include "34.1" "3" etc

dpsutton15:10:59

I think read-string is very misguided

dpsutton15:10:01

also, strings behave differently from floats in important aspect for you: 3 == 3.00 but "3" != "3.00"

TK15:10:08

But if I want to use the return value not just for UI, but also as a parameter for another function (to use in some calculation)

TK15:10:42

using string is not flexible

dpsutton15:10:54

right. so only the UI does the string conversion

andy.fingerhut15:10:24

So if you were not aware, floating point numbers do not give exact results from arithmetic operations, except in unusual circumstances. Depending upon the kinds of tests you want to write, this can make it tricky to have repeatable tests across code changes.

andy.fingerhut15:10:57

i.e. you may have test failures because a result came out 3.999999998 instead of 4.0

andy.fingerhut15:10:35

unless you write tests that expect an appropriate amount of difference between expected and actual results, where "expected amount" may very well depend on what you are doing.

TK18:10:53

If I have 3 different src files for my simple app: core, persistence, and calculation. The core.clj file require the other 2.

(ns rachar-conta.core
  (:require [rachar-conta.calculation :refer :all])
  (:require [rachar-conta.persistence :refer :all]))
If I want to do an "integration" test, do I need to require the persistence and calculation namespaces on my core_test.clj? Something like this..
(ns rachar-conta.core-test
  (:require [clojure.test :refer :all]
            [rachar-conta.calculation :refer :all]
            [rachar-conta.persistence :refer :all]
            [rachar-conta.core :refer :all]))
Or can I just require the core namespace and it will require all other namespaces?

enforser18:10:05

If you require core then you will only be able to call functions from core - you won't be able to call the other namespaces directly.

enforser19:10:14

:refer :all does not mean that the functions from other namespaces also exist as functions of the core namespace - it just means that from the core namespace you can refer to them without a prefix.

dpsutton19:10:20

use of :refer is discouraged in modern clj and :refer :all is pretty much an antipattern. just a code style note for as you learn

TK19:10:03

How can I refactor to not use the refer?

dpsutton19:10:36

(ns schema
  (:require [clojure.edn :as edn]
            [ :as io]
            [clojure.string :as str]))

dpsutton19:10:59

Then anything from the clojure.string namespace is used as str/blank?

neural19:10:56

@dpsutton do you have some link for good practice in modern clj?

๐Ÿ‘† 4
neural19:10:44

i mean i use :refer :all all the time!!๐Ÿ˜ฎ

noisesmith19:10:43

I'd make an exception for a test ns - it should be OK to :refer :all from the specific namespace being tested

noisesmith19:10:07

otherwise use :as, and then use the prefix defined by as

dpsutton19:10:12

i prefer not to for autocompletion reasons but that's a valid place where they make sense

noisesmith19:10:19

(as shown in @dpsuttonโ€™s example above)

dpsutton19:10:02

@neural.works.com go read some code of yours that uses refer all and try to reason about the code and where functions are defined. It makes a world of difference in readability.

dpsutton19:10:56

@neural.works.com there's the clojure style guide but i'm not too sure how authoritative that is but its probably worth a glance. I think it includes arguments for its conventions so its at least good to show why even if you remain unconvinced. But in general reading code on github or without an editor hooked up should convince you of the reason to not use :refer :all

neural19:10:50

@dpsutton i see now. tks!

seancorfield19:10:08

I used to be in the "`:refer :all` is okay in test namespaces" camp but I've changed my opinion after several years and a lot of (test) code.

seancorfield19:10:55

I will sometimes :refer specific symbols that would otherwise be a bit harder to read with an alias, but mostly I try to stick to :as.

jaawerth19:10:13

they shoulda called it :clobber ๐Ÿ˜„

jaawerth19:10:37

:clobber [:ALL :THE :THINGS]

๐Ÿ˜† 4
dpsutton19:10:41

one other thing is shadowing and the dreaded unshadowing. Ie, namespace foo/bar defines thing. You import it using :refer :all so rather than foo.bar/thing you just have thing in your namespace.

noisesmith19:10:28

it's not just about clobbering though (there's :rename if that's the main concern) - for me it's about knowing where some definition came from

dpsutton19:10:53

so if you define a thing in your current namespace you have a collision and you can't refer to something. Also, if you name a parameter (defn function [thing] (stuff thing)) and then rename it to (defn function [different-name] (stuff thing)` you won't get an error because thing is a function not undefined now

dpsutton19:10:36

this just bit me because someone destructured something as uuid and a refactor removed that binding so i was calling transaction function with the function uuid rather than the now-non-existant identifier of an object

jaawerth19:10:47

also protocol methods that hook into existing or common-named functions, particularly when you're extending java builtins

jaawerth19:10:40

at any rate, other languages have taught me to be explicit

Audrius22:10:50

how to join vector to string with a symbol in betweean of every of all?

manutter5122:10:55

> (clojure.string/join ", " [1 2 3 4])
"1, 2, 3, 4"
Like this ^^?

๐Ÿ‘ 4