Fork me on GitHub
#beginners
<
2018-08-05
>
prabhath600:08:58

I just got started with clojurescript and Reagent recently. I am trying to update two atom during the on-click event handler. But only one of the atom is being updated.

(defn set-state [result metric-name]
  #(reset! s/metric-bmi-index "sdfsd")
  (cond
    (= metric-name "imperial") #(reset! s/imperial-result result)
    (= metric-name "metric") #(reset! s/metric-result result)))

(defn button-helper [weight height type f bmi-name]
  (let [bmi-result (f weight height)]
    [:input {:type "button"
             :value type
             :on-click (set-state bmi-result bmi-name)}]))
This #(reset! s/metric-bmi-index "sdfsd") never updated the atom. If i move it to the bottom of the function then it executed but the other one does not. Is there a way to update both atoms? Am i missing something here.

seancorfield01:08:23

@prabhath6 #(...) is a function -- a value -- it doesn't evaluate what's in the parens because you're not calling it.

seancorfield01:08:31

Remove the # and it will work.

prabhath601:08:25

Thanks will give that a try.

stardiviner01:08:23

Which clojure library is great for Redis?

seancorfield01:08:09

@stardiviner We started off with Carmine but switched to Redisson (because we needed Redis Cluster support).

seancorfield01:08:41

@prabhath6 Now I look at your code in more detail, you need a bit of a restructuring there. You are conditionally returning one of two functions, but you actually want to return a single function that does the conditional work, right?

seancorfield01:08:42

You want something like this:

(defn set-state [result metric-name]
  (fn []
    (reset! s/metric-bmi-index "sdfsd")
    (cond
      (= metric-name "imperial") (reset! s/imperial-result result)
      (= metric-name "metric") (reset! s/metric-result result))))

seancorfield01:08:56

When you call (set-state ...) you want to get back a single function (fn [] ...) of no arguments. When that function is invoked, you want it to reset! the BMI, then either reset! the imperial result or the metric result.

prabhath602:08:07

cool, this what i exactly needed. Thanks for the help

runswithd6s01:08:09

So, I've been looking for an excuse to move my blog from Google's http://Blogger.com platform to GitHub or GitLab Pages. I've been a huge fan of GitLab ever since we started using it at work. Given that I also wanted an excuse to do more with Clojure, I chose Cryogen as the Static Content Compiler. Here's what I ended up with: https://runswithd6s.gitlab.io/posts-output/2018-08-03-Migrated-the-Blog/ No customizations yet, but it's a good start!

šŸ‘ 8
Bobbi Towers04:08:04

Thanks! I tried to do that awhile back and gave up because I couldn't get the CI configuration right. Following your guide worked perfectly, and now I feel silly!

Drew Verlee02:08:34

really basic clojure script question. There is (.foo) which I believe gets the property foo's value and calls it like a function. Then (.-foo) which just returns the properties value. What is (-foo) then?

lilactown02:08:54

(-foo) is used in a few different places

lilactown02:08:04

itā€™s often an idiom used when defining protocols

lilactown02:08:49

-foo also pops up when you use the .. macro

lilactown02:08:20

(.. foo -bar -baz) is the same thing as (.- baz (.-bar foo))

lilactown02:08:57

actually, to correct myself, I think that often -fn-name is used for private-ish functions in general, not just protocols

lilactown02:08:30

if that makes sense?

Drew Verlee02:08:01

I'm confused, so it isn't part of js interopt?

lilactown02:08:30

if you see it called like (-foo bar) then I donā€™t believe so

lilactown02:08:38

do you have an example youā€™re seeing it in?

lilactown02:08:54

yep, so youā€™ll see at the top of the file:

(ns ajax.edn
  (:require [ajax.interceptors :refer [map->ResponseFormat]]
            [ajax.protocols :refer [-body]]

lilactown02:08:32

the -body method is a part of the AjaxResponse protocol. itā€™s not interop syntax

Drew Verlee02:08:20

Ah ok. That makes sense, I should have looked further.

lilactown02:08:53

šŸ˜„ itā€™s okay. itā€™s a bit confusing how much syntax Clojure has sometimes, itā€™s hard to know whatā€™s a special built-in form vs. whatā€™s not

šŸ‘ 4
lilactown02:08:46

I recently found out that @ is hard-coded in the clojure compiler šŸ˜µ

b-paul02:08:01

Iā€™m seeing references to :jvm-options in a project.clj Is there an equivalent way to specify JVM options when using the new CLI tools?

andy.fingerhut02:08:16

This article is a decent reference for special characters in Clojure: https://clojure.org/guides/weird_characters The Clojure cheat sheet has them listed in the bottom left, with very brief descriptions of each: https://clojure.org/api/cheatsheet

b-paul02:08:00

I see I can alias them, then include them in my command line with -O, but can I simply have them apply by default?

_rj_r_02:08:02

so I deleted a bunch of files from the .m2 directory, now when I run the command clj I get the error

Error: Could not find or load main class clojure.main
I reinstalled clojure and still does not work. Any ideas?

andy.fingerhut02:08:02

I am not 100% familiar with all the inner working of the clj script, but I think if you delete your /.clojure/.cpcache directory and its contents, as well as /.m2 and its contents, the next run of clj script appears to re-fetch them.

b-paul02:08:56

@ryan.russell011 If that doesnā€™t work, try running with -Sforce. I ran into the same problem

_rj_r_02:08:58

@andy.fingerhut wiping the .m2 and the .clojure directories triggered clj to refetch the dependencies needed.

andy.fingerhut02:08:15

Yes, -Sforce seemed to help in my little bit of testing as well.

_rj_r_02:08:51

@b-paul Thank you for the tip... if I run into issues and wiping the two directories doesn't work, I'll give that a shot as well. :thumbsup::skin-tone-3:

_rj_r_02:08:16

so it kind of seems to me that deps.edn is the way to go... this was a conversation in here a long time ago as well..... wishing there was more beginner friendly documentation for it though šŸ˜•

samarth03:08:53

Iā€™m trying to use the Instapaper API in a Clojure project (https://www.instapaper.com/api/full) and it uses xAuth to get an access token. Iā€™ve tried using the clj-oauth library and it has a function called build-xauth-access-token-request but I have no idea how to use it. Any suggestions on how I solve this?

lilactown04:08:23

@samarthkishor1 I think that is a typo on the instapaper docs

lilactown04:08:35

have you tried normal oauth?

soulflyer09:08:51

Hi, I could do with a clue as to what is going on here. I'm trying to automate the title field of a ring-swagger setup. So far I have been hand editing the title so that my dev version carries a different title to the production version that I have running in the background. This is the start of the swagger services definition in ...routes.services.clj

(defapi service-routes
  {:swagger {:ui "/swagger-ui"
             :spec "/swagger.json"
             :data
             {:info
              {:version "1.0.1"
               ;; Switch to correct title before lein uberjar
               ;; TODO Automate this so swagger page always shows dev or prod version
               ;;:title "Photo API"
               :title (:title env)
               :description "Access a mongo database containing details of photos"}}}}
I have added an extra field, :title to the env setup provided by the luminus scaffolding. It all works fine from the repl in the ..routes.services namespace. I can see my new :title field by doing (:title env) at the repl prompt. However the swagger front page just shows Null for the title. Providing it with a string as per the commented out line above the :title (:title env) shows the string as expected. Am I doing something stupid here?

valerauko09:08:10

that sounds like what i had when i didn't mount/start the env component

soulflyer10:08:34

It's getting started, the env info shows up fine in the repl, and the port number the server gets started on comes from env too.

pez09:08:40

This is awesome! An Animated Introduction to Clojure https://ourcodestories.com/markm208/Playlist/4/

šŸ˜ƒ 4
šŸ˜Ž 4
bartuka12:08:07

Cool! Very helpful

metal 4
Karol WĆ³jcik10:08:10

Hi all! Is there some easy way to take always 3 elements from the channel no less no more?

Karol WĆ³jcik10:08:16

Or to process the channel in the way that 3 elements are grouped to the sequence. Now I'm trying to achieve it with atoms but I'm looking for more idiomatic way šŸ™‚

jaihindhreddy-duplicate10:08:16

(partition-all 3) is the transducer you're looking for.

ā¤ļø 4
jaihindhreddy-duplicate10:08:29

But the last partition may contain fewer items

soulflyer10:08:34

It's getting started, the env info shows up fine in the repl, and the port number the server gets started on comes from env too.

forrest11:08:36

Howdy! Over the past year or two Iā€™ve been learning about functional patterns and loving them in JavaScript land. Recently started reading The Joy of Clojure and gotta say itā€™s up there with some of the most exciting programming books Iā€™ve read, so super excited to be learning my first functional language! Any companion material to JoC you guys would recommend to a Clojure/FP n00b?

valerauko11:08:08

i really enjoyed working through clojure for the brave and true

12
soulflyer11:08:12

Joy of clojure is pretty much jumping in the deep end. I wouldn't recommend it as a first book. I quite liked "Programming Clojure" and "Clojure Programming". Also "Web Development with CLojure"

4
bartuka11:08:41

do you think is worth to learn Common Lisp or other lisp dialects to help with clojure (and FP in general)?

jonahbenton15:08:00

Potentially unpopular opinion- I would say no, don't do that. Everyone can learn what they want to learn, of course, but IMHO the "lispyness" of Clojure is less salient a feature than immutability- which it shares with non-lisp functional languages like Haskell- and its opinions about data- which are subtle and best understood in contrast to languages that depend on types. Common Lisp has a ton of confusing (for beginners) historical cruft. Scheme/Racket are conceptually lovely but depart significantly from Clojure, which is more about pragmatic tradeoffs, an approach it shares with other "industrial" languages. The feature Clojure shares with other lisps- macros- should be rarely used in pragmatic, industrial Clojure.

šŸ‘ 4
kennytilton18:08:56

I am a hard-core Lisper. Just Learn Clojure(tm). It will get you accustomed to the vital Lispy qualities of Clojure: every form returns a value; no static types; interactive; and the parens are in the right place. With CL you will get a Lisp-2, which changes a lot but will slow you down on Clojure.

šŸ‘ 8
bartuka19:08:35

Thanks for the replies. I was imagining that lisp could help me with the structure of the solutions written in clojure which are different from OOP or imperative-style. I wanted to get more exposed to this kind of thinking. However, makes sense to focus on clojure and just get all experience from that.

bartuka19:08:33

@U0FF3A4V6 why macros are not so common in industrial Clojure?

kennytilton19:08:44

I happen to be the God of Common Lisp Macros and I think CLJ being a Lisp-1 (and the keyword fn being so easier to type than lambda) eliminates most of the code simplification need for which I used macros.

jonahbenton19:08:14

Yeah, in Clojure there almost always is a non-macro solution, which will almost always be easier for readers to understand and reason about.

jonahbenton19:08:00

"industrial" -> solve for lowest abstraction load.

bartuka19:08:46

lol. I had just started to make my whish list in Amazon and the book ā€œmaster macros in clojureā€ was on the listā€¦ šŸ˜ƒ

jonahbenton19:08:48

Absolutely! šŸ™‚ Nothing wrong with learning and deepening. But like other artifacts produced in a commercial rather than artistic context, the range of the "language" used in "successful" artifacts- whether it's visual language in film, human language in books or articles, or something made by an industrial programming team- is most often best constrained to be consumable by the widest audience, and to introduce the fewest risks and least edge cases.

šŸ’Æ 4
soulflyer14:08:37

It's worth learning other lisps for their own sake, or just for curiosity, but I don't see any great advantage in learning another lisp to help with clojure. Having said that its always good to know a few languages.....

jaihindhreddy-duplicate14:08:42

@forrest.akin SICP and the companion lecture series are amazing https://www.youtube.com/playlist?list=PLE18841CABEA24090 I started reading the Joy of Clojure as my first Clojure book too, love it so far. I think I didn't find it as intimidating because I watched pretty much every talk about Clojure under the sun šŸ™‚

aw_yeah 4
forrest17:08:33

Yeah, I've watched so many Rich Hickey videos at this point that it doesn't feel foreign or intimidating at all, and I actually find it resonates with me on so many levels hahaha

lilactown14:08:11

common lisp is worth learning if only to experience the debugging and how it compares to clojure šŸ˜‰ #goals

hmaurer16:08:00

Is the debugging better or worse in common lisp? Because I have found Clojureā€™s debugging to be godawful šŸ˜„

tdantas15:08:14

hey guys, one quick and probably dumb question Iā€™m using the cats from funcool and to get used to the library Iā€™m doing some experiments Iā€™ve created the follow dumb sum

(defn sum-3 [a b c]
  (+ a b c))
and Iā€™m trying to apply using fapply
(m/fapply
  (maybe/just sum-3)
   (just 1) (just 2) (just 3))
but not having success with that , do you guys have any experience with cats ! with single arguments ( using inc for instance ) Iā€™m able to use properly
(m/fapply (just inc) (just 2))

lilactown15:08:58

@oliv okay this was confusing, since Iā€™ve never used cats before. but it looks like fapply calls the function like this: (((f 1) 2) 3)

tdantas15:08:29

hey @lilactown, what is f ? my dumb-sum function ?

lilactown15:08:58

so itā€™s calling your sum-3 function like: (((sum-3 1) 2) 3)

lilactown15:08:07

youā€™ll want to curry it

lilactown15:08:19

(m/fapply
 (maybe/just (m/curry sum-3))
 (maybe/just 1) (maybe/just 2) (maybe/just 3))

tdantas15:08:07

was trying to use the regular partial function

tdantas15:08:08

thanks mate !

tdantas15:08:19

forgot the ā€œliftedā€ world

zooes18:08:21

I get the below error on line #3 in the code. Does anyone know what is wrong with the code? I tried out (defn arr [4 3 2 1]) (arr 0) in the REPL, and that seemed to work. IllegalStateException Attempting to call unbound fn

hiredman18:08:56

for is not a loop, and def doesn't deal in local names

zooes18:08:31

Thanks! I keep going back to OOP's way of thinking..

kennytilton18:08:56

I am a hard-core Lisper. Just Learn Clojure(tm). It will get you accustomed to the vital Lispy qualities of Clojure: every form returns a value; no static types; interactive; and the parens are in the right place. With CL you will get a Lisp-2, which changes a lot but will slow you down on Clojure.

šŸ‘ 8
kennytilton18:08:58

Help a Java noob: I have an XHR wrapper library in Clojars. I am using it in a tutorial and need to hack it a bit. I can hack and push to Clojars, but ugh. Or I can symlink in checkouts. Orā€¦ can I hack and lein install? Leading to this question: does a lein installed version completely shadow Clojars? So if I forget the install and just go back to hacking and deploying, I will need toā€¦`lein uninstall`?

pauld18:08:02

If your project name (in project.clj) has a different name / version, there will be no conflict.

kennytilton18:08:39

But if I am just hacking and the name/version stay the same, I have to think the lein install version will forever shadow the Clojars. Maybe I should just get used to bumping the version ā€” but then the clutter on Clojars will be insane.

pauld18:08:24

lein install is local if I recall correctly, while lein deploy goes to clojars

pauld18:08:59

So I would have to remember to change the dependency of the project that is using your library.

kennytilton18:08:39

I think I just talked myself into using checkouts, and Googling lein uninstall. šŸ™‚

mg20:08:40

Sounds like a situation to use a SNAPSHOT version @hiskennyness

kennytilton20:08:56

I am on SNAPSHOT. I see that letā€™s me push the same version to Clojars and be noticed. But I do not type very well and cannot figure out the mechanics not to be prompted for credentials so I just go with checkouts. Yes, I am lame. :face_with_rolling_eyes: