first time without state - and I'm lost

2020-05-12 Thread Scaramaccai
Hi everyone,

I wanted to give a try to Clojure and functional programming in general but 
I can't really stop thinking "object oriented" or "with state everywhere". 
After 20+ years with objects + state I guess I'm lost without them :)

The first thing I want to try is to get some data from an API that needs 
OAuth authentication. I would like to hide the fact that there's an OAuth 
token to be sent. In pseudo-Java I would do something like:

class MyAuthHttpClient {
  private token;
  public MyAuthHttpClient(String usr, String psw) {...}

  public ... getResponse(Url) {
  // here check if token is available and if it is expiring;
  // if expiring -> fetch a new token before call the http service
  // caller doesn't even know there's a token involved in the process
  }
}

What's the proper way to do that in Clojure?
I guess one way would be to have a function that returns a new token given 
a (possibly old) token and user+psw

(defn gettkn [usr, psw, tkn] (return a new token if tkn is expiring or tkn if 
not expiring))

(def wrap-gettkn (partial gettkn "myuser" "mypass"))


(defn geturl [url, tkn] client/get url {:oauth-token (wrap-gettkn tkn)})



I can "save" usr and psw, but I always have to "keep" the tkn around at 
every level;
while I would like the token to be "hidden" to the "geturl" clients (just 
like I did in the "private token" in the pseudo-Java).

What's the proper way of doing something like this in Clojure?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/6aa66613-24d9-4ebc-87d4-e9a6cca05165%40googlegroups.com.


Re: “compiling” stacktrace error

2016-02-12 Thread Scaramaccai


On Friday, February 12, 2016 at 9:51:50 AM UTC+1, Scaramaccai wrote:
>
>
>
> On Thursday, February 11, 2016 at 9:54:19 PM UTC+1, Sean Corfield wrote:
>>
>> Scaramaccai wrote on Thursday, February 11, 2016 at 8:32 AM: 
>> >I'm learning Clojure, and I find difficult to understand where a 
>> specific compiler error happens: 
>>
>> The stacktraces can be pretty daunting at first, unfortunately. 
>>
>> How are you compiling / running the code? That will have some bearing on 
>> how errors are reported. 
>>
>>
> Yes, the problem seems to be how I compiled the code. I was using 
> Vim+Fireplace, and doing a "cpr" (takes the content from the active buffer 
> and requires it inside the REPL) I had the error without a proper 
> stacktrace (see below). Running using "reipl run" gives the "proper" 
> stacktrace:
>


BTW using 

(.printStackTrace *e)

>From inside the REPL gave me the full stack... so I can actually keep using 
Fireplace and get the full stack when needed.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: “compiling” stacktrace error

2016-02-12 Thread Scaramaccai


On Thursday, February 11, 2016 at 9:54:19 PM UTC+1, Sean Corfield wrote:
>
> Scaramaccai wrote on Thursday, February 11, 2016 at 8:32 AM: 
> >I'm learning Clojure, and I find difficult to understand where a specific 
> compiler error happens: 
>
> The stacktraces can be pretty daunting at first, unfortunately. 
>
> How are you compiling / running the code? That will have some bearing on 
> how errors are reported. 
>
>
Yes, the problem seems to be how I compiled the code. I was using 
Vim+Fireplace, and doing a "cpr" (takes the content from the active buffer 
and requires it inside the REPL) I had the error without a proper 
stacktrace (see below). Running using "reipl run" gives the "proper" 
stacktrace:

[...]
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to 
clojure.lang.IPersistentCollection
at clojure.core$conj__4345.invokeStatic(core.clj:82)
at clojure.core$conj__4345.invoke(core.clj:82)
at fwpd.core$fib_seq3.invokeStatic(core.clj:98)
at fwpd.core$fib_seq3.invoke(core.clj:92)
at fwpd.core$fib_seq3.invokeStatic(core.clj:94)
at fwpd.core$fib_seq3.invoke(core.clj:92)
at fwpd.core$eval70.invokeStatic(core.clj:105)
at fwpd.core$eval70.invoke(core.clj:105)
at clojure.lang.Compiler.eval(Compiler.java:6927)
at clojure.lang.Compiler.load(Compiler.java:7379)


*Thank you!!!*


Fireplace+REPL stacktrace:

|| java.lang.ClassCastException: java.lang.Long cannot be cast to 
clojure.lang.IPersistentCollection, compiling:(fwpd/core.clj:100:1)
|| clojure.lang.Compiler.load(Compiler.java:7391)
|| clojure.lang.RT.loadResourceScript(RT.java:372)
|| clojure.lang.RT.loadResourceScript(RT.java:363)
|| clojure.lang.RT.load(RT.java:453)
|| clojure.lang.RT.load(RT.java:419)
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5893|
 
clojure.core$load$fn__5677.invoke
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5892|
 
clojure.core$load.invokeStatic
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5876|
 
clojure.core$load.doInvoke
|| clojure.lang.RestFn.invoke(RestFn.java:408)
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5697|
 
clojure.core$load_one.invokeStatic
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5692|
 
clojure.core$load_one.invoke
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5737|
 
clojure.core$load_lib$fn__5626.invoke
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5736|
 
clojure.core$load_lib.invokeStatic
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5717|
 
clojure.core$load_lib.doInvoke
|| clojure.lang.RestFn.applyTo(RestFn.java:142)
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|648|
 
clojure.core$apply.invokeStatic
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5774|
 
clojure.core$load_libs.invokeStatic
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5758|
 
clojure.core$load_libs.doInvoke
|| clojure.lang.RestFn.applyTo(RestFn.java:137)
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|648|
 
clojure.core$apply.invokeStatic
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5796|
 
clojure.core$require.invokeStatic
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|5796|
 
clojure.core$require.doInvoke
|| clojure.lang.RestFn.invoke(RestFn.java:421)
|| fwpd.core$eval3604.invokeStatic(form-init936506867427907734.clj:1)
|| fwpd.core$eval3604.invoke(form-init936506867427907734.clj:1)
|| clojure.lang.Compiler.eval(Compiler.java:6927)
|| clojure.lang.Compiler.eval(Compiler.java:6890)
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|3105|
 
clojure.core$eval.invokeStatic
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\core.clj|3101|
 
clojure.core$eval.invoke
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\main.clj|240|
 
clojure.main$repl$read_eval_print__7408$fn__7411.invoke
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::clojure\main.clj|240|
 
clojure.main$repl$read_eval_print__7408.invoke
zipfile:C:\Users\105066315\.m2\repository\org\clojure\clojure\1.8.0\clojure-1.8.0.jar::c

“compiling” stacktrace error

2016-02-11 Thread Scaramaccai


I'm learning Clojure, and I find difficult to understand *where* a specific 
compiler error happens:


java.lang.ClassCastException: java.lang.Long cannot be cast to 
clojure.lang.IPersistentCollection, compiling:(fwpd/core.clj:100:1)


Line 100 is just:


(fib-seq3 5)


So it says nothing, because in fact the error is in the fib-seq3 function 
(parameters to a "conj" call are inverted, see below).


Is this normal? No way to know where an error is???


Just for reference, here's the code (again, *I know where the error is*; I 
just don't understand how was I supposed to find it, given that the message 
doesn't tell me at which line it happens):


(defn fib-seq3
  ([to]
   (fib-seq3 [] 0 1 0 to))
  ([coll a b k to]
(if (= k to)
  coll
  (fib-seq3 (conj b coll) b (+ a b) (inc k) to)))
(fib-seq3 5)



Thank you!


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.