Fork me on GitHub
#beginners
<
2016-11-24
>
roelofw06:11:38

oke, one problem I do not see how to solve it

roelofw06:11:07

Let's say we have the seq `[ 1 [ 2 3] 4] `

roelofw06:11:24

We first have a 1 and that one can be conj with a acc

roelofw06:11:59

Then we have a seq [ 2 3 ] . We have to iterate trougth that one.

roelofw06:11:54

But how di I "save" the contents of the 4 . Do I need to use 3 variables acc , rest seq and the rest of the whole seq ?

seancorfield07:11:33

@roelofw Consider instead that you could `concat` sequences together -- so if you have a non-sequence element, you can turn it into a sequence by wrapping it in `[ ]`, and if you do have a sequence you can recursively flatten it and then `concat` it (since it may contain nested sequences).

seancorfield07:11:17

Is this a 4clojure puzzle? Which one? (so I know exactly what problem you're trying to solve)

roelofw08:11:27

@seancorfield This is a 4clojure puzzle and here is the link : http://www.4clojure.com/problem/28

roelofw08:11:01

As described im not allowed to use flatten, otherwise the puzzle would be very simple

seancorfield08:11:32

Since you need to handle deeply nested sequences, you'll need to call your "flatten" function recursively on any vectors or lists but you can do this with mapcat and a function that does what I described above.

sb08:11:46

Hello, Could you help me - give an example/ tutorial - what is possible with clojure in the NLP area?

sb08:11:05

(eg. generate english communication from AI)

roelofw08:11:54

@seancorfield you mean the part , if it's not a seq turn it into a seq otherwise take all the elements out of it ?

seancorfield08:11:39

@sb: Google suggests this might be a good starting point for you https://github.com/dakrone/clojure-opennlp/blob/master/README.markdown

sb08:11:14

Yes, I see that. and the stanford nlp too

sb08:11:14

For me mainly the question, experience with that

sb08:11:22

what do you think about, what is possible

seancorfield08:11:27

@roelofw: if coll? x then recursively call your flatten on it else return [x] -- then your flatten function would just mapcat that logic over the collection. Make sense?

roelofw08:11:43

oke, that way

roelofw08:11:59

you almost give the code away

seancorfield08:11:22

@sb: I think your question is too vague. You can do anything with Clojure that you can do with Java libraries, just easier.

seancorfield08:11:55

@roelofw: I avoided parentheses so it's not code ðŸ˜†

roelofw08:11:12

@seancorfield time to experiment with the code. But I was on the good way when I thought about mapcat

seancorfield08:11:26

:thumbsup::skin-tone-2:

sb08:11:36

@seancorfield Yes, I know. I saw a few really good example with java libraries (like stanford-nlp). I try a few.. and I will see. By the way thanks!

roelofw10:11:00

bummer, I see a object here :

``````(defn my-flatten [s] (mapcat (fn [_ n ]  (if (coll? n) (my-flatten n ) ([n])) s )))

(my-flatten [1 [2 3 ] 4]) ``````

roelofw11:11:10

anyone a tip where I go wrong ?

rauh11:11:16

@roelofw Indent your code properly and you'll probably see 1-2 mistakes

roelofw11:11:43

@rauh : You mean something like this :

``````(defn my-flatten [s]
(map (fn [_ n ]
(if (coll? n)
(my-flatten n )
([n]))
s )))
``````

genmeblog11:11:18

yes, line 5 as my first hint

roelofw11:11:35

you mean [x] where I want to convert a number to a vector ?

roelofw11:11:27

hmm, for me that is line 5

genmeblog11:11:42

there are more problems in the code

roelofw11:11:31

oke, what I try to do is take a item of a seq with map.

roelofw11:11:59

When that item n is not a seq then make it a seq

roelofw11:11:33

otherwise call the function again so it get flattened with mapcat

roelofw11:11:38

that was my idea

roelofw11:11:04

As a challenge I have to flatten a deep nested seq without using flatten

genmeblog11:11:08

I know, I see

genmeblog11:11:44

but first correct some mistakes

genmeblog11:11:11

first hint: there is a difference between a and (a)

roelofw12:11:14

you still refering to the ([n] ) part ?

roelofw12:11:31

Im still confused what you exactly wants

genmeblog12:11:47

I'm talking about difference between ([n]) vs [n]

roelofw12:11:04

So it can be `[n]` instead of `([n]) ` ?

genmeblog12:11:35

yes, question is: do you know why?

genmeblog12:11:42

because it's crucial

genmeblog12:11:41

your first solution (with mapcat) works after you correct 3 errors

genmeblog12:11:44

this is first one

roelofw12:11:54

I think ([x]) is a seq containing a vector

roelofw12:11:07

[x] is a vector with one element

genmeblog12:11:27

try to make in repl

genmeblog12:11:07

and see what it does or what errors you get

roelofw12:11:54

with the second you get this error : ` ArityException Wrong number of args (0) passed to: PersistentVector clojure.lang.AFn.throwArity (AFn.java:429)`

roelofw12:11:26

first one works fine and also the thirth one

genmeblog12:11:29

yes, arity exceptions you get when you try to call function with wrong number of arguments

roelofw12:11:04

and the last one works also , gives as output 1

roelofw12:11:23

I know that after a ( clojure expects a function

genmeblog12:11:30

yes, exactly!

genmeblog12:11:47

and that's why ([x]) doesn't produce sequence

roelofw12:11:49

except when doing a quote , then you tell clojure to handle it as it is

genmeblog12:11:24

not exactly, '(a b c) produces list

roelofw12:11:40

I think I need to use `(seg [x] ) `

genmeblog12:11:41

(type '(1 2 ));; => clojure.lang.PersistentList

genmeblog12:11:18

why? [x] is already a sequence

roelofw12:11:03

so one error is solved ðŸ™‚

genmeblog12:11:27

ooops, no it isn't, sorry

genmeblog12:11:39

but it doesn't matter

genmeblog12:11:46

mapcat works on vectors too

roelofw12:11:06

oke, time for error 2

roelofw12:11:40

any tip where I can find it

roelofw12:11:58

You said there are still 2 errors but I cannot see it

genmeblog12:11:26

yes, first one is parenthesis

genmeblog12:11:49

I meant second one

rauh12:11:49

@roelofw you never indented your code properly. Use Cursive's help if you can. It'll indent itproperly

genmeblog12:11:19

third one you get when you call after fixing two first bugs

roelofw12:11:00

@rauh : I use the intendation og cursive all the time ðŸ˜ž

genmeblog12:11:20

ok, then see how many arguments you have for each function and check closing paranthesis

genmeblog12:11:43

*parantheses

roelofw12:11:54

flatten has one. only the given s

roelofw12:11:27

map has only 1 , the current item named n

roelofw12:11:13

the if then works also on n

roelofw12:11:02

aha. the s is on the wrong place. its looks now if it's a part of the anymous function

genmeblog12:11:18

map needs two parameters, function and collection

roelofw12:11:01

oke, it has now . the anymous function and s

genmeblog12:11:07

if needs three parameters: predicate, value for true and value for false

genmeblog12:11:14

`if`

roelofw12:11:22

oke, I have now this :

``````(defn my-flatten [s]
(map (fn [_ n ]
(if (coll? n)
(my-flatten n )
[n]))
s )) ``````

roelofw12:11:43

and I now see a arity error on my-flatten

genmeblog12:11:59

actually not in my-flatten

roelofw12:11:27

I see this error : `ArityException Wrong number of args (1) passed to: core/my-flatten/fn--1463 clojure.lang.AFn.throwArity (AFn.java:429) `

genmeblog12:11:41

`fn--1463 ` is a key

roelofw12:11:53

Which is wierd, my flatten has and must have 1 argument

genmeblog12:11:33

`fn--1463` ss internal anonymous function, and error says that it's called with one (which is wrong) argument

roelofw12:11:14

my-flatten has 1 argument on first calling nl. s

genmeblog12:11:25

roelofw12:11:43

map has also 1 argument the current item

genmeblog12:11:46

it's about `my-flatten/fn--1463`

roelofw12:11:10

oke, but i cannot see what that function is

genmeblog12:11:30

you have only one anonymous function inside your my-flatten

genmeblog12:11:36

so this is this one

roelofw12:11:59

That one has only 1 argument. namely n

genmeblog12:11:16

you're wrong

roelofw12:11:24

the other is _ . so it is not important

genmeblog12:11:49

but it is treated as argument

roelofw12:11:18

oke, so at first it has 2 arguments , one who is not important and the n \

roelofw12:11:47

I check the if then with n , which looks right to me

genmeblog12:11:58

"In Clojure, the underscore is used idiomatically to indicate that the argument it identifies is not subsequently used."

genmeblog12:11:30

map expects a function which has exactly one argument

genmeblog12:11:38

you've provided function with two

roelofw12:11:48

then if n is a seq , I called my_flatten again with n otherwise I put n in a vector

roelofw12:11:13

oke, then I confused map with reduce

genmeblog12:11:47

underscore `_` is valid argument name

roelofw12:11:55

oke, now n is not known

genmeblog12:11:56

``(defn underscore [_] _)``
works well

roelofw12:11:14

I have to find another way to use the current item ðŸ˜ž

genmeblog12:11:27

``(underscore "aaa") ;; => "aaa"``

genmeblog12:11:39

show me what you have

roelofw12:11:35

got it working, Some code were overwritten

roelofw12:11:46

This one :

``````(defn my-flatten [s]
(mapcat  (fn [ n ]
(if (coll? n)
(my-flatten n )
[n]))
s ))

(my-flatten [1 [2 3 ] 4])
``````

roelofw12:11:56

works with my own example

genmeblog12:11:11

with mine as well

roelofw12:11:11

Now I hopes it works also with the examples of 4 clojure

genmeblog12:11:41

``````(my-flatten [1 [22 [:e] '(1 2 3 [33 33 {:e a}]) 33] [33 33]])
;; => (1 22 :e 1 2 3 33 33 :e a 33 33 33)``````

roelofw12:11:43

@tsulej thanks for the help and patience with me

genmeblog12:11:59

you're welcome ðŸ™‚

genmeblog12:11:01

one more hint

roelofw12:11:37

always appericaited

roelofw12:11:49

and time for a late lunch

genmeblog12:11:53

``````(defn my-flatten [xs]
(mapcat
#(if (coll? %)
(my-flatten %)
[%])
xs))``````

genmeblog12:11:18

roelofw12:11:22

aha, instead of fn use the # syntax

genmeblog12:11:26

just a syntactic sugar

roelofw12:11:44

I have to dig into the # , Im still used to use fn

roelofw13:11:10

tsulej did you ever work with haskell. I know they use xs for a list/vector

genmeblog13:11:13

no, I was trying to learn it once but I gave up

genmeblog13:11:34

this is in use in Scala too (xs is plural for 'x')

roelofw13:11:47

oke, thanks

mikecarter13:11:39

i'm trying to copy a file to a resource path that's different in development than it is to in production, any ideas?

mikecarter13:11:56

is there a way to get the current resource path?

mikecarter13:11:32

usually `io/resource` takes care of it, but it doesn't work for a file that doesn't exist yet

dpsutton13:11:41

This has bitten me before. It seems like the `resource` directory is not present in production

dpsutton13:11:50

dpsutton13:11:55

I think this is a jvm question

mikecarter13:11:02

it is present, but it's within the jar

mikecarter13:11:18

so i have a `resources` directory alongside the jar in production

mikecarter13:11:24

but in development, it's called `dev-resources`

mikecarter13:11:26

any ideas would be great!

mikecarter14:11:03

@dpsutton I think i've got it

mikecarter14:11:29

instead of calling `io/resource` on the none-existent file, i'm doing it on the target directory then amending the new filename

mikecarter14:11:37

let me know if you want to discuss it further!

roelofw16:11:46

Can I somehow flatten a list 1 level. so (( [ 1,2] )) will be ( [ 1,2] ) ?

roelofw16:11:03

I cannot find the right method

kauko16:11:53

apply concat

agile_geek16:11:43

@roelofw of course if you know that you're upper level list will only have one item (or at least you're only interested in the first) you can simply use `(first xs)` where `xs` is your list

roelofw17:11:11

I know. I had to repeat a item several times in a seq so `(1 2 ) 3` would be `( 1 1 1 2 2 2 3 3 3 ) `