hariszaman
hariszaman

Reputation: 8424

How to check if a sequence is increasing with +1 increment clojure?

I am making poker game in clojure I have a function like:

(sort (map rank straight-hand))

that returns rank of the hand in sort order let say

(2 3 4 5 6)

how to check that the difference of preceding number and current number in the sequence is 1

Upvotes: 0

Views: 692

Answers (3)

Diego Basch
Diego Basch

Reputation: 13069

Here's another way: you want to check that what you have is a range. So compare it to the range between the first and the last element of your list.

  (= xs (range (first xs)
           (inc (last xs))))

Upvotes: 3

noisesmith
noisesmith

Reputation: 20194

(every? #{1} (map - (rest hand) hand))

This verifies that every result of subtracting an item from the following in your hand is in the set #{1}

Upvotes: 8

YosemiteMark
YosemiteMark

Reputation: 675

Use (partition 2 1 <seq>) to break your sorted sequence into overlapping groups of two, then get the difference of each group of two, then test that each difference is -1:

(defn incrementing?
  [xs]
  (every? #(= % -1) (map #(apply - %) (partition 2 1 xs))))

Upvotes: 3

Related Questions