{"action":"created","comment":{"url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/comments/67092623","id":67092623,"diff_hunk":"@@ -0,0 +1,111 @@\n+module FunctionalNullableOperations\n+\n+# extends Base with operations that treat nullables as collections\n+\n+importall Base\n+import Base: promote_op, LinearFast\n+\n+# conceptually, nullable types can be considered infinite-order tensor powers of\n+# finite-dimensional vector spaces — we attempt to support most operations on\n+# arrays except the linear algebra related ones; the infinite-dimensional nature\n+# makes subtyping AbstractArray a little dangerous, which explains why\n+# functionality is reimplemented instead of subtyping AbstractArray\n+\n+# size   – not implemented since an infinite-dimensional tuple would be strange\n+# length – one or zero\n+# endof  – same as length\n+length(u::Nullable) = u.isnull ? 0 : 1\n+endof(u::Nullable)  = length(u)\n+\n+# indexing is either without index, or with 1 as index\n+# generalized linear indexing is not supported\n+# setindex! not supported because Nullable is immutable\n+linearindexing{T}(::Nullable{T}) = LinearFast()\n+function getindex(u::Nullable)\n+    @boundscheck u.isnull && throw(NullException())\n+    u.value\n+end\n+function getindex(u::Nullable, i::Integer)\n+    @boundscheck u.isnull | (i ≠ one(i)) && throw(BoundsError(i, u))\n+    u.value\n+end\n+\n+# iteration protocol\n+start(u::Nullable) = 1\n+next(u::Nullable, i::Integer) = u.value, 0\n+done(u::Nullable, i::Integer) = u.isnull || i == 0\n+\n+# next we have reimplementations of some higher-order functions\n+filter{T}(p, u::Nullable{T}) = u.isnull ? u : p(u.value) ? u : Nullable{T}()\n+\n+# warning: not type-stable\n+map{T}(f, u::Nullable{T}) = u.isnull ? Nullable{Union{}}() : Nullable(f(u.value))\n+\n+# multi-argument map doesn't broadcast, so not very useful, but no harm having","path":"src/functional.jl","position":44,"original_position":44,"commit_id":"d69b31dc1e7ee2ffc89e4f5ecf516d21bd4163ef","original_commit_id":"d69b31dc1e7ee2ffc89e4f5ecf516d21bd4163ef","user":{"login":"TotalVerb","id":12903942,"avatar_url":"https://avatars.githubusercontent.com/u/12903942?v=3","gravatar_id":"","url":"https://api.github.com/users/TotalVerb","html_url":"https://github.com/TotalVerb","followers_url":"https://api.github.com/users/TotalVerb/followers","following_url":"https://api.github.com/users/TotalVerb/following{/other_user}","gists_url":"https://api.github.com/users/TotalVerb/gists{/gist_id}","starred_url":"https://api.github.com/users/TotalVerb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TotalVerb/subscriptions","organizations_url":"https://api.github.com/users/TotalVerb/orgs","repos_url":"https://api.github.com/users/TotalVerb/repos","events_url":"https://api.github.com/users/TotalVerb/events{/privacy}","received_events_url":"https://api.github.com/users/TotalVerb/received_events","type":"User","site_admin":false},"body":"left out the comments","created_at":"2016-06-15T02:54:55Z","updated_at":"2016-06-15T02:54:55Z","html_url":"https://github.com/JuliaStats/NullableArrays.jl/pull/115#discussion_r67092623","pull_request_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/115","_links":{"self":{"href":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/comments/67092623"},"html":{"href":"https://github.com/JuliaStats/NullableArrays.jl/pull/115#discussion_r67092623"},"pull_request":{"href":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/115"}}},"pull_request":{"url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/115","id":73509997,"html_url":"https://github.com/JuliaStats/NullableArrays.jl/pull/115","diff_url":"https://github.com/JuliaStats/NullableArrays.jl/pull/115.diff","patch_url":"https://github.com/JuliaStats/NullableArrays.jl/pull/115.patch","issue_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/issues/115","number":115,"state":"open","locked":false,"title":"[WIP/RFC] functional operations on nullables as collections","user":{"login":"TotalVerb","id":12903942,"avatar_url":"https://avatars.githubusercontent.com/u/12903942?v=3","gravatar_id":"","url":"https://api.github.com/users/TotalVerb","html_url":"https://github.com/TotalVerb","followers_url":"https://api.github.com/users/TotalVerb/followers","following_url":"https://api.github.com/users/TotalVerb/following{/other_user}","gists_url":"https://api.github.com/users/TotalVerb/gists{/gist_id}","starred_url":"https://api.github.com/users/TotalVerb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TotalVerb/subscriptions","organizations_url":"https://api.github.com/users/TotalVerb/orgs","repos_url":"https://api.github.com/users/TotalVerb/repos","events_url":"https://api.github.com/users/TotalVerb/events{/privacy}","received_events_url":"https://api.github.com/users/TotalVerb/received_events","type":"User","site_admin":false},"body":"This implements Scala-style `map` and `filter`. I did not implement Scala's fold semantics, because I consider that to be a mistake, since it is inconsistent with the regular `foldr` and `foldl`, which I did keep. Since `map` is not type-stable, I also implemented `broadcast` as a type-stable `map`. `broadcast` also preserves the broadcasting capability of the version for arrays.\r\n\r\nI also implemented x[] and x[1] to replace `get(x)`. Finally, I added in the iteration interface, which allows imperative-style for loops as well as functional-style higher order functions.\r\n\r\nJohn presented several options for propagation in this [old mailing list post](https://groups.google.com/forum/#!msg/julia-dev/WD7-vQeweJE/yGBFU2E2b2cJ), which I quote:\r\n\r\n```\r\nAs I see it, progagation can work in a couple of ways:\r\n\r\n(1) Always off: you can't propagate nullability without explicitly constructing a Nullable object.\r\n(2) Opt-in at the call-site: you can propagate nullability by using something like map(f, Nullable).\r\n(3) Opt-in near the call-site: you can propagate nullability within a block of code using something like @propagate begin x = Nullable(1) + Nullable(2); sin(x) end\r\n(4) Opt-in at the definition site: you can propagate nullability by annotating a function as propagating in the same way that we annotate functions as vectorizable.\r\n(5) Always on: you can't prevent propagation without explicitly checking for nullability before calling any function.\r\n```\r\n\r\nThe implementation of `broadcast` here doubles as an implementation of (2). This is simply a nice result of the algebraic properties of broadcasting. With new broadcast syntactic sugar, (2) is no longer very verbose. I think alternative syntaxes like ? are hence no longer required.\r\n\r\n---\r\n\r\nNotes: (optional, sorry for the long PR)\r\n\r\n - I avoided implementing the Haskell-style monadic functions, because I don't think Julia has any monads and so that would be a big departure. The Scala-style functions feel more typical in a Julia environment.\r\n - I haven't optimized for performance yet. Obviously some functions will need to be specialized. But since 0.5 allows specialization on functions, I am sure that this syntax can be made as fast as any manual lowering.\r\n - Over the course of this PR I discovered some further nasty things with nullable, such as how `get(Nullable(), 1)` doesn't work (it expects a `Union{}` second argument?!) and how equality comparison is a bit broken (try `Nullable() == Nullable()`). I am new to Nullables so please excuse if there is a reason for this behaviour, but I found it heavily unintuitive.\r\n - This is only turned on on v0.5- right now. It can probably be extended to 0.4 (I don't see why not) but performance will be terrible and the shorthand `.` broadcast syntax will not be available.\r\n - I would prefer to deprecate `get(u)` in favour of `u[]`. The former syntax seems strange to me, as all other uses of `get` in Julia include a default value. I don't know if this is a popular viewpoint; my background in nullables comes more from Lisp (where `(cons x nil)` can be used as `Nullable(x)`) and Scala than from numerical computing languages.\r\n - Mathematically, I'm treating nullables akin to infinite-dimensional square arrays here. (More precisely, a Nullable type is an union of infinite-order tensor power [defining \"infinite\" here as \"arbitrarily large and finite\", since actual infinite-order tensor powers probably behave more strangely] of finite-dimensional vector spaces, of course disallowing infinite objects. The reason for this long-winded analogy is that it's basically a `SquareArray{T, ∞}`, which provides a baseline for what should and shouldn't work.) This is perfectly consistent algebraically but the algebra is unimportant. It's only a guide for what kind of functionality should be implemented.\r\n - While the mathematics makes sense to me, I couldn't find any papers on treating Option values as infinite-order tensor powers of finite-dimensional vector spaces, and I wouldn't be surprised if this was not a description in use at all. So if this turns out to be absolutely incoherent, then let's abandon the analogy. I just thought that it is quite nice for explaining the parallel for `broadcast` and for indexing, as identical operations.","created_at":"2016-06-12T22:52:27Z","updated_at":"2016-06-15T02:54:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d435fa4195b996f98c572a003c291d81282f7348","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/115/commits","review_comments_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/115/comments","review_comment_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/comments{/number}","comments_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/issues/115/comments","statuses_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/statuses/d69b31dc1e7ee2ffc89e4f5ecf516d21bd4163ef","head":{"label":"TotalVerb:fw/nullableops","ref":"fw/nullableops","sha":"d69b31dc1e7ee2ffc89e4f5ecf516d21bd4163ef","user":{"login":"TotalVerb","id":12903942,"avatar_url":"https://avatars.githubusercontent.com/u/12903942?v=3","gravatar_id":"","url":"https://api.github.com/users/TotalVerb","html_url":"https://github.com/TotalVerb","followers_url":"https://api.github.com/users/TotalVerb/followers","following_url":"https://api.github.com/users/TotalVerb/following{/other_user}","gists_url":"https://api.github.com/users/TotalVerb/gists{/gist_id}","starred_url":"https://api.github.com/users/TotalVerb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TotalVerb/subscriptions","organizations_url":"https://api.github.com/users/TotalVerb/orgs","repos_url":"https://api.github.com/users/TotalVerb/repos","events_url":"https://api.github.com/users/TotalVerb/events{/privacy}","received_events_url":"https://api.github.com/users/TotalVerb/received_events","type":"User","site_admin":false},"repo":{"id":60992448,"name":"NullableArrays.jl","full_name":"TotalVerb/NullableArrays.jl","owner":{"login":"TotalVerb","id":12903942,"avatar_url":"https://avatars.githubusercontent.com/u/12903942?v=3","gravatar_id":"","url":"https://api.github.com/users/TotalVerb","html_url":"https://github.com/TotalVerb","followers_url":"https://api.github.com/users/TotalVerb/followers","following_url":"https://api.github.com/users/TotalVerb/following{/other_user}","gists_url":"https://api.github.com/users/TotalVerb/gists{/gist_id}","starred_url":"https://api.github.com/users/TotalVerb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TotalVerb/subscriptions","organizations_url":"https://api.github.com/users/TotalVerb/orgs","repos_url":"https://api.github.com/users/TotalVerb/repos","events_url":"https://api.github.com/users/TotalVerb/events{/privacy}","received_events_url":"https://api.github.com/users/TotalVerb/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/TotalVerb/NullableArrays.jl","description":"Prototype of the new JuliaStats NullableArrays package","fork":true,"url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl","forks_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/forks","keys_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/keys{/key_id}","collaborators_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/teams","hooks_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/hooks","issue_events_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/issues/events{/number}","events_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/events","assignees_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/assignees{/user}","branches_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/branches{/branch}","tags_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/tags","blobs_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/git/refs{/sha}","trees_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/git/trees{/sha}","statuses_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/statuses/{sha}","languages_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/languages","stargazers_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/stargazers","contributors_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/contributors","subscribers_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/subscribers","subscription_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/subscription","commits_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/commits{/sha}","git_commits_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/git/commits{/sha}","comments_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/comments{/number}","issue_comment_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/issues/comments{/number}","contents_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/contents/{+path}","compare_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/compare/{base}...{head}","merges_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/merges","archive_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/downloads","issues_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/issues{/number}","pulls_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/pulls{/number}","milestones_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/milestones{/number}","notifications_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/labels{/name}","releases_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/releases{/id}","deployments_url":"https://api.github.com/repos/TotalVerb/NullableArrays.jl/deployments","created_at":"2016-06-12T22:31:30Z","updated_at":"2016-06-12T22:31:31Z","pushed_at":"2016-06-12T22:59:38Z","git_url":"git://github.com/TotalVerb/NullableArrays.jl.git","ssh_url":"git@github.com:TotalVerb/NullableArrays.jl.git","clone_url":"https://github.com/TotalVerb/NullableArrays.jl.git","svn_url":"https://github.com/TotalVerb/NullableArrays.jl","homepage":null,"size":163,"stargazers_count":0,"watchers_count":0,"language":"Julia","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"JuliaStats:master","ref":"master","sha":"9842b91c21561f7fa6b84aaa4175245306530f73","user":{"login":"JuliaStats","id":2761531,"avatar_url":"https://avatars.githubusercontent.com/u/2761531?v=3","gravatar_id":"","url":"https://api.github.com/users/JuliaStats","html_url":"https://github.com/JuliaStats","followers_url":"https://api.github.com/users/JuliaStats/followers","following_url":"https://api.github.com/users/JuliaStats/following{/other_user}","gists_url":"https://api.github.com/users/JuliaStats/gists{/gist_id}","starred_url":"https://api.github.com/users/JuliaStats/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JuliaStats/subscriptions","organizations_url":"https://api.github.com/users/JuliaStats/orgs","repos_url":"https://api.github.com/users/JuliaStats/repos","events_url":"https://api.github.com/users/JuliaStats/events{/privacy}","received_events_url":"https://api.github.com/users/JuliaStats/received_events","type":"Organization","site_admin":false},"repo":{"id":36910534,"name":"NullableArrays.jl","full_name":"JuliaStats/NullableArrays.jl","owner":{"login":"JuliaStats","id":2761531,"avatar_url":"https://avatars.githubusercontent.com/u/2761531?v=3","gravatar_id":"","url":"https://api.github.com/users/JuliaStats","html_url":"https://github.com/JuliaStats","followers_url":"https://api.github.com/users/JuliaStats/followers","following_url":"https://api.github.com/users/JuliaStats/following{/other_user}","gists_url":"https://api.github.com/users/JuliaStats/gists{/gist_id}","starred_url":"https://api.github.com/users/JuliaStats/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JuliaStats/subscriptions","organizations_url":"https://api.github.com/users/JuliaStats/orgs","repos_url":"https://api.github.com/users/JuliaStats/repos","events_url":"https://api.github.com/users/JuliaStats/events{/privacy}","received_events_url":"https://api.github.com/users/JuliaStats/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/JuliaStats/NullableArrays.jl","description":"Prototype of the new JuliaStats NullableArrays package","fork":false,"url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl","forks_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/forks","keys_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/keys{/key_id}","collaborators_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/teams","hooks_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/hooks","issue_events_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/issues/events{/number}","events_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/events","assignees_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/assignees{/user}","branches_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/branches{/branch}","tags_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/tags","blobs_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/git/refs{/sha}","trees_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/git/trees{/sha}","statuses_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/statuses/{sha}","languages_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/languages","stargazers_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/stargazers","contributors_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/contributors","subscribers_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/subscribers","subscription_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/subscription","commits_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/commits{/sha}","git_commits_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/git/commits{/sha}","comments_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/comments{/number}","issue_comment_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/issues/comments{/number}","contents_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/contents/{+path}","compare_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/compare/{base}...{head}","merges_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/merges","archive_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/downloads","issues_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/issues{/number}","pulls_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls{/number}","milestones_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/milestones{/number}","notifications_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/labels{/name}","releases_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/releases{/id}","deployments_url":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/deployments","created_at":"2015-06-05T03:48:58Z","updated_at":"2016-06-04T05:51:31Z","pushed_at":"2016-06-15T00:59:34Z","git_url":"git://github.com/JuliaStats/NullableArrays.jl.git","ssh_url":"git@github.com:JuliaStats/NullableArrays.jl.git","clone_url":"https://github.com/JuliaStats/NullableArrays.jl.git","svn_url":"https://github.com/JuliaStats/NullableArrays.jl","homepage":null,"size":177,"stargazers_count":22,"watchers_count":22,"language":"Julia","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":14,"mirror_url":null,"open_issues_count":29,"forks":14,"open_issues":29,"watchers":22,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/115"},"html":{"href":"https://github.com/JuliaStats/NullableArrays.jl/pull/115"},"issue":{"href":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/issues/115"},"comments":{"href":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/issues/115/comments"},"review_comments":{"href":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/115/comments"},"review_comment":{"href":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/pulls/115/commits"},"statuses":{"href":"https://api.github.com/repos/JuliaStats/NullableArrays.jl/statuses/d69b31dc1e7ee2ffc89e4f5ecf516d21bd4163ef"}}}}
{"actor":{"display_login":"TotalVerb"}}