first time without state - and I'm lost

2020-05-12 Thread Gerard Klijs
What helped for me was picking an existing  Clojure project, and try to make 
changes to it. I also struggled with some things. And not declaring and 
mutating objects was confusing at times. But I think with this regard it helped 
me a lot. In my case it was an existing cljs snake game 
https://github.com/OMantere/cljs-snake and add multi player with a clj backend 
to it.

It's just an idea, but I think this might work better than trying to convert 
existing oop code. 

-- 
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/cbf5af7e-8175-4f4b-a51a-a735517e5525%40googlegroups.com.


Re: first time without state - and I'm lost

2020-05-12 Thread Jag Gunawardana

I know that lost feeling. I came from a background of OO too. Most of the 
languages I'd used (C++, Java, Python, Go) had either total OO orientation, 
or at least pushed you into an OO way of thinking. I found that reading 
some Clojure books and other's code helped a lot (Joy of Clojure, Eric 
Normand's content etc). Once I got my head around some practical examples 
like the one you state, I found that my thinking changed. I also found the 
MIT SCIP fried the way I think and spat it out anew. It takes time and 
practice (well at least it took me time and practice).

I think that what you are doing is similar to something I did to get the 
Google Identity Keys. 

(defmethod ig/init-key ::get-google-keys [_ _]
  (let [gkeys (ref {:keys nil :expires 0})]
(fn []
  (let [current @gkeys]
(:keys (if (>= (c/to-epoch (t/now)) (:expires current))
 (do
   (log/info "Fetching Google Keys, expiry: " (:expires 
current))
   (dosync
(ref-set gkeys (fetch-google-keys ::jwk ;; only use 
JWK for now
 current))

I was using the excellent Integrant library, but it would work the same 
without it. I guess that you could do something similar with your token, if 
it had expired, then it would fetch it?

The main thing that I changed in my mind is that it is ok to pass the state 
to a function and get back a response, hiding everything in private data 
doesn't give you as much as OO promises it will.



On Tuesday, 12 May 2020 09:38:08 UTC+1, Orestis Markou wrote:
>
> I’m interested in this too — you can get inspiration from 
> https://github.com/cognitect-labs/aws-api/blob/master/src/cognitect/aws/credentials.clj
>  which 
> does something similar for expired credentials. Seems like a mutable 
> internal field is fine for this use case.
>
> On 12 May 2020, at 9:27 AM, Scaramaccai > 
> wrote:
>
> 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 clo...@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
> clo...@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 clo...@googlegroups.com .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/clojure/6aa66613-24d9-4ebc-87d4-e9a6cca05165%40googlegroups.com
>  
> 
> .
>
>
>

-- 
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 

Re: first time without state - and I'm lost

2020-05-12 Thread Orestis Markou
I’m interested in this too — you can get inspiration from 
https://github.com/cognitect-labs/aws-api/blob/master/src/cognitect/aws/credentials.clj
 

 which does something similar for expired credentials. Seems like a mutable 
internal field is fine for this use case.

> On 12 May 2020, at 9:27 AM, Scaramaccai  wrote:
> 
> 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
>  
> .

-- 
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/7C785215-2169-461D-917F-0605F30B0154%40orestis.gr.


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.