{"action":"created","issue":{"url":"https://api.github.com/repos/hylang/hy/issues/195","labels_url":"https://api.github.com/repos/hylang/hy/issues/195/labels{/name}","comments_url":"https://api.github.com/repos/hylang/hy/issues/195/comments","events_url":"https://api.github.com/repos/hylang/hy/issues/195/events","html_url":"https://github.com/hylang/hy/issues/195","id":15882765,"number":195,"title":"Unable to translate Python code into Hy.","user":{"login":"joehakimrahme","id":300964,"avatar_url":"https://avatars.githubusercontent.com/u/300964?v=3","gravatar_id":"","url":"https://api.github.com/users/joehakimrahme","html_url":"https://github.com/joehakimrahme","followers_url":"https://api.github.com/users/joehakimrahme/followers","following_url":"https://api.github.com/users/joehakimrahme/following{/other_user}","gists_url":"https://api.github.com/users/joehakimrahme/gists{/gist_id}","starred_url":"https://api.github.com/users/joehakimrahme/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joehakimrahme/subscriptions","organizations_url":"https://api.github.com/users/joehakimrahme/orgs","repos_url":"https://api.github.com/users/joehakimrahme/repos","events_url":"https://api.github.com/users/joehakimrahme/events{/privacy}","received_events_url":"https://api.github.com/users/joehakimrahme/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/hylang/hy/labels/bug","name":"bug","color":"e10c02"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2013-06-22T10:21:37Z","updated_at":"2015-08-10T09:54:08Z","closed_at":"2015-08-10T09:54:08Z","body":"I'm trying to translate this Python code into Hy. `memoized` is a decorator which applies a [simple optimization technique](https://en.wikipedia.org/wiki/Memoization).\r\n\r\n``` python\r\ndef memoized(fn):\r\n    fn.cache_dict = {}\r\n\r\n    def memoized_fn(n):\r\n\r\n        if n in fn.cache_dict:\r\n            return fn.cache_dict[n]\r\n        else:\r\n            fn.cache_dict[n] = fn(n)\r\n            return fn.cache_dict[n]\r\n\r\n    return memoized_fn\r\n\r\n```\r\n\r\nTo show that this code works, I run a small test on a recursive Fibonacci sequence calculator:\r\n\r\n``` python\r\n@memoized\r\ndef fibo_recursive(n):\r\n    if n <= 1:\r\n        return n\r\n    return fibo_recursive(n-1) + fibo_recursive(n-2)\r\n\r\n\r\n>>> fibo_recursive(100)\r\n354224848179261915075L\r\n```\r\nThe result is instantaneous. The non-decorated version of this would've otherwise not been able to compute this value. Success!\r\n\r\nNow I want to translate this to Hy code. Here's my attempt:\r\n\r\n``` clj\r\n(defn memoized [fn]\r\n  (setv fn.cache_dict {})\r\n  \r\n  (defn memoized_fn [n] ;; TODO: replace it with *args\r\n    (if (.has_key fn.cache_dict n)\r\n      (.get fn.cache_dict n)\r\n      (do\r\n       (.setdefault fn.cache_dict n (fn n)))))\r\n  memoized_fn)\r\n```\r\n\r\nI don't know if this is the correct way to access dictionary elements, but it is beyond the point. When I try to execute the Hy code above, I get a very cryptic error message, that I pasted on a pastebin because it wouldn't fit on a Github issue.\r\n\r\n* [Find the traceback here](http://www.hastebin.com/gevunowada.vhdl)\r\n\r\nI'm not sure if there's an error in my Hy code or it's actually a compiler error like the traceback seems to indicate, but in any case, I thought I'd report it. I am trying to pin-point more precisely where the bug might come from, but I don't know the source code well enough and I'm afraid I may be in a little bit over my head.\r\n\r\nIn the case that this is not a compiler bug, but an error in my code, can someone explain to me where the problem is, and maybe we can consider a more expressive error message?"},"comment":{"url":"https://api.github.com/repos/hylang/hy/issues/comments/129388509","html_url":"https://github.com/hylang/hy/issues/195#issuecomment-129388509","issue_url":"https://api.github.com/repos/hylang/hy/issues/195","id":129388509,"user":{"login":"algernon","id":17243,"avatar_url":"https://avatars.githubusercontent.com/u/17243?v=3","gravatar_id":"","url":"https://api.github.com/users/algernon","html_url":"https://github.com/algernon","followers_url":"https://api.github.com/users/algernon/followers","following_url":"https://api.github.com/users/algernon/following{/other_user}","gists_url":"https://api.github.com/users/algernon/gists{/gist_id}","starred_url":"https://api.github.com/users/algernon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/algernon/subscriptions","organizations_url":"https://api.github.com/users/algernon/orgs","repos_url":"https://api.github.com/users/algernon/repos","events_url":"https://api.github.com/users/algernon/events{/privacy}","received_events_url":"https://api.github.com/users/algernon/received_events","type":"User","site_admin":false},"created_at":"2015-08-10T09:54:08Z","updated_at":"2015-08-10T09:54:08Z","body":"The current error is much better:\r\n\r\n```\r\nHyTypeError: First argument to (fn) must be a list\r\n```\r\n\r\nTherefore, since olasd's suggestion also solves the problem, I'm closing this issue."}}