Fork me on GitHub
#beginners
<
2022-04-27
>
Michael Agres01:04:47

Back to "Brave and True"... 2. write a function, append, which will append a new suspect to your list of suspects I can get this to work stand-alone: (conj (parse (slurp filename)) ["John Smith" "0"]) =>(["John Smith" "0"] ["Edward Cullen" "10"] ["Bella Swan" "0"] ["Charlie Swan" "0"] ["Jacob Black" "3"] ["Carlisle Cullen" "6"] ["John Cena" "0"]) But when I try to wrap it into a function: (defn append [name glitter-index] (conj (parse (slurp filename)) [name glitter-index])) I get... ; Execution error (ArityException) at fwpd.core/eval7950 (form-init9865369693235614087.clj:70). ; Wrong number of args (1) passed to: fwpd.core/append How do I pass a record (Name and glitter-index) as a single argument?

Cora (she/her)02:04:58

the error message says you're passing one argument but the function is written to take two arguments. can you just do (append name glitter-index) instead? that will work with the current code

Cora (she/her)02:04:37

otherwise you'll need the function to take a single argument, like (defn append [name-and-glitter-index] (conj ... name-and-glitter-index)) and call it like (append [name glitter-index])

Michael Agres23:04:41

@U02N27RK69K (append "new name" "0") worked. I originally ran it as (append ["new name" "0"]). Why isn't the latter a valid call?

Cora (she/her)01:04:04

because it's passing one argument, a vector, instead of two

xbrln12:04:33

Am using https://github.com/ptaoussanis/carmine to interact with Redis. Each time I start a REPL or compile I get the following warnings. Is there a way to not show these warnings ?

WARNING: abs already refers to: #'clojure.core/abs in namespace: taoensso.encore, being replaced by: #'taoensso.encore/abs
WARNING: update-keys already refers to: #'clojure.core/update-keys in namespace: io.aviso.exception, being replaced by: #'io.aviso.exception/update-keys
WARNING: parse-long already refers to: #'clojure.core/parse-long in namespace: taoensso.carmine, being replaced by: #'taoensso.carmine/parse-long
WARNING: parse-double already refers to: #'clojure.core/parse-double in namespace: taoensso.carmine, being replaced by: #'taoensso.carmine/parse-double

didibus16:04:27

You need to exclude them explicitly.

(ns your-namespace
  (:refer-clojure :exclude [abs update-keys])
  ...)

didibus16:04:13

But, that would need to be done inside taoensso, so otherwise if you don't have access to that code since it's a library O don't think you can hide the warming

didibus16:04:26

Just ignore them

didibus16:04:15

Well, technically I think you could do something, but it's a bit complicated and I wouldn't recommend it, best to just live with the warnings and ignore them.

xbrln16:04:08

Thats what I thought too, since its a library excluding does not work.

xbrln16:04:34

thank you anyways πŸ™‚

janezj13:04:59

I would like to have a base library providing components to several other applications. components (mount) in the base: β€’ config β€’ postgress connection β€’ db2 connection β€’ ibm mq connection (with jms listener) β€’ pathom β€’ immutable web server β€’ rabbit mq streams β€’ fucro β€’ soap β€’ ..... So the goal is that my apps would use just a couple of deps beside the base. And there comes a problem, I just don't want that app using rabbit-mq-stream and postgress will include all the jars from the base (all jars required by fulco, pathom ...). The base should be tested well enough that I am sure that all components can be used in one project. Is it possible to solve it with aliases in base/deps.edn and referring required alias in app/deps.edn?

delaguardo13:04:55

this is not the nicest road but you can get some inspiration from this set of articles - https://corfield.org/blog/2021/02/23/deps-edn-monorepo/

Drew Verlee14:04:14

I'm not sure what the input is to your problem. Is a user going to run a command program with flags like +fulcro?

Drew Verlee14:04:03

If they are building the deps file manually, then i assume a base deps file added to others would achieve this as well.

janezj14:04:16

One of my programs will listen on rabbitmq stream and put some data into postgress and ibmmq. Another app will listen to rabbit and visualize events on frontend fulcro UI. Another app will listen on ibmmq and forward events to soap endpoints. So many apps, and I could end with copy pasting boilerplate code from one to another. So I want DRY and small docker images

dgb2315:04:31

Did you have a look at #polylith? It helps you organize your repository to span over multiple projects, components and a decoupled development project (that you set up to be repl friendly) dependencies are defined per use-case.

dgb2315:04:56

Or you can use some of the ideas there if you don’t agree with all of it maybe.

janezj16:04:22

Thanks, I am going to study polylith and monorepo and do some experiments. I was hoping for some some does.edn magic, so that my base project would be usable outside my organization.

Ben Sless18:04:04

This is a rough sketch of something I figured out the other day https://gist.github.com/bsless/839bbf7b7ab73adfba6703e79c079cff Aligning dependencies between modules isn't solved yet with this, but I have a few idea

Drew Verlee19:04:00

@UK0810AQ2 the link is giving me a 404.

janezj20:04:27

The final deps.edn is built from 3 deps.edn files: system, user, project. If there would be a possibility to inject another one, all problems could be solved with aliases?

Ben Sless03:04:39

@U0DJ4T5U1 fixed, must have missed a character

orpheus17:04:47

