Fork me on GitHub
#beginners
<
2016-11-14
>
yusup08:11:41

Hi , is it OK to call itself within a macro ?

yusup08:11:42

it is a macro with one definite arity .

yusup08:11:12

I am getting StackOverflowError .

veggiemonk12:11:29

Hi everybody, I made this Atom theme to help me to learn clojure faster because I always make mistakes, then parinfer change a paren without me noticing it. This leads to error messages are often cryptic for a new comers (to say the least) so here you go https://atom.io/packages/clojure-color-scope-syntax http://i.imgur.com/LEilNt1.png

akiroz12:11:28

@yusup probably not ok, macros are expanded at compile-time, this is an infinite loop

akiroz12:11:29

but even with real functions, you should use the (loop [] (recur)) structure because clojure (or rather the JVM) doesnt support tail-call optimization

yusup12:11:40

yeah. compile time . I cant do that. I need to create some helper function to achieve what i want. thanks. @akiroz

alexmiller13:11:32

It's ok to expand to a call to yourself, just don't actually call yourself in the evaluated parts of the macro

alexmiller13:11:44

Lots of macros expand with a call to themselves

alexmiller13:11:04

Like or or and

roelofw14:11:02

is this a good solution of a recursion exercises of clojure koans :

(defn recursive-reverse [coll]
  (loop [ coll  coll
         acc () ]
    (if (empty? coll)
      acc
      (recur (rest coll)  (conj acc (first coll)))))) 

kauko15:11:38

That looks good to me. But you can use destructuring for more idiomatic code 🙂 (let [[first & rest ] coll ...])

roelofw15:11:31

I know , but the challenge was to use recursion or do I misunderstand you

kauko15:11:13

Heh, yes. You do. You can destructure the coll in the let, so you dont need calls to the rest and first functions 🙂

kauko15:11:56

