How to create a clojure arrayList

Asked

Viewed 46 times

0

How to create a clojure List array and add elements? (the one in java)

My problem is this: I need to read a file and go adding substrings of that file (bounded by space and line break " n"), as separate elements in that arrayList.

I can read, delimit spaces, but I can’t import how to declare java arrayList and I don’t know how to do it.

2 answers

0

To create a clojure arrayList, do as below:

user=> (def newlist '(1 2 3))

remembering that, newlist is the name I set for the list, it is necessary to use ' at first, or exchange ' for (list 1 2 3)

To add elements to the list use:

user=> (conj newlist 4)

In the above example, I added element 4 to newlist, in return:

(4 1 2 3)

The function conj add the element at the beginning of the list, it is the same thing as using cons, but is more efficient.

source: https://learnxinyminutes.com/docs/pt-br/clojure-pt/ edited by the author.

0

You can use the split-Lines to do everything at once:

(:require string :as string]))
  (string/split-lines "1\n2\n3") ; resultado: ["1", "2", "3"]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.