Fork me on GitHub
#beginners
<
2017-08-10
>
sobiaadil05:08:49

Hi everyone ! I am new to clojure. Have read clojure for brave and true. Want to do a web based project in clojure. Any suggestions ??

jumar16:08:35

@sobiaadil you may want to check "Web development with clojure 2nd ed." book - it uses Luminus which is a pretty decent web app template combining multiple useful libraries.

sobiaadil16:08:07

Hi !! Thanks I will look into it.

clj.max09:08:22

Hi, I'm looking for something like NCurses for a Clojure console application. I basically want to use raw keypresses in the console. Any suggestions on how to get them or what library to use? The only thing I've found is JCurses, but not sure how to use that from Clojure. Thanks!

bfabry15:08:04

@U5JPZFFR6 another alternative would be to write in cljs and use a nodejs ncurses lib. example: https://www.reddit.com/r/Clojure/comments/6qd05a/what_other_platform_would_you_most_like_to_see/dkwtzw5/ obviously this option is very dependent on your other requirements and how comfortable you are with cljs

clj.max19:08:46

@U06CM8C3V thanks, that looks exactly like what I need

clj.max19:08:01

I'd thought about cljs just because I'm more familiar with JS than Java, but don't care either way

daedelus198209:08:51

@sobiaadil not got any web based projects but you might want to check out the 4clojure website if you havent before http://www.4clojure.com/. There are about 150 problems there where you need to write a simple function to pass the given questions. Great for learning and great fun too

sobiaadil09:08:15

@daedelus1982 I'm already doing the 4clojure problems. Thanks!!!

daedelus198209:08:57

cool, šŸ™‚

daedelus198210:08:19

I ran into something I cant explain. A function I wrote ran without throwing an exception whereas there was a mistake in it and there should have been an Arity exception. The question was about finding anagrams on the 4clojure site: http://www.4clojure.com/problem/77. And here was my solution: (fn [c] (let [anagram? #(and (= (count %1) (count %2)) (empty? (clojure.set/difference (set %1) (set %2)))) anagrams #(set (conj (filter (partial anagram? %2) %1)))] (set (remove #(= 1 (count %)) (distinct (map (partial anagrams c) c)))))) In my local REPL there was no exception but in a separate REPL the expected exception was thrown because there is a missing argument to conj function in the anagrams binding. Presumably my local REPL was using an older working 'let anagrams' binding and not the new broken one. Are let bindings memoized or something?

clj.max10:08:47

I've sometimes found that my local REPL and the 4clojure one slightly differed. Maybe Clojure versions.

clj.max10:08:03

I don't recall what problem it was, but a few times I had to use slightly different stlib functions to make something work

daedelus198210:08:01

thanks, I just tried the same broken function in a rebooted REPL with different arguments and it worked fine. no expected exception

manutter5112:08:01

@daedelus1982 I think older versions of Clojure throw an arity exception if you call conj with only a single arg, but I just looked up the source for conj in Clojure 1.8.0, and it now accepts zero or more args.

manutter5112:08:49

Also, if you go to the main 4clojure page, thereā€™s a link to the github repo. I did a quick clone and checked the project.clj, and 4clojure is still on clojure 1.4.0, so thatā€™s probably why your code throws an exception on 4clojure but not in your local REPL.

daedelus198213:08:57

thanks for the help. yes, the clojure version does seem to be the cause here. FYI im using 1.8.0

nmax14:08:49

Hi, how can I set use-context-classloader to false? I have a problem that classes from my clojure lib (compiled with gen-class) and added to war are not observable. I have to do ugly thing like ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

nmax14:08:28

so the questions: can I change the default clojure classloader and where should I do it?

sundarj14:08:29

(set! *use-context-classloader* false)

nmax15:08:38

@sundarj thx, i'll try

colinkahn17:08:19

Iā€™ve been trying to use maps in my programs vs creating new types but iā€™m wondering what the best way to achieve polymorphism is? When everything is a map I canā€™t dispatch on type, so protocols arenā€™t that useful. Iā€™m struggling to see how multimethods can be used, and right now my best solution is to add a :type to my data and dispatch on that. Iā€™m wondering if this is a good pattern to adopt.

sundarj17:08:41

that's what Records are for

manutter5117:08:42

If you need polymorphism then itā€™s legit to start defining Records. If youā€™re new to functional programming, though, itā€™s possible youā€™re reaching for polymorphism prematurely, if youā€™re used to doing things the OOP way. It might be worth describing the bigger picture of what youā€™re trying to do that you want polymorphism for.

manutter5117:08:34

Iā€™m not assuming that youā€™re necessarily doing it wrong, but just thought Iā€™d mention the possibility, since this is the beginners channel.

colinkahn18:08:45

@sundarj I was considering using Records kind of the same as defining my own type in this case. Trying to see if I can just use plain maps.

sundarj18:08:18

