{"action":"created","issue":{"url":"https://api.github.com/repos/manastech/crystal/issues/1859","labels_url":"https://api.github.com/repos/manastech/crystal/issues/1859/labels{/name}","comments_url":"https://api.github.com/repos/manastech/crystal/issues/1859/comments","events_url":"https://api.github.com/repos/manastech/crystal/issues/1859/events","html_url":"https://github.com/manastech/crystal/pull/1859","id":114588453,"number":1859,"title":"Add Array#bsearch, Array#bsearch_index, Range#bsearch","user":{"login":"cielavenir","id":1495368,"avatar_url":"https://avatars.githubusercontent.com/u/1495368?v=3","gravatar_id":"","url":"https://api.github.com/users/cielavenir","html_url":"https://github.com/cielavenir","followers_url":"https://api.github.com/users/cielavenir/followers","following_url":"https://api.github.com/users/cielavenir/following{/other_user}","gists_url":"https://api.github.com/users/cielavenir/gists{/gist_id}","starred_url":"https://api.github.com/users/cielavenir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cielavenir/subscriptions","organizations_url":"https://api.github.com/users/cielavenir/orgs","repos_url":"https://api.github.com/users/cielavenir/repos","events_url":"https://api.github.com/users/cielavenir/events{/privacy}","received_events_url":"https://api.github.com/users/cielavenir/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-11-02T12:53:46Z","updated_at":"2015-11-02T17:18:45Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/manastech/crystal/pulls/1859","html_url":"https://github.com/manastech/crystal/pull/1859","diff_url":"https://github.com/manastech/crystal/pull/1859.diff","patch_url":"https://github.com/manastech/crystal/pull/1859.patch"},"body":"Currently Crystal lacks binary search, which is a great feature of Ruby 2.0+.\r\n\r\nI have (partially) ported https://github.com/marcandre/backports/blob/master/lib/backports/2.0.0/range/bsearch.rb .\r\nThe only point is that current port lacks Float support, which requires reinterpreting Float64 as Int64.\r\n\r\nAnd, `->{ (pointerof(midpoint) as Pointer(Int64)).value }.call` caused\r\n```\r\nError: you've found a bug in the Crystal compiler. Please open an issue, including source code that will allow us to reproduce the bug: https://github.com/manastech/crystal/issues\r\n```\r\n\r\nAnyway, the Int version works flawlessly. How do you think of this pull request?\r\nCurrently it is also available as https://github.com/cielavenir/crystal-bsearch shard and the spec will be ported after approval (I'll need to fix the indent).\r\n"},"comment":{"url":"https://api.github.com/repos/manastech/crystal/issues/comments/153088793","html_url":"https://github.com/manastech/crystal/pull/1859#issuecomment-153088793","issue_url":"https://api.github.com/repos/manastech/crystal/issues/1859","id":153088793,"user":{"login":"cielavenir","id":1495368,"avatar_url":"https://avatars.githubusercontent.com/u/1495368?v=3","gravatar_id":"","url":"https://api.github.com/users/cielavenir","html_url":"https://github.com/cielavenir","followers_url":"https://api.github.com/users/cielavenir/followers","following_url":"https://api.github.com/users/cielavenir/following{/other_user}","gists_url":"https://api.github.com/users/cielavenir/gists{/gist_id}","starred_url":"https://api.github.com/users/cielavenir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cielavenir/subscriptions","organizations_url":"https://api.github.com/users/cielavenir/orgs","repos_url":"https://api.github.com/users/cielavenir/repos","events_url":"https://api.github.com/users/cielavenir/events{/privacy}","received_events_url":"https://api.github.com/users/cielavenir/received_events","type":"User","site_admin":false},"created_at":"2015-11-02T17:18:45Z","updated_at":"2015-11-02T17:18:45Z","body":"For Float binary-search, I wrote pseudo code. Although it doesn't compile (yet), you might know what I mean by using block.\r\n\r\n```\r\n    midpoint = 0 # placeholder\r\n    if from.is_a?(Int) && to.is_a?(Int)\r\n      convert = -> { midpoint }\r\n    else\r\n      map = ->(T,v) {\r\n        result = (pointerof(v.abs) as Pointer(T)).value\r\n        v<0 ? -result : result\r\n      }\r\n      from = map.call(UInt64,from.to_f)\r\n      to = map.call(UInt64,to.to_f)\r\n      convert = -> { map.call(Float64,midpoint) }\r\n    end\r\n```"}}