This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-17
Channels
- # beginners (69)
- # calva (25)
- # cider (12)
- # cljdoc (17)
- # cljs-dev (60)
- # clojure (67)
- # clojure-spec (2)
- # clojure-uk (17)
- # clojurescript (46)
- # cloverage (1)
- # code-reviews (6)
- # cursive (3)
- # datascript (8)
- # figwheel-main (1)
- # fulcro (13)
- # hyperfiddle (25)
- # off-topic (7)
- # re-frame (24)
- # reagent (12)
- # reitit (24)
- # ring (8)
- # spacemacs (11)
- # specter (22)
- # tools-deps (7)
- # unrepl (1)
I need to build a function that takes a string and a number, compares the amount of characters of both and, if the string is shorter than the number, attach more of the same string to the end of the first one until it gets the size of the number. So if I enter the string test
and the number 8
it would return testtest
. So far the way I tried to do it is to loop, checking if the string is equal or greater in number of characters to the number, and then applying something like (subs string 0 number)
to the result, but things aren't working as I expected, and it looks really ugly, I'm for more than an hour trying to spot what's going wrong. Any ideas on how to accomplish this in a different way?
Ok, got it working:
(defn make-secret [text length]
(loop [secret text]
(if (< (count secret) length)
(recur (str secret secret))
(subs secret 0 length))))
But would still like feedback if I could improve something. Thanks.@anantpaatra although strings aren't sequences themselves (`(seq? "test")`)
they are seqable (`(seqable? "test")`) which means you can call seq
to get a sequence of characters of the string.
And this means that you can use all of Clojure's sequence functions on strings directly.
Here's what I came up with
(defn make-secret [text len]
(->> (cycle text)
(take len)
(apply str)))
Peruse
when you have time. Lots of useful sequence functions in the core library. Many of them are have multiple arities that make sense. partition
for example, can also overlap the partitions. Clojure is elegant 😄
thanks. is there a better way to do this? I've got the html now, but the components in the list are rerendering more often than they should be, and I suspect it's because of how they're evaluated.
okay, I fixed it. turns out the issue was that I was calling note-view
with square brackets rather than round. I switched it to (doall (for [note notes] [note-view note]))
and now it's working.
@UC9FL22JD you aren't calling note-view
here too. How's it working?
afaik, it's something unique to reagent, and is how it figures out what to render as react components
https://github.com/reagent-project/reagent/blob/master/doc/UsingSquareBracketsInsteadOfParens.md
context: I'm adding local state to a re-frame render function, so I want a list of hiccup / html data, but instead I'm getting a list of zero argument functions
(doall (map (partial note-view) notes))
(defn note-view
[note]
(let [local-state (reagent/atom "")]
(fn [] [:div ... ])))
I tend to eval (use 'clojure.repl)
, (use 'clojure.pprint)
and (require '[clojure.spec.alpha :as s])
every time I open my REPL with rebel-readline
. Can I automate this somehow?
Does anyone know how to upgrade the clojure version used by lein repl
when used outside of a project directory?
I tried adding the following to ~/.lein/profiles.clj
but it had no effect:
{:profiles
{:repl {:dependencies [^:displace [org.clojure/clojure "1.10.0-beta5"]]}}}
lein repl
is clearly reading the file just fine, since it gives me an error/warning if i have a typo in the file
@vheuken well you don't need the outer {:profiles }
map in ~/.lein/profiles.clj, you can just do {:repl {... } }
but strangely I just checked and mine doesn't seem to be respecting it either, though it does respect the plugins I have in my user profile
oddly I changed my test to beta4 and it DID download beta4, it just didn't use it to launch the repl
By the time Leiningen reads that file, it's already loaded Clojure so you can't change it.
Oh, interesting. You never used to be able to do that I think...
Weird, it downloads the specified version, but still doesn't use it. Probably a regression from some version where it used to work?
in the meantime there's always the clojure cli tools! what I did was set up a beta
alias for 1.10.0-beta5 so I can launch a beta repl with clj -A:beta
outside of a project
Yup, it works in 2.7.1
seanc@DESKTOP-JMD1DBJ:~/clojure$ lein upgrade 2.7.1
The script at /home/seanc/bin/lein will be upgraded to the latest 2.7.1 version.
Do you want to continue [Y/n]?
Upgrading......
Leiningen 2.7.1 on Java 1.8.0_181 OpenJDK 64-Bit Server VM
seanc@DESKTOP-JMD1DBJ:~/clojure$ lein repl
nREPL server started on port 56061 on host 127.0.0.1 -
REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.10.0-beta6
OpenJDK 64-Bit Server VM 1.8.0_181-8u181-b13-0ubuntu0.16.04.1-b13
...
user=>
(you can use lein upgrade
to go up and down the releases)
We do everything with the CLI tools at work. We started on Leiningen back in 2011, switched to Boot at the end of 2015, and just switched over to clojure
and deps.edn
in the last month or so.
Neat. I've been meaning to give that kind of workflow a try anyway. Always had trouble maintaining a clean lein config...
the only thing I miss with the CLI tools so far is being able to just define tasks as code without having to throw a scripts dir somewhere. well that and lein and boot do provide some plugins/tasks that take away some of the manual tinkering you have to do with a few common setups
granted I've only done light work with boot anyway and I'm still relatively new to everything
Boot's ability to let you define arbitrary tasks in your build.boot
is a double-edged sword. We ended up with 2,000 lines of code in ours over the years 🙂 Breaking things into separate scripts is good practice -- that CLI forces on you.
deps.edn
is pure data -- no way to have env vars in it. What would you want to use env vars for? (so maybe there's a way to achieve it)
mainly just to clean up how local deps are handled. Like, the other day I wrote a tiny script that sets up rebel-readline and also spins up a cider-nrepl server, since there isn't currently a built-in integration, and it'd be nice to not have to hard-code a fixed path into my home directory. Not a huge deal - I should probably throw it in a git repo or something anyway
@seancorfield the main env var of interest are the ones used by clj
itself - $HOME
, $CLJ_CONFIG
, and $XDG_CONFIG_HOME
- for instances where you might want to configure some general tooling that doesn't live in any one project
what might be better is a way to resolve relative paths but that almost seems like a taller order given the JVM of it all
hmm you know it should be pretty easy to write up some kinda dispatcher script for such things, and just use that - but more likely I'll just throw my custom central stuff into a git URL so I don't have to worry about hardcoding a local absolute path for it. it doesn't come up THAT often
We have a monorepo with a lot of subprojects. One subproject is special and provides all the pinned versions (in a :defaults
alias with :override-deps
for everything).
So then we do CLJ_CONFIG=../versions clojure -A:defaults:other:aliases
in each one as needed.
one decision I love about clojure CLI tools is the fact that you can define one or more clojure version right from there - putting the version manager in the official tooling. almost every other language I can think of requires a userland solution for version management
And we have a simple build
shell script to automate that part, so build alias:list subproject
does a cd
to that subproject and then runs the clojure
command.
We have a build
subproject which has various short scripts with -main
functions that can be invoked via aliases. We also have a dev
subproject with REPL-starting code that can be pulled in as a :local/root
dependency, as I recall.
@jesse.wertheim Like this
:nrepl ; modern nREPL server
{:extra-deps {ws/dev {:local/root "../dev"}
nrepl {:mvn/version "RELEASE"}}
:main-opts ["-m" "ws.dev.nrepl"]}
(and you can specify a port for the REPL as a command-line argument)
@veix.q5 defrecord
takes a fixed list of fields that the record will contain.
@seancorfield thanks this is giving my some ideas for a good local setup. I'm also looking to do something for the kinds of tooling you may want as an individual without polluting a project
@seancorfield I was under the mistaken impression that it doesn't like relative paths, but it's because I was trying it as a script without making it into a full dep folder with a src
dir and its own deps.edn
- and running it via main-opts, where of course the relative path wouldn't work
Yeah, we have to have all the subprojects at the same "level" (inside a clojure
top-level folder in our repo) so that the relative paths all work -- because tools.deps
doesn't currently deal with transitive relative paths very well (they all end up relative to where you start the script, instead of being relative to their respective deps.edn
files).
yep - tested that out a minute ago by setting the relative path with regard to my home folder and then launching the alias from there
Hi all, I do a lot of 3D image processing in python and I was wondering what tools are out there in the Clojure ecosystem. One nice thing about python is that so many libraries agree on using numpy ndarrays, but idk if there's such a standard in Clojure (core.matrix?). I eventually want to shift to Clojure to make parallel image processing easier, maybe even distributed with something like Onyx.