When I use clj in the same dir as a deps.edn file, how can I have it run a repl while also loading some namespace? So I can have some src/main.clj with a bunch of helper functions ready to go. using clj -i src/main.clj errors:

WARNING: Implicit use of clojure.main with options is deprecated, use -M
And I'm not sure exactly how to use -M

dpsutton17:04:41

/tmp/foo via :coffee: v17.30
❯ cat deps.edn
{:paths ["."]}

/tmp/foo via :coffee: v17.30
❯ cat foo.clj
(ns foo)
(def x 3)

❯ clj -M -e "(doto 'foo require in-ns)" -r
foo
foo=> x
3
so you just -e to evaluate a form to require and set the namespace and -r to get a repl. I think this is a bit much and i just prefer to use the normal interactions of a repl to require whatever namespace i need

delaguardo17:04:44

there is a shady trick - you can use not well documented feature of clojure repl add somewhere in your path file user.clj and it will be loaded automatically whenever you start new repl

/tmp/x via β˜• v11.0.11 took 8s 
❯ tree .       
.
β”œβ”€β”€ deps.edn
└── src
    └── user.clj

1 directory, 2 files

/tmp/x via β˜• v11.0.11 
❯ cat deps.edn 
{:paths ["src"]}

/tmp/x via β˜• v11.0.11 
❯ cat src/user.clj 
(defn foo [] "!")

/tmp/x via β˜• v11.0.11 
❯ clj
Clojure 1.11.1
user=> (foo)
"!"

delaguardo17:04:49

I usually have a dev directory with user.clj and everything else that I need during development. then in deps.edn I add an alias :dev {:extra-paths ["dev"]}

πŸ™Œ 1
delaguardo17:04:17

and use this alias to spin up the repl

orpheus17:04:02

@U11BV7MTK That worked nicely thank you!

orpheus18:04:04

@U04V4KLKC What am I doing wrong here?

user@architect .cljhome % tree
.
β”œβ”€β”€ README.md
β”œβ”€β”€ deps.edn
└── src
    └── main.clj

1 directory, 3 files

> user@architect .cljhome % cat deps.edn 
{:deps
 {clj-http/clj-http {:mvn/version "3.12.3"}
  org.clojure/core.async {:mvn/version "1.5.648"}}
 :paths ["src"]}

> user@architect .cljhome % cat src/main.clj 
(ns main
  (:require [clojure.core.async :as async]
            [clj-http.client :as client]))

(defn health-check [] 
  (:body (client/get "")))

> user@architect .cljhome % clj
Clojure 1.11.1
user=> (health-check)
Syntax error compiling at (REPL:1:1).
Unable to resolve symbol: health-check in this context
user=> 

delaguardo18:04:39

Filename must be user.clj

orpheus18:04:26

Ahh gotchya. I see that said here now: https://practical.li/clojure/clojure-cli/projects/configure-repl-startup.html surprised it's not in the deps and cli docs: https://clojure.org/guides/deps_and_cli

πŸ‘ 1
Fredrik Holmqvist20:04:34

How do you comp a seq functions?

(def search-function 
  (->> search-settings
       (select-keys search-functions)
       (vals)
       (comp)))
vals returns a clojure.lang.APersistentMap$ValSeq, passing it to comp doesn't change it.

dpsutton20:04:41

you want (apply comp) i believe

Fredrik Holmqvist20:04:13

Cheers, that seem to have evaluated them. However the arity of those functions is 2, I'm getting an exception of wrong arity passed (1).

(search-function people "eva")
Guessing that (apply comp) needs some more help?

dgb2320:04:45

What do you want to achieve?

Fredrik Holmqvist20:04:15

I want one function, composed of several functions that I got out from a map of keyword, function .

dgb2320:04:20

you could map over the functions first

dgb2320:04:31

before (apply comp)

dgb2320:04:28

with (partial people)

dgb2320:04:35

but then you need to provide that value

Fredrik Holmqvist20:04:50

You mean partially apply them? If so, I need to keep the arity 2 as I want to reuse this function for different inputs. Eg:

(search-function people1 "eva")
(search-function people2 "steve")

Fredrik Holmqvist20:04:11

Ah yes, sorry, can't πŸ˜•

dgb2320:04:33

just throwing out ideas - how about wrapping them first in an FN that takes 1 argument and returns a function with the second argument while the first ist bound

dgb2320:04:50

seems convoluted

dgb2320:04:48

(fn [people] (partial search-fn people))

Fredrik Holmqvist20:04:11

Sort of like a lazy partial? Yeah that probably works. Not great, but it works.

Fredrik Holmqvist20:04:32

I've used Ramdajs in the past, rewriting some stuff in Clojure.

Fredrik Holmqvist20:04:58

There it's:

const searchFunction = R.compose(...searchFunctions)

Fredrik Holmqvist20:04:43

Does Clojure automatically support currying? Maybe that's the reason.

Fredrik Holmqvist20:04:13

Ah, that explains the problem and your suggested solution!

Fredrik Holmqvist20:04:19

Thanks @U01EFUL1A8M πŸ™‚

πŸ‘ 1
dgb2321:04:25

Doesn’t really work right?

Jose Varela23:04:55

If you want great tutorials about everything Clojure (testing, React/Reagent, tooling, Datomic, etc), checkout https://lambdaisland.com/episodes which just released their paywalled videos to the public.