Fork me on GitHub
#beginners
<
2018-02-07
>
pooboy02: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

pooboy02: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.

pooboy02: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. "

pooboy02:02:55

Hahaha..superb

pooboy02: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?

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

hawari06: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}

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

hawari07:02:53

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

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

genmeblog08:02:09

emacs+cider, atom+protorepl, intellij+cursive

Markus Åkerlund Larsson08: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.

hawari08:02:16

You can use Java sleep method from Thread class @stardiviner

hawari08:02:28

(Thread/sleep 1000)

pooboy10:02:53

Which are the best clojure frameworks?

delaguardo10:02:59

What kind of frameworks you are looking for?

pooboy10:02:59

Full stack

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

pooboy12:02:33

I would like to build a clojar someday 🙂

pooboy12:02:39

hey what about luminus?

pooboy12: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?

pooboy14:02:13

Are there any minimilaistic frameworks like sinatra,flask ?

pooboy14:02:15

(awesome?clojurians) => true

roelof16:02:39

For a finncial app can i better use hoplon or luminus

pooboy16:02:16

So clojure is very good for backend ?

pooboy16:02:49

Can deploy at heroku,right ?

Ryan Radomski16:02:14

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

Ryan Radomski16:02:46

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

pooboy16:02:39

It's fun to be in clj world

pooboy16:02:03

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

pooboy16:02:12

Since both run on jvm

Ryan Radomski16:02:26

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

pooboy16:02:45

How to use the or operator

pooboy16:02:34

An example would be appreciated :)

Ryan Radomski16:02:38

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

Ryan Radomski16:02:48

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

pooboy16:02:16

So it returns the truth value

Ryan Radomski16:02:26

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

Ryan Radomski16:02:43

it will return the first truthy value so 4

Ryan Radomski16:02:50

which works in if as true

pooboy16:02:55

So or can have multiple arguments ?

Ryan Radomski16:02:15

it can have 0..n arguments

pooboy16:02:17

@radomski how is ur code formating in red

Ryan Radomski16:02:26

wrap in backticks

Ryan Radomski16:02:53

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

Ryan Radomski16:02:35

Nvm I'm thinkin discord. Ignore me

Ryan Radomski16:02:51

You can still click the plus and select clojure code snippet

pooboy16:02:00

@Ryan thanks !!

pooboy16: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)

Alex Miller (Clojure team)17: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 😁

pooboy17:02:32

@U064X3EF3..I meant alexmiller

pooboy17:02:57

Okay...nice

pooboy17:02:08

Could I get an example pls

sundarj17:02:09

user=> (java.util.Random.)
#object[java.util.Random 0x34237b90 "java.util.Random@34237b90"]

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 "java.util.Random@319854f0"]

sundarj17:02:30

no problem 🙂

pooboy17:02:43

How to do Arity overloading?

joelsanchez17:02:43

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

pooboy17: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/

Ryan Radomski17:02:55

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

pooboy17:02:59

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

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

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.

justinlee19: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.