Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the class of something in clojure?

Tags:

I'm learning clojure and have a very basic question: given that clojure has type inference, how can you tell what class was inferred?

For instance, these would each result in different data types:

(2) (/ 2 3) (/ 2.0 3) 

Is there some kind of class function that will return the data type? Also, is there a normal way of casting something to be a specific type? So in the second example above, what would I do if I wanted the result to be float?

like image 817
Shane Avatar asked Jan 06 '10 11:01

Shane


People also ask

What is Defrecord Clojure?

Clojure allows you to create records, which are custom, maplike data types. They're maplike in that they associate keys with values, you can look up their values the same way you can with maps, and they're immutable like maps.

What is Type Clojure?

Clojure is written in terms of abstractions. There are abstractions for sequences, collections, callability, etc. In addition, Clojure supplies many implementations of these abstractions. The abstractions are specified by host interfaces, and the implementations by host classes.

How do you define a list in Clojure?

List is a structure used to store a collection of data items. In Clojure, the List implements the ISeq interface. Lists are created in Clojure by using the list function.

Is Vector a Clojure?

Clojure collections "collect" values into compound values. There are four key Clojure collection types: vectors, lists, sets, and maps.


1 Answers

There is a type function in the clojure.core library.

user> (type 2) java.lang.Integer  user> (type (/ 2 3)) clojure.lang.Ratio  user> (type (/ 2.0 3)) java.lang.Double 

If you want to convert a given number into a float then use float.

user> (float 10) 10.0 
like image 89
aatifh Avatar answered Oct 26 '22 11:10

aatifh