yeah, but if you need polymorphism - then plain maps don't cut it

sundarj18:08:01

using Records is a better approach than defining your own ad-hoc :type keys

sundarj18:08:42

although what @U06CM8C3V said is very good

colinkahn18:08:01

@U06CM8C3V Iā€™m just thinking about how to design my app where I might have different data, like users, movies, movie clips etc, and I want to do similar operations on them without necessarily needing to know the shape of the data (keys)

colinkahn18:08:43

that might be an un-Clojure way of thinking about it though

sundarj18:08:27

i wouldn't say so, but i am new to this too šŸ˜›

colinkahn18:08:28

@sundarj yeah, using :type didnā€™t feel right, so at least I know that much now šŸ˜„

colinkahn18:08:09

@sundarj I was saying that ā€œdo similar operations without know the keysā€ might be un-Clojure

sundarj18:08:39

ah, right. possibly

sundarj18:08:35

though maybe that's just to simplify the example

colinkahn18:08:11

yeah, good example, Iā€™ve seen others, and Iā€™d assume that sort of pattern is what hierarchies are meant for: https://clojure.org/reference/multimethods

tbaldridge18:08:09

Often though what you would normally use polymorphism for can be divined from the data itself, and multimethods work great for that

tbaldridge18:08:37

For example you could assume that a map with :width and :height calculates its area via width * height. While a map with :radius would calculate it differently.

colinkahn18:08:34

@U07TDTQNL that seems more in the spirit of keeping it all data

manutter5118:08:47

Whatā€™s nice about functional programming is that you often donā€™t need polymorphism, because all youā€™re interested in is one or two keys in the map. Movies, actors, directors, and studios might all have a :name key, and you might have a function that, I dunno, translates names into pig-latin. All the function needs to do is grab the :name, transform it, and return the result. It doesnā€™t care if itā€™s a movie name or an actor name or whatever.

manutter5118:08:21

There are perfectly legit uses for Records and polymorphism in Clojure, but thereā€™s also a lot of cases where itā€™s not needed, and we just reach for it out of habit. Or at least I did when I was first learning.

sundarj19:08:10

i guess the solution is to force yourself to use maps, and then if/when that breaks down you can look at Records and whatnot

tbaldridge19:08:58

pretty much, maps are almost always the right option, sometimes records are faster, that sometimes is a lot less often than people would assume

sundarj19:08:18

awesome, thanks šŸ™‚

fominok17:08:57

Hi! I have a question about performing IO. Would you mind if I link a stackoverflow?

noisesmith18:08:14

@fominok agents are best for the case where you need to accumulate results from these asynchronous operations, but they should happen one at a time

noisesmith18:08:50

if you want to run them in parallel, and still merge results, Iā€™d use futures that call swap! on an atom only after they get their result (so that only the data modification is retried, and not the http request)

noisesmith18:08:18

a single agent will only run one action at a time

noisesmith18:08:43

and using a single agent for a request that doesnā€™t rely on the data in other requests doesnā€™t really make much sense

noisesmith18:08:29

for more complex interactions Iā€™d look into core.async (putting IO into core.async/thread invocations, and reading the result from the channel returned) or possibly a mix of futures for IO and refs for state coordination - really depends on what kind of coordination needs you have

meows19:08:49

Are all the Lisps basically transposable

meows19:08:02

So, you won't make a choice learning the "wrong" Lisp?

v3ga19:08:57

Yes and no. Iā€™m not having a problem reading On Lisp and the little schemer while studying clojure.

v3ga19:08:57

Yes and no. Iā€™m not having a problem reading On Lisp and the little schemer while studying clojure.

meows19:08:11

Is there a canonical book for the Clojure community?

meows19:08:20

Is it the Brave and True book?

noisesmith19:08:16

some people like that one, but thereā€™s also Joy of Clojure and Clojure Programming and Clojure Applied - depends on your tolerance for silliness

noisesmith19:08:29

because Brave and True is definitely silly

dees19:08:52

i'm part-way into the brave and true book and enjoying it, but got a little stuck on one of the exercises and haven't gotten back to it for a while. hopefully i'll find some time in the near future to go back to it and get some help from here

sb21:08:00

I would like to run this (merge-with union {1 [1]} {1 [3]}) code and I have ({1 [1]} {1 [3]}) format. How can I change in easy way to use with merge-with union? flatten not works.. any idea? maybe Iā€™m tired šŸ™‚

noisesmith21:08:07

@sb Iā€™d expect (merge-with into ...) to work

sb21:08:32

@noisesmith thanks I try it now!

sb21:08:31

but I have an extra ( ) around of this.. that is my problem

noisesmith21:08:36

@sb clojure.set has many functions that quietly accept things that are not sets but also donā€™t quite do the right thing (though Iā€™m not sure union is one of them, I think into has the correct collection-independent behavior)

sb21:08:02

ohh thanks!! šŸ’ŖšŸ‘