(defn recursive-reverse [coll]
  (loop [ [element & the-rest]  coll
         acc '() ]
    (if-not element 
      acc
      (recur the-rest  (conj acc element))))) 

roelofw15:11:47

oke, thanks for letting me see what you mean

roelofw15:11:58

I have to look up what if-not does

kauko15:11:24

it's just like if, just "the other way around"

roelofw15:11:58

oke, and it does now the same as my (empty? coll) ?

kauko15:11:55

the name element in my example is the first element of coll. Nil in Clojure is a false'y value, which means, if there are no more elements in coll, element will be nill, which will be evaluated as false.

kauko15:11:58

Which means we return the acc

kauko15:11:02

Understand? 🙂

roelofw15:11:14

yes ,thanks

roelofw15:11:37

I learn and understand more and more the clojure way of doing things

kauko15:11:46

Do you have experience with any other languages before coming to clojure?

roelofw15:11:18

ruby and haskell

roelofw15:11:38

both a little bit

kauko15:11:18

java and javascript before coming to clojure. Been using clojure for 1.5 years now professionally 🙂

roelofw15:11:18

oke, I do not think I can use it proffessionaly. In the Netherlands almost none clojure jobs

roelofw15:11:56

I hope when Im done with the koans and have solved a lot of 4 clojure I can start with making web sites with clojure, @kauko

roelofw15:11:57

to get "apples" out of this sentence : "An Oxford comma list of apples, " can I just do (last (split " " "An oxford omma list of apples." )) ?

madstap17:11:23

The string argument comes first, and the other argument needs to be a regex. Also you'd usually have clojure.string aliased to str.

(ns foo.core
  (:require
      [clojure.string :as str]))

madstap17:11:08

@roelofw ^

cschep17:11:38

how does clojurebot work

cschep17:11:46

/clj (reduce + [1 2 3 4 5])

madstap18:11:00

you write /clj then the expression

madstap18:11:54

Also you can direct message the clojurebot to test it out without cluttering up the channel

roelofw18:11:30

thanks maddstap

roelofw18:11:35

I think I misunderstood the challenge

roelofw18:11:50

This is the challenge :

"Whether in function definitions"
  (= (str "An Oxford comma list of apples, "
          "oranges, "
          "and pears.")
     ((fn [[a b c]] __)
      ["apples" "oranges" "pears"])) 

cschep18:11:07

they want you to provide a function that will make the correct string when passed [”apples” “oranges” “pears]

cschep18:11:31

sorry, they want you to provide a function body, to that function.

roelofw18:11:04

oke, then I think about that. I think the solution is not very difficult

roelofw18:11:26

Is there a way I can see what this function produces :

((fn [[a b c]] str("An Oxford list of " a ", " b " and " c "." ))
      ["apples" "oranges" "pears"]) 

madstap19:11:39

@roelofw Do you have a dev environment set up for clojure?

roelofw19:11:16

do you mean if I have a repl, yes, I have that

roelofw19:11:37

I work with intelij with the cursive plugin

madstap19:11:55

So then I what do you mean by seeing what the function produces? Just run it at the repl...

madstap19:11:41

That particular funtion will throw an error like java.lang.String cannot be cast to clojure.lang.Ifn

roelofw19:11:16

Then I see this error :

ClassCastException java.lang.String cannot be cast to clojure.lang.IFn  koan-engine.runner/eval1814/fn--1816 (form-init2907239809023632635.clj:1)

roelofw19:11:35

so you are right about the error messsage

madstap19:11:55

You need to remember that function calls in a lisp are (do-stuff 1 2 3) and not do-stuff(1, 2, 3)

madstap19:11:10

In this case str is the function

madstap19:11:23

You will see that error message a lot when learning clojure by the way: foo.bar.FooBar cannot be cast to clojure.lang.IFn

roelofw19:11:33

oke, I have now this :

(fn [[a b c]] (str "An Oxford list of " a ", " b " and " c "." )
   ["apples" "oranges" "pears"])
=> #object[koan_engine.runner$eval1886$fn__1888 0x1da0e59 "[email protected]"] 

roelofw19:11:45

so still no output

madstap19:11:04

You removed the outer parens

roelofw19:11:06

maybe change fn to defn

cschep19:11:23

that’s evaluating to a function, but that function isn’t being called

roelofw19:11:16

and I cannot call it because the function has no name 😞

madstap19:11:11

You can call it, just put it at the front of a list... The first element of the list is evaluated as a function with the rest of the elements as arguments

cschep19:11:27

haa, but you can easily give it a name! or.. you can call it directly after defining it. like madstap suggested, by putting the outer parens back.

roelofw19:11:27

wierd on me :

((fn [[a b c]] (str "An Oxford list of " a ", " b " and " c "." )
   ["apples" "oranges" "pears"]))  
it still gives no output

madstap19:11:55

the vector is inside the fn form

roelofw19:11:57

oke, I see , I messed up the parentheses

cschep19:11:35

ahh, there you go.

roelofw19:11:59

sometimes the parentheses driven me crazy. and this are still the koans 😞

cschep19:11:29

the parens are harder at the repl I think

cschep19:11:35

editors help a lot

madstap19:11:38

Yeah, you'll get used to them pretty quickly

madstap19:11:00

You should definitely use paredit, parinfer or something similar though, even though it might feel strange at the start

roelofw19:11:56

I used it , With cursive I can choose between both

roelofw19:11:19

but I noticed that on repl I have to use one and when I type code I have to use another

roelofw19:11:26

I find that annoying

cschep19:11:26

ah, no separate settings for each?

roelofw19:11:25

@cschep not that I know of

cschep19:11:35

bummer, that does sound annoying 🙂

roelofw19:11:42

I like to use cursive on windows

roelofw19:11:20

maybe later this month Im going to try to run a Linux distro on a usb stick . Then I can test other "ide's "

roelofw19:11:40

or maybe try atom or sublime text on Windows

roelofw19:11:05

No idea if both have plugins for clojure development and if I can use a repl

seancorfield19:11:03

I tried Sublime Text and it drove me nuts. Weird bugs. Little inconsistencies across Mac, Windows, Linux. And the REPL experience really wasn’t very good.

dpsutton19:11:10

what do you mean > but I noticed that on repl I have to use one and when I type code I have to use another

seancorfield19:11:08

I ended up going back to Emacs (after about a 20 year gap) and that’s where I’ve stayed ever since. Well documented, lots of good Clojure configurations (I use Prelude), consistent experience across Mac, Windows, Linux.

roelofw19:11:04

@dpsutton i have to use parinfer on the repl but on the code editor I had to use a paredit to make things work

roelofw19:11:30

@seancorfield have you ever tried inteliij with cursive ?

dpsutton19:11:31

the repl is not inside of the code editor?

dpsutton19:11:42

get that fixed

dpsutton19:11:48

your editor should be attached to your repl

roelofw19:11:13

o, I can send things to repl but there are seperate windows on my screen

dpsutton19:11:38

are they the same program?

roelofw19:11:39

I have a code editor screen, a repl output screen and a repl input screen

roelofw19:11:02

@dpsutton yep, there are all on the intelij programm

dpsutton19:11:18

can you explain why you have to use different ones based on repl or code then?

roelofw19:11:21

If I have one choosen on repl and I type something the closing ) disappear but if I type with the same on my code part everything works well

roelofw19:11:30

and the other way around

dpsutton19:11:14

i'm not sure i understand

dpsutton19:11:39

it deletes closing parens on the repl?

dpsutton20:11:21

and this doesn't happen if you use one mode on the repl and a different mode in code?

roelofw20:11:47

that is correct

seancorfield21:11:09

@roelofw I’ve tried various IntelliJ releases and just don’t like it. They’ve even sent me free licenses to review the product for my blog… but I really don’t like it and didn’t want to write up anything so negative given their generosity of letting me try it out for free.

seancorfield21:11:18

I used to quite like Eclipse, so I used Counter ClockWise for a while for my Clojure dev but that was just too slow on the Linux Netbook I was using for a year or so when traveling (and it was sluggish on Windows too).

seancorfield21:11:44

Since I don’t do any Java, I don’t need an IDE anyway.