jdantonio / functional-ruby

A gem for adding functional programming tools to Ruby. Inspired by Erlang, Clojure, Haskell, and Functional Java.

Home Page:http://www.functional-ruby.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

re-implementing ascending? and friends

404pnf opened this issue · comments

commented
def in_order?(compare_fn)
  -> col, &blk do
    if blk
      col.map {|e| blk[e] }
         .each_cons(2)
         .all? { |e1, e2| e1.send(compare_fn, e2) }
    else
      col.each_cons(2).all? { |e1, e2| e1.send(compare_fn, e2) }
    end
  end
end
# ==> nil

def ascending?(col, &blk)
  in_order?(:<)[col, &blk]
end
# ==> nil

def descending?(col, &blk)
  in_order?(:>)[col, &blk]
end
# ==> nil

def non_ascending?(col, &blk)
  in_order?(:>=)[col, &blk]
end
# ==> nil

def non_descending?(col, &blk)
  in_order?(:<=)[col, &blk]
end
# ==> nil

ascending? [1,2,2,3]
# ==> false
## simple tests
ascending? ["z", "mn", "abc"]
# ==> false
ascending? ["z", "mn", "abc"], &:length
# ==> true
non_descending? [1,2,2,3]
# ==> true
descending? [4, 3, 2, 1]
# ==> true
descending? [4, 3, 3, 2]
# ==> false
non_ascending? [4, 3, 3, 2]
# ==> true
non_ascending? [4, 3, 3, 2]
# ==> true

If you like it. I will prepare a pull request.

Thank you for the feedback. Please create the PR and I'll happily merge it. I've been spending most of my free time working on my concurrent-ruby gem but I would like to get back to work on this gem, too. There are a couple of updates I'd like to make. We can figure out the next release date together.