What is the pos method in Clojure number?

What is the pos method?

The pos method is a number test method, and it is used to test if a number is greater than zero.

Syntax

(pos? number)
Syntax for pos method

Parameter

The pos method accepts just one parameter. This is the number it will test to see if it is greater than zero.

Return value

The pos method returns true if the number is greater than 0 and false if the number is less than or equal to 0.

Usage

There are times when we want to carry out a calculation and don't want our calculation to include zeros. In such situations, we use the pos method to test the number. Let's see an example to understand this better.

Code

(ns clojure.examples.hello
(:gen-class))
(defn poss []
(def x (pos? 0))
(println x)
(def x (pos? -1))
(println x)
(def x (pos? 9))
(println x))
(poss)

Explanation

  • Line 5: We define a function poss.
  • Line 6: We pass 0 into the pos method.
  • Line 7: We print the output and notice that the output we get is a false, because of the 0.
  • Line 9: We pass -1 into the pos method.
  • Line 10: We print the output and notice that the output we get is a false because -1 is less than 0.
  • Line 12: We pass 9 into the pos method.
  • Line 13: We print the output and notice that the output we get is a true because 9 is greater than 0.
  • Line 14: We call our poss function to execute the code.

Free Resources