Skip to content

Instantly share code, notes, and snippets.

@mrkaspa
Created September 13, 2017 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkaspa/01e27baea774cacf0cf25e4a4eb41bcf to your computer and use it in GitHub Desktop.
Save mrkaspa/01e27baea774cacf0cf25e4a4eb41bcf to your computer and use it in GitHub Desktop.
Crystal module playing
module Ord(A)
abstract def cmp(a : A, b : A)
end
class IntOrd
include Ord(Int32)
def cmp(a, b)
a > b
end
end
class Sorter(A)
getter :order
def initialize(@order : Ord(A))
end
def sort(list : Array(A))
i = 0
j = list.size - 1
aux = nil
while i < j
if order.cmp(list[i], list[j])
aux = list[i]
list[i] = list[j]
list[j] = aux
end
i += 1
j -= 1
end
list
end
end
ls = [5, 5, 2]
puts Sorter(Int32).new(IntOrd.new).sort(ls)
puts ls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment