{"action":"created","issue":{"url":"https://api.github.com/repos/manastech/crystal/issues/1116","labels_url":"https://api.github.com/repos/manastech/crystal/issues/1116/labels{/name}","comments_url":"https://api.github.com/repos/manastech/crystal/issues/1116/comments","events_url":"https://api.github.com/repos/manastech/crystal/issues/1116/events","html_url":"https://github.com/manastech/crystal/pull/1116","id":99506407,"number":1116,"title":"Add permutation method for array","user":{"login":"jalyna","id":5362483,"avatar_url":"https://avatars.githubusercontent.com/u/5362483?v=3","gravatar_id":"","url":"https://api.github.com/users/jalyna","html_url":"https://github.com/jalyna","followers_url":"https://api.github.com/users/jalyna/followers","following_url":"https://api.github.com/users/jalyna/following{/other_user}","gists_url":"https://api.github.com/users/jalyna/gists{/gist_id}","starred_url":"https://api.github.com/users/jalyna/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jalyna/subscriptions","organizations_url":"https://api.github.com/users/jalyna/orgs","repos_url":"https://api.github.com/users/jalyna/repos","events_url":"https://api.github.com/users/jalyna/events{/privacy}","received_events_url":"https://api.github.com/users/jalyna/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":10,"created_at":"2015-08-06T19:20:49Z","updated_at":"2015-08-08T22:18:52Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/manastech/crystal/pulls/1116","html_url":"https://github.com/manastech/crystal/pull/1116","diff_url":"https://github.com/manastech/crystal/pull/1116.diff","patch_url":"https://github.com/manastech/crystal/pull/1116.patch"},"body":"Adds `Array#permutation`"},"comment":{"url":"https://api.github.com/repos/manastech/crystal/issues/comments/129058019","html_url":"https://github.com/manastech/crystal/pull/1116#issuecomment-129058019","issue_url":"https://api.github.com/repos/manastech/crystal/issues/1116","id":129058019,"user":{"login":"kirbyfan64","id":1690697,"avatar_url":"https://avatars.githubusercontent.com/u/1690697?v=3","gravatar_id":"","url":"https://api.github.com/users/kirbyfan64","html_url":"https://github.com/kirbyfan64","followers_url":"https://api.github.com/users/kirbyfan64/followers","following_url":"https://api.github.com/users/kirbyfan64/following{/other_user}","gists_url":"https://api.github.com/users/kirbyfan64/gists{/gist_id}","starred_url":"https://api.github.com/users/kirbyfan64/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kirbyfan64/subscriptions","organizations_url":"https://api.github.com/users/kirbyfan64/orgs","repos_url":"https://api.github.com/users/kirbyfan64/repos","events_url":"https://api.github.com/users/kirbyfan64/events{/privacy}","received_events_url":"https://api.github.com/users/kirbyfan64/received_events","type":"User","site_admin":false},"created_at":"2015-08-08T22:18:52Z","updated_at":"2015-08-08T22:18:52Z","body":"Well, this is the PyPy algorithm, ported to Ruby:\r\n\r\n```ruby\r\nclass Array\r\n  def self.permutation2(array, r=nil)\r\n    r = array.length if r.nil?\r\n    n = array.length\r\n    if n-r < 0\r\n      # Do something here (an error?)\r\n      return\r\n    else\r\n      indices = (0...n).to_a\r\n      cycles = n.downto(n-r).to_a\r\n    end\r\n    nxt = false\r\n    while true\r\n      result = (0...r).map {|i| indices[i]}\r\n      i = r-1\r\n      while i>=0\r\n        j = cycles[i]-1\r\n        if j>0\r\n          cycles[i] = j\r\n          indices[i], indices[-j] = indices[-j], indices[i]\r\n          yield result\r\n          nxt = true\r\n          break\r\n        else\r\n          cycles[i] = indices.length-i\r\n          num = indices[i]\r\n          n1 = indices.length-1\r\n          (i...n1).each {|k| indices[k] = indices[k+1] }\r\n          indices[n1] = num\r\n          i -= 1\r\n        end\r\n      end\r\n      if nxt\r\n        nxt = false\r\n      else\r\n        yield result\r\n        return\r\n      end\r\n    end\r\n  end\r\nend\r\n\r\nArray.permutation2([1, 2, 3], 2) {|r| puts \"#{r}\" }\r\n```\r\n"}}