This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-29
Channels
- # aws (1)
- # aws-lambda (2)
- # bangalore-clj (3)
- # beginners (26)
- # boot (25)
- # braveandtrue (1)
- # cider (5)
- # cljsrn (7)
- # clojure (144)
- # clojure-android (2)
- # clojure-czech (1)
- # clojure-greece (3)
- # clojure-italy (17)
- # clojure-poland (5)
- # clojure-russia (25)
- # clojure-spec (9)
- # clojure-uk (100)
- # clojurescript (85)
- # core-async (42)
- # cursive (11)
- # datascript (2)
- # datomic (25)
- # duct (3)
- # emacs (5)
- # figwheel (2)
- # fulcro (49)
- # graphql (16)
- # hoplon (8)
- # klipse (10)
- # leiningen (10)
- # lumo (9)
- # off-topic (12)
- # om (14)
- # onyx (25)
- # planck (34)
- # portkey (6)
- # re-frame (43)
- # reagent (4)
- # remote-jobs (2)
- # ring (36)
- # ring-swagger (1)
- # rum (1)
- # shadow-cljs (187)
- # specter (25)
- # sql (5)
- # unrepl (75)
ins)user=> (run! println (range 10))
0
1
2
3
4
5
6
7
8
9
nil
and that’s what I reach for even when I’m not using core/async
@clojurian The great thing about this channel is that everyone here is either a beginner or someone who has decided to be in this channel to help beginners. That means there are never any dumb questions here 🙂
Hello everybody, I have started learning clojure 2 weeks back. I tried to do few programs to get familiar with syntaxes. So i was trying to print this:
*
**
***
****
*****
******
*******
********
*********
So I wrote a program with two methods which look something like this:
user=> (defn print-row [num]
#_=> (loop [iter 0]
#_=> (if(< iter num)
#_=> (do (print "*")
#_=> (recur (inc iter)))
#_=> (println ""))))
#'user/print-row
user=> (defn print-a-triangle [num]
#_=> (loop [iter 0]
#_=> (if(< iter num)
#_=> (do (print-row iter)
#_=> (recur (inc iter)))
#_=> (print ""))))
#'user/print-a-triangle
I’m also fairly new to clojure, but here’s how I would do it:
user=> (defn print*line[x] (cond (> x 0) (do (print "*") (recur (- x 1))) :else (print "\n")))
#'user/print*line
user=> (print*line 5)
*****
nil
user=> (defn print-pyramid [x] (cond (> x 0) (do (print-pyramid (- x 1)) (print*line x))))
#'user/print-pyramid
user=> (print-pyramid 5)
*
**
***
****
*****
nil
Note that the nil at the end is what the function returns (I think)
Also, this is just copy-pasted from the terminal, no formattingyeah this is a good way of doing.
thanks!
everytime you use recur
you should try to find a solution that doesn’t use it, often it is possible to write it in a more declarative way
No problem. Thanks mate! @U3L6TFEJF
@abiduzair420 better late than never, my over-engineered solution with documentation and prose. feel free to play around with it, refactor it, or comment on it; that's what gists are for https://gist.github.com/gdeer81/6d93e6938ef0ed1a0264d4a34e635d66
Thanks @U30H25RT6 that's great
@U3L6TFEJF can you explain why recur
is not a good thing to use in general?
@sammy.castaneda90 sometimes recur
is necessary, but very often there is a solution available that uses clojures sequence functions (such as take
, take-while
, repeatedly
etc etc) and a function such as map
, filter
or reduce
that will be shorter, more idiomatic, and easier to understand for those used to functional programming 🙂
learning to recognize these situations and building this “inventory” of sequence functions is one of the major challenges every new clojure programmer coming from an OOP or imperative background has to face 🙂
I get a feeling that I'm not writing declarative code. this stuff up here looks more of imperative code. so i would like to ask you how could we re-write the same code in declarative manner ?
regular expressions are not my forte and i'm having trouble using one in clojure. i have a regex pattern that i know works for splitting a string on spaces unless they're wrapped in quotes: [^\s"']+|"([^"]*)"|'([^']*)'
here's a working example: https://regex101.com/r/qqDz70/1
the escaped pattern looks like so: #"[^\\s\"']+|\"([^\"]*)\"|'([^']*)'"
and a working example uses Java's regexMatcher.find() method to build a list of matched groups: https://stackoverflow.com/questions/366202/regex-for-splitting-a-string-using-space-when-not-surrounded-by-single-or-double
how can i translate this to clojure? re-find
is giving me unexpected results:
user=> (re-find #"[^\\s\"']+|\"([^\"]*)\"|'([^']*)'" "one two three")
["one two three" nil nil]
thanks!To expand a bit:
(defn print-a-triangle [num]
(println
(clojure.string/join
"\n"
(map #(clojure.string/join (repeat % "*"))
(range 1 num))))
(formatting is weird I know, I formatted it to fit my window and then realised everyone else probably had different window widths anyway)
This is cool stuff. Thanks guys! @gon @sammy.castaneda90 @carr0t
this gets me a little closer but only returns the first match:
user=> (re-find (re-matcher (re-pattern "[^\\s\"']+|\"([^\"]*)\"|'([^']*)'") "This is a string"))
["This" nil nil]
never mind, figured it out 🙂
user=> (let [matcher (re-matcher (re-pattern "[^\\s\"']+|\"([^\"]*)\"|'([^']*)'") "This is \"a string\" with quotes")]
(map (partial (comp last (partial take-while some?))) (take-while (comp some? first) (repeatedly #(re-find matcher)))))
("This" "is" "a string" "with" "quotes")
@abiduzair420 better late than never, my over-engineered solution with documentation and prose. feel free to play around with it, refactor it, or comment on it; that's what gists are for https://gist.github.com/gdeer81/6d93e6938ef0ed1a0264d4a34e635d66