Fork me on GitHub
#beginners
<
2018-02-07
>
suryapjr02:02:27

How to do 1 == 1 in clojure?

noisesmith02:02:46

numeric equality if you want to compare floating point and integral is ==, general equality is =

noisesmith02:02:19

(== 1 1) and (= 1 1) are both true, (== 1 1.0) is true (= 1 1.0) is false

suryapjr02:02:58

is clojure general-purpose?

noisesmith02:02:53

it's meant to be, there's definitely some things it does better than others - eg. if you need long running processes that use a lot of threads, clojure handles that very well, if you need short startup time and low RAM usage it might not fit as well

andy.fingerhut02:02:54

And I'd add that even if you don't need a lot of threads, Clojure can be cool, too.

andy.fingerhut02:02:13

Most of my Clojure is purely single-threaded.

noisesmith02:02:37

right - those were random examples of a good fit and a bad one

noisesmith02:02:44

there's others in each category ๐Ÿ˜„

andy.fingerhut02:02:56

Oh, I liked your examples. Just didn't want a beginner to get the impression that if you want to write single-threaded programs, that it wasn't in Clojure's wheelhouse, 'cause it sure is.

suryapjr02:02:29

Great stuff !!

andy.fingerhut02:02:56

I am reminded of a great answer someone gave to a question about whether Common Lisp was general purpose or not, which listed a bunch of application areas it had been successfully used in, but can't find it right now.

andy.fingerhut02:02:32

Found it, a quote from Kent Pitman: "Please donโ€™t assume Lisp is only useful for Animation and Graphics, AI, Bioinformatics, B2B and E-Commerce, Data Mining, EDA/Semiconductor applications, Expert Systems, Finance, Intelligent Agents, Knowledge Management, Mechanical CAD, Modeling and Simulation, Natural Language, Optimization, Research, Risk Analysis, Scheduling, Telecom, and Web Authoring just because these are the only things they happened to list. "

suryapjr02:02:55

Hahaha..superb

suryapjr02:02:05

Its so much less verbose

dpsutton03:02:02

I think the digital version is not for redistribution. It's free on the website. That should be removed

dpsutton03:02:16

@suryapjr do you mind deleting that?

suryapjr04:02:07

my apologies

dpsutton04:02:20

no worries. it's online for free at https://www.braveclojure.com/ and offers the digital download for sale. great business model and one i hope is successful for him

roelof06:02:11

@shaun-mahood thanks, I will look into that book

roelof06:02:54

and good morning all

seancorfield06:02:09

Mornin' @roelof!

seancorfield06:02:20

Welcome back to Clojure -- it's been a while, eh?

roelof06:02:55

did a side step with haskell and c#

seancorfield06:02:45

An interesting diversion! Having fun?

roelof06:02:08

I always having fun. From every language I learned something

hawari.rahman1706:02:30

Hello, is there any function to ensure that only certain set of keys are present in a map? For example I only want to allow :a :b :c keys in a map, so that a map like {:a 1 :b 2 :x 5} gets transformed into {:a 1 :b 2}

hawari.rahman1707:02:13

Nevermind, found select-keys

roelof07:02:47

What is wrong with the try for making a factorial :

(defn factorial [n]
  (loop [number n
         acc    1]
    (if (== n 1)
      acc
      (recur (- n 1) (* acc n )))))

roelof07:02:14

when I run it I see this message :

ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow (Numbers.java:1501)
 

xiongtx10:02:31

In loop you're binding n to number, but your if tests for (== n 1), which'll never be true. You want to use number in the body of the loop (or just bind n to n as in (loop [n n acc 1] ...)).

roelof11:02:57

oke, thanks. I will set up my cursive again and try again

hawari.rahman1707:02:34

Try this one @roelof

hawari.rahman1707:02:53

(defn factorial [n]
  (loop [number n
         acc    1]
    (if (== number 1)
      acc
      (recur (- number 1) (* acc number )))))

hawari.rahman1707:02:21

I think the issue is because you're using the n value as the indicator of when to stop, but n will never change its value

roelof07:02:47

thanks, I will try it

roelof07:02:33

hmm, or there is something wrong with my computer or something wrong with the koans but after several minutes the recursion part is still not evaluted

roelof07:02:06

(ns koans.14-recursion
  (:require [koan-engine.core :refer :all]))

(defn is-even? [n]
  (if (= n 0)
    true
    (not (is-even? (dec n)))))

(defn is-even-bigint? [n]
  (loop [n   n
         acc true]
    (if (= n 0)
      acc
      (recur (dec n) (not acc)))))

(defn recursive-reverse [coll]
  (loop [ col coll
         result []]
    (if (empty? col)
      result
      (recur (rest col) (cons (first col) result)))))

(defn factorial [n]
  (loop [number n
         acc    1]
    (if (== n 1)
      acc
      (recur (- number 1) (* acc number )))))

(meditations
  "Recursion ends with a base case"
  (= true (is-even? 0))

  "And starts by moving toward that base case"
  (= false (is-even? 1))

  "Having too many stack frames requires explicit tail calls with recur"
  (= false (is-even-bigint? 100003N))

  "Reversing directions is easy when you have not gone far"
  (= '(1) (recursive-reverse [1]))

  "Yet it becomes more difficult the more steps you take"
  (= '(6 5 4 3 2) (recursive-reverse [2 3 4 5 6]))

  "Simple things may appear simple."
  (= 1 (factorial 1))

  "They may require other simple steps."
  (= 2 (factorial 2))

  "Sometimes a slightly bigger step is necessary"
  (= 6 (factorial 3))

  "And eventually you must think harder"
  (= 24 (factorial 4))

  "You can even deal with very large numbers"
  (< 1000000000000000000000000N (factorial 1000N))

  "But what happens when the machine limits you?"
  (< 1000000000000000000000000N (factorial 100003N)))

roelof08:02:09

any recommendation for a good ide/text-editor with repl

tsulej08:02:09

emacs+cider, atom+protorepl, intellij+cursive

c09mld08:02:32

@roelof that's a naming issue, the if checks for equality against n, it should check against number

stardiviner08:02:13

What is the function to sleep for a few seconds? I have not found sleep or wait similar functions.

hawari.rahman1708:02:16

You can use Java sleep method from Thread class @stardiviner

hawari.rahman1708:02:28

(Thread/sleep 1000)

suryapjr10:02:53

Which are the best clojure frameworks?

delaguardo10:02:59

What kind of frameworks you are looking for?

suryapjr10:02:16

like rails,phoenix?

roelof11:02:01

@c09mld so the old error is still into it

roelof11:02:30

or is there another reason why the testing of koans takes ages

roelof11:02:38

Thanks, my problems are solved

roelof11:02:59

@suryapjr I know only one full stack one and that's luminus

gklijs11:02:28

@suryapjr not many โ€˜realโ€™ frameworks, since clojure is composible, there is https://github.com/fulcrologic/fulcro, and https://hoplon.io/ which have a highly coupled clojure/clojurescript, luminus gives you a starting point composed of different libaries.

roelof11:02:01

@donmullen is arachne also ready for production ?

sundarj11:02:41

@roelof i don't think so, there's a great big warning on all of the repos

suryapjr12:02:45

@donmullen Thanks !!

suryapjr12:02:10

@sundarj Thanks !!!

suryapjr12:02:33

I would like to build a clojar someday ๐Ÿ™‚

suryapjr12:02:39

hey what about luminus?

suryapjr12:02:46

Its a web framework right ?

sundarj12:02:44

@suryapjr yeah, luminus is the most popular one i think

teodorlu14:02:26

Hello! I am a novice trying to build a Clojure WebSockets client. The WebSockets client is a chat bot for Slack. I'm aware that there are complete solutions, but I want to understand what's going on underneath. I've looked at RFC6455, and I don't want to re-implement the protocol either. I'm currently looking at aleph, sente and chord. I've received thoughtful opinions here before, so I'm trying again. Any recommendations? Regards, Teodor

schmee14:02:00

Iโ€™ve used Sente in a project, it was a while ago but as I remember I had a good experience

teodorlu14:02:23

Thanks, @schmee. Looking further into sente now. Any others?

suryapjr14:02:48

@sundarj cool

suryapjr14:02:13

Are there any minimilaistic frameworks like sinatra,flask ?

suryapjr14:02:47

@mateusz.probachta thanks dude ..

suryapjr14:02:15

(awesome?clojurians) => true

suryapjr14:02:22

โค๏ธ

roelof16:02:39

For a finncial app can i better use hoplon or luminus

suryapjr16:02:16

So clojure is very good for backend ?

suryapjr16:02:49

Can deploy at heroku,right ?

radomski16:02:14

Yes in fact there exist build tool templates with heroku deployment built right in

radomski16:02:46

https://github.com/plexus/chestnut chestnut has heroku deployment by default

suryapjr16:02:23

@radomski great !!

suryapjr16:02:39

It's fun to be in clj world

suryapjr16:02:03

So... Can clojure do everything that java can do

suryapjr16:02:12

Since both run on jvm

radomski16:02:26

You can access all java libraries and primitives and export classes and interfaces that can be used from Java code

suryapjr16:02:45

How to use the or operator

suryapjr16:02:34

An example would be appreciated :)

radomski16:02:38

I opt for (or nil nil false 4) =>> 4

radomski16:02:48

4 is truthy so (if (or nil true) "True" "False") ==> "True"

suryapjr16:02:16

So it returns the truth value

radomski16:02:26

or (if (or nil nil false 4) "True" "False")

radomski16:02:43

it will return the first truthy value so 4

radomski16:02:50

which works in if as true

suryapjr16:02:55

So or can have multiple arguments ?

radomski16:02:15

it can have 0..n arguments

suryapjr16:02:17

@radomski how is ur code formating in red

radomski16:02:26

wrap in backticks

radomski16:02:53

you can also have syntax highlighing by wrapping your code in three backticks wheree the first one is `clojure

radomski16:02:35

Nvm I'm thinkin discord. Ignore me

radomski16:02:51

You can still click the plus and select clojure code snippet

suryapjr16:02:00

@Ryan thanks !!

suryapjr16:02:42

How would i import java.util.Random in clojure ?

sundarj17:02:52

@suryapjr you can just refer to java.util.Random - it'll be imported automatically. if you want to refer to it as just Random, then you can do (import 'java.util.Random)

alexmiller17:02:18

small distinction... โ€œimportโ€ is just a means of making a short name Random refer to the long name java.util.Random in Clojure code. Any time you refer to a class it will be automatically loaded if needed by the JVM.

sundarj17:02:01

haha yeah - my bad on the phrasing ๐Ÿ˜

suryapjr17:02:32

@..I meant alexmiller

suryapjr17:02:57

Okay...nice

suryapjr17:02:08

Could I get an example pls

sundarj17:02:09

user=> (java.util.Random.)
#object[java.util.Random 0x34237b90 "[email protected]"]

user=> (Random.)
CompilerException java.lang.IllegalArgumentException: Unable to resolve classname: Random, compiling:(NO_SOURCE_PATH:2:1)

user=> (import 'java.util.Random)
java.util.Random

user=> (Random.)
#object[java.util.Random 0x319854f0 "[email protected]"]

suryapjr17:02:18

@sundarj thank you !!!

sundarj17:02:30

no problem ๐Ÿ™‚

suryapjr17:02:43

How to do Arity overloading?

joelsanchez17:02:43

(defn multiarity
  ([a]
   (println a))
  ([a b]
   (println a b)))

suryapjr17:02:58

And ... What is let

joelsanchez17:02:46

I'd suggest you to go through a brief introductory course at https://www.braveclojure.com/clojure-for-the-brave-and-true/ or do some exercises on http://www.4clojure.com/

radomski17:02:55

I recommend brave clojure to all newcommers. It's very entertaining and covers the highlights really well

suryapjr17:02:59

@joelsanchez @radomski..the book is open by my side ..clojurians is on and the repl is ready

suryapjr17:02:38

@sundarj...sure !!

andy.fingerhut17:02:15

@suryapjr Excellent setup! I'd recommend section of book first, repeat any examples shown in the book in the REPL, and then try varying them if you are curious what those variations will do, re-read the section to see if it explains anything you didn't expect or don't understand, maybe some more REPL experiments, then clojurians when the book fails you.

roelof17:02:35

oke, I do first the clojure koans and after that the braveclojure book with some exercises of 4 clojure

roelof17:02:22

for a beginner could I better use hoplon or luminus

jmromrell17:02:55

I'm unfamiliar with hoplon, but used luminus when I was learning and did fine

jmromrell17:02:13

Luminus does come with a fair amount of included libraries a beginner won't need or use for quite a while though

jmromrell17:02:55

But if you're fine ignoring some of the template code you start with for a while, luminus gives you a good playground to mess around with and learn things

suryapjr18:02:31

@andy.fingerhut noted !!!

roelof18:02:54

a clojure koans question :

"You can regain the full argument if you like arguing"
  (= {:original-parts ["Stephen" "Hawking"] :named-parts {:first "Stephen" :last "Hawking"}}
     (let [[first-name last-name :as full-name] ["Stephen" "Hawking"]]
       __))

roelof18:02:28

must I made the output like the {:orginal_parts ..... } ?

noisesmith18:02:18

or call (hash-map โ€ฆ) but a map literal is usually nicer

noisesmith18:02:11

the point in this one must be showing you how destructuring lets you access the full input plus individual destructured parts

sabbatical201719:02:21

I have been playing with Clojure recently and I found it very fun. It was not too much hassle to set up an nrepl server in my app and to connect to it from emacs/CIDER (as well as Cursive). I have now tried to do something with clojurescript in my browser and had an unpleasant experience. This probably means that I am doing it wrong somehow...

sabbatical201719:02:31

What I am trying to achieve is some clojurescript running in a browser that I can interact with, and at the same time I can edit the code in my editor (preferably emacs/CIDER but Cursive might be fine too) and send forms from the editor directly to the running Clojurescript in the browser. I thought that this was what figwheel was for, so I tried the flappy bird demo. Typing lein figwheel gave me a repl, but it did not have any editing features (e.g., use left arrow to go back and fix a typo earlier on the line). The documentation on setting up figwheel with emacs/CIDER had a "very advanced" warning on it, as well as looking really fussy, so clearly that's not the right way to do it.

sabbatical201719:02:33

Can anyone suggest a more beginner-friendly environment for me to start with? I am also okay with trying a completely new workflow (i.e., not evaluating forms from the editor) if it still allows for an interactive development experience in a different way. (I'd like to be able to discover and modify the state of the running system easily.)

manutter5119:02:36

@sabbatical2017 do you have rlwrap installed? The rlwrap lein figwheel command give you back a lot of the edit functionality.

sabbatical201719:02:15

@manutter51 Thank you! rlwrap helps.

sabbatical201719:02:15

Is that the workflow you recommend? All these people working in clojurescript, it's hard to believe they don't have something slicker

noisesmith19:02:59

the other day bhauman shared a preview of something new he is working on that makes it much slicker

manutter5119:02:13

I used rlwrap with lein figwheel for a while, but then I switched to using the Cursive/IntelliJ integration, which is much nicer.

lee.justin.m19:02:34

I use atom + protorepl and now I use a technique @seancorfield has suggested in here before, which is to draft my interactions in a (comment) in my source code and send them to the repl using cmd-option-b

bmaddy21:02:34

@sabbatical2017 I like to start my repl from within emacs. From scratch it looks like this:

lein new figwheel demo
cd demo
emacs src/demo/core.cljs
Then, in emacs, run cider-jack-in-clojurescript (which is , " in spacemacs; I'm not sure what it is in normal emacs). This will open a clojure repl and a clojurescript repl. Code sent from clj files goes to the clj repl, and code in cljs files gets sent to the cljs repl. This simple setup has it working with the rhino js engine, but there is a way to get it to work with the browser repl too.