Fork me on GitHub
#beginners
<
2015-12-30
>
frankk3850614:12:02

@room With clojure, how can I grab the amounts in this lazy sequence? (#USD 19.95 #USD 36.37)

agile_geek15:12:34

@frankk38506: Depends on how you want to identify the amounts? For example, assuming that seq is bound to currencies in the code below - by type:

(filter number? currencies)

agile_geek15:12:00

or by position

(map second (partition 2 currencies))

frankk3850615:12:38

(filter number? (#USD 19.95 #USD 36.37)) - so something like this?

frankk3850615:12:43

I’d like a vector or list returned containing only (19.95 36.37)

agile_geek15:12:34

Yes. Except I'm confused by your sequence - what are #USD is this a symbol? In which case it would be 'USD. Also if the sequence is a list in needs quoting in literal form e.g. '(1 2 3 4)

agile_geek15:12:32

Either of my examples would return a lazy sequence of just the amounts. The filter does it by returning only numbers the second example does it by splitting the seq into a sequence of pairs and then takes the second item in each pair.

frankk3850615:12:20

i’m not sure what #USD is, i’ll find out from another dev on my team, i’ll give your suggestions a try, thank you @agile_geek

solicode16:12:16

@frankk38506 @agile_geek #USD is likely a tagged literal: http://clojure.org/reader#The%20Reader--Tagged%20Literals

solicode16:12:48

I don't know what your #USD implementation looks like though. I'm guessing it either creates a currency object of some kind, or it's just converting the number to a non-floating point representation or something.

agile_geek17:12:43

@solicode so in the sequence the #USD is just the tag, in which case you need to take every element?

solicode17:12:20

@agile_geek: Yeah, it's just a tag. But I don't know what type it returns. Is it a Java object of some kind? Do you have to call .getValue or something on it? I can't know without knowing what #USD does.

solicode17:12:30

It would be easy for @frankk38506 to check though. For example, what does (type #USD 19.95) return?

frankk3850617:12:11

Thank you @solicode. I’ll see what i can find out

solicode17:12:53

@frankk38506: Oh, sorry, I was assuming you had quick access to the REPL right now. Maybe not so "easy" then... 😛

frankk3850617:12:40

In REPL, i get something like this - RuntimeException No reader function for tag USD clojure.lang.LispReader$CtorReader.readTagged (LispReader.java:1245)

solicode17:12:12

Hmm, that's strange. Is that a plain REPL, or your project REPL with everything loaded already (like dependencies and so on)?

solicode17:12:53

Or the other possibility is that these tagged literals are in EDN? So you wouldn't have access to it in the REPL like that.

frankk3850617:12:41

just a plain repl

solicode17:12:17

Ah, okay, that won't work because #USD isn't a standard Clojure thing. It's defined somewhere in your project.

solicode17:12:12

I'm guessing there's a data_readers.clj file somewhere in the project.