Fork me on GitHub
#beginners
<
2018-08-01
>
andy.fingerhut00:08:48

@hiredman Any examples of impure functions that you have used memoize on before? If the memoized thing is doing read-only behavior from a database that is never or slowly changing, I suppose memoize could be used there to improve performance and give the correct results (perhaps even past the time it gives up to date correct results, if you aren't careful).

hiredman01:08:02

I don't use memoize all that much, I think the last place I used it maybe spun up a thread to do something and return a core async promise-chan, but I don't entirely recall. What came to mind immediately was lein used to use memoize internally on tasks to stop them running multiple times

sb07:08:06

Hello, I would like to create lein-plugin for myself for esier deploy cycle. I try to use clojure/tools.cli but when I tested.. with “-h” flag not works the program.. if I use “-e” everything ok.

(def cli-options
  "all optional and positional"
  [["-e" "--helps" "Help me"
    :default false]
   ["-u" "--sudo" "Configure passwordless sudo"
    :default false]])

sb07:08:43

sb$ cd ../devops && sudo lein install && cd ../test-project/ && sudo lein devops -h
Created /Users/sb/Desktop/devops/target/devops-0.1.0.jar
Wrote /Users/sb/Desktop/devops/pom.xml
Installed jar and pom into local repo.
Main function called from Leiningen from original docs

Arguments: ([& args])
sb$ cd ../devops && sudo lein install && cd ../test-project/ && sudo lein devops -e
Created /Users/sb/Desktop/devops/target/devops-0.1.0.jar
Wrote /Users/sb/Desktop/devops/pom.xml
Installed jar and pom into local repo.
Lein-devops Leiningen plugin helper

   Usage: ${0} (-h | -S | -u | -k | -s | -d [docker_ver] | -a [docker_ver])

  ENVIRONMENT VARIABLES
  Usage: (you can add in project.clj)
  Example: :devops {:server-ip "123.12.12.232"}

  ..works

sb07:08:06

any idea how to fix it? is that possible or good example how to use -h option with tools.cli?

sb07:08:20

I used with lein install and added to a new pure project.

lilactown14:08:36

@sb I don’t see -h as an option in your code.

sb15:08:14

I modified to -e, before this was -h. I added tests for this code.. “validate-args cli-options” and come back a normal response. So, I don’t know what is really the problem..

sb15:08:07

Other characters.. works fine.. just problem with the “h” character.. when run the plugin (lein devops -h).

zlrth15:08:39

I’m trying to make something remove based on two criteria:

(remove (and (comp #{"one"} :id) (comp #{"two"} :other-id)) '({:id "one" :other-id "two"}))
=>()
(remove (and (comp #{"one"} :id) (comp #{"two"} :other-id)) '({:id "not-one" :other-id "two"}))
=>() ;; should return '({:id "not-one" :other-id "two"})
i’m not surprised that this doesn’t work; i think it’s only comparing :id because and is evaling and returning the first comp. (i only picked and because it semantically fits what i want; do this and do this)

mg15:08:36

Your predicate isn't a function

☝️ 1
zlrth15:08:09

point taken of course. i have not figured out how to write what i want. i want an expression that accepts a list of maps and removes them if they match two kv-pairs. but to be pedantic, it is a function! (and thing thing) returns thing, which is a function.

user> (and (comp #{"one"} :id) (comp #{"two"} :other-id)) 
#function[clojure.core/comp/fn--5360]

zlrth15:08:57

this does it, but is verbose:

(remove (fn [{:keys [id other-id] :as m}] (and (contains? #{"one"} id) (contains? #{"two"} other-id)) ) '(  {:id "not-one" :other-id "two"}))

👍 1
jaihindhreddy10:08:03

(remove #(= {:id "one" :other-id "two"} (select-keys % [:id :other-id])) '({:id "not-one" :other-id "two"}))
Does this satisfy your requirements?

zlrth17:08:24

that does in that case! thanks. a requirement i didn’t say but that i have, is that i actually need it to support a set of values (hence my (contains #{“one”}), which easily supports more values. thanks though, i forgot about select-keys

👏 1
dpsutton15:08:37

You can remove the contains call and just invoke the set

👍 1
zlrth15:08:56

i was looking in the docs for something like juxt. I want to be able to say

(foo (comp #{"one"} :id) (comp #{"two"} :other-id))
where foo means “give as input the same to each”

lilactown15:08:53

yeah, that would be useful. you could use something like:

(defn juxt? [f g]
  (let [ps (juxt f g)]
    #(apply and (ps %))))

zlrth15:08:32

that’s pretty cool! i get “can’t take value of a macro” : (. but i see your point!

dpsutton15:08:33

cljs.user> (every? identity ((juxt (comp #{"one"} :id) (comp #{"two"}  :other-id)) {:id "one" :other-id "two00"}))
false
cljs.user> (every? identity ((juxt (comp #{"one"} :id) (comp #{"two"}  :other-id)) {:id "one" :other-id "two"}))
true

dpsutton15:08:45

i'm not sure code golf helps here though

zlrth16:08:07

every? identity juxt is good. and yeah agreed with your comment. thanks!

sundarj18:08:31

@U0HJ4RAGY i think you want every-pred

=> (remove (every-pred (comp #{"one"} :id) (comp #{"two"} :other-id)) [{:id "not-one" :other-id "two"} {:id "one" :other-id "two"}])
({:id "not-one", :other-id "two"})

zlrth19:08:10

’s it! thank you very much

sundarj19:08:52

no problem!

lilactown19:08:42

nice! I think I've discovered every-pred once before and promptly forgot it :d

sundarj19:08:19

hahah, fair enough

sundarj19:08:07

there's also some-fn, which is like the 'or' version

Chase16:08:01

so if i run an assert function in the repl and it does not give me an exception, does that mean it passed. it is returning nil which is why i wanted to clarify. nil means it's all good in this instance?

hiredman16:08:02

what do you mean by an assert function?

Chase16:08:10

cool. i probably should have re-read the docs and asked here before I refactored my function 3 times. lol

Chase16:08:35

(assert (= "Hello, World!" (greeting))) kind of thing

Mario C.20:08:51

Something mysterious is happening and I can't quite understand why.

Mario C.20:08:23

I have two projects Project A and Project B that both use another project called Project C

Mario C.20:08:17

Project C uses a utility (:import org.apache.commons.codec.digest.DigestUtils)

Mario C.20:08:07

In Project C there is a function that calls a method* (DigestUtils/sha1Hex "Mario")

Mario C.20:08:09

If Project A calls that function, everything succeeds and I get the expected result: 8e7d820ad9d3323edb541a72da4912148cb9c0c2

Mario C.20:08:31

If Project B calls that function, it hangs and does not return anything.

Mario C.21:08:43

Project C is defined as (defproject project-c "0.1.1-SNAPSHOT")

noisesmith21:08:19

I bet lein deps :tree in Project A and Project B would show the difference

Mario C.21:08:02

How would I use that? I am not familiar with lein deps :tree

Mario C.21:08:30

Running those command for both Project A and Project B give me different results

Mario C.21:08:20

If I add a println in Project C. Both Project A and Project B reflect that.

Mario C.21:08:14

@noisesmith I think I caught it with lein deps :tree

noisesmith21:08:48

NP - I could have been more verbose and said the difference in behavior must reflect different dependency versions etc. but glad you saw the issue

Mario C.21:08:17

[project "0.1.1-SNAPSHOT" :exclusions [commons-codec]]

Mario C.21:08:20

What does that mean? ^

noisesmith21:08:03

it means that you won't use commons-codec, even if something you depend on depends on it

noisesmith21:08:06

usually exclusions are useful on a specific dep (saying "I don't want the version of this that foo/bar asks for") - an exclusion at the top of your project says "I won't provide this dep at runtime even if something says it needs it"

Mario C.21:08:06

Okay so in my Project C I am using a certain version of commons-codec that I want to use. In Project B I have some dependencies that seem to depend on an older version of commons-codec. How can I resolve this issue?

noisesmith21:08:44

usually listing C before the conflicting dep will sort that out, otherwise you can add an exclusion to the specific dep asking for something different, and/or ask for the specific version that B wants

noisesmith21:08:29

lein help sample has examples of using exclusions