Fork me on GitHub
#beginners
<
2016-01-24
>
jonahbenton02:01:58

hey @guru01-have you seen the Clojure Cookbook? How does this code snippet strike you? https://github.com/clojure-cookbook/clojure-cookbook/tree/master/05_network-io

jonahbenton02:01:55

That code is all you need for a program that receives a request- like from a browser- and sends a response back. On top of that, a basic http server is just having your server send text responses that conform to the basics of the protocol. This is a gentle introduction to that protocol: https://www.jmarshall.com/easy/http/

jonahbenton02:01:42

This person has gone through a similar exercise: http://angeleah.com/blog/index.html, with posts on building an Echo Server, and an Agreeable Web Server. Maybe those are helpful?

jonahbenton02:01:58

Unfortunately the Clojure libraries that hide the underlying "Socket" abstraction in Java- which Java itself adopted, and which dates back more than 30 years- look up Berkeley Sockets if interested- are typically intended to serve more sophisticated use cases and programming styles, and may require a much greater cognitive lift.

guru0108:01:33

Thanks a lot Jonah. That is very helpful. I did see that chapter in Clojure Cookbook - but not in detail. Let me go digging a bit more. All those links and info are very useful.

meow14:01:30

props to @jonahbenton on crushing it with the detailed help and code reviews

jonahbenton18:01:57

sure, thanks, happy to help @meow @guru01

ioneyed18:01:04

I am converting a string map to keyword map and trying to massage some data as well as manipulate other. The :company value is set but when it reaches the function (accounts/account-create) it shows up nil.

user (-> (zipmap (map keyword (keys user)) (vals user))
                 (assoc :password salt)
                 (assoc :account_id (accounts/account-create (get user :company nil)))
             )
Here is the map
{:first_name John, :last_name Quincy, :uid , :email , :password CYE8MGnf3Dyj, :company tester-mctestington, :account_id nil}

jonahbenton18:01:46

hey @ioneyed the user object is a string -> value map? so (get user :company nil) should be (get user "company" nil) ?

plexus18:01:08

is your accounts/account-create function working correctly?

ioneyed18:01:32

the accounts/account-create currently just prints the incoming company and it reports a nil

plexus18:01:32

I tried to make your example self-contained, and this seems to work

plexus18:01:40

(def user 
     {:first_name "John"
     :last_name "Quincy"
     :uid ""
     :email ""
     :password "CYE8MGnf3Dyj"
     :company "tester-mctestington"
     :account_id nil})

(-> (zipmap (map keyword (keys user)) (vals user))
    (assoc :password "hard-coded-salt")
    (assoc :account_id (str "company->account: " (get user :company nil))))

plexus18:01:46

so if you swap accounts/account-create with identity you get the same result?

ioneyed18:01:15

@jonahbenton: from my learning I thought the (-> operator would have applied the conversion from a string map to a keyword map before it got to my assoc :account_id so that it would be available via keywords

ioneyed18:01:09

@jonahbenton: I was wrong and never thought to try that very simplistic attempt. It is being passed through now as it should.

jonahbenton19:01:33

@ioneyed- cool- one thing to still observe though about that code pattern. what that code is doing is "reassigning" a new map to a previously assigned local symbol. that will "work" but it's kind of anti-idiomatic, in that it's in the vein of mutability. so, one approach would be to populate a "str-user" symbol with the string map, and then assign a "kw-user" with the results of the (-> ) block.

ioneyed19:01:49

I guess the better solution is to find a way to get it from compojure as a kw-map instead of str-map to begin with.

jonahbenton19:01:45

sure, though in web apps it's common and good practice to have an explicit data ingestion/validation/transformation step. Incoming data should be considered by default untrusted and at minimum in need of validation, subsequent to which it can be transformed into the model that suits the internal logic of the app

hoopes20:01:12

Hi, I’m trying to do remote CLJS figwheel stuff - I’m working on a remote server (in this case, a VM). I want my client code to connect to the websocket server on this VM. I’m using the re-frame todomvc example: . I added :figwheel { :websocket-host “VM ip addr” } under the :source-paths [“devsrc”] line, but i still get WebSocket connection to '' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED . I’m feeling pretty dumb at this point, am I missing something obvious? Thanks very much in advance for any help.

hoopes20:01:09

I was expecting my config change to change the URL in that error message...

hoopes20:01:33

i’m just running lein figwheel, fwiw, so i’m not sure if the lein profiles are playing into this?

hoopes21:01:50

welp, nevvvvvvvvver mind - found the actual place to put the config over here:

mark4carter21:01:45

Does anyone know how to use void mutating java methods in clojure

mark4carter21:01:51

I'm using the itext library(api?) and there's a .setData() method, but it only returns void, I can't seem to get the new object after .setData()

mark4carter21:01:00

I tried doto, and the method does more than just change set one field, so I don't think set! applies here either

mark4carter21:01:17

I've been experimenting with StringBuilder's .trimToSize() to test how to get a void method to work, how would I implement that for example?

jonahbenton21:01:58

hey @mark4carter doto is the right approach. can you share a non-working example?

mark4carter21:01:20

(defn testSB
	[]
	(.toString (doto (StringBuilder. "   jump  ")
					(.trimToSize)
		)))

mark4carter21:01:19

i think i read .trimtosize incorrectly

mark4carter21:01:22

that might be the issue

jonahbenton21:01:11

ah, yeah. that doesn't do trim() on the visible part of the string, e.g. eliminate whitespace

mark4carter22:01:43

let me try testing with something else, it could've been working how it was suppose to

mark4carter22:01:05

ok yeah perfect

mark4carter22:01:10

(defn testSB
	[]
	(.toString (doto (StringBuilder. "jump")
						(.setCharAt 1 (char 97))
			)))

mark4carter22:01:14

works like it should