Skip to content

Commit

Permalink
adds map for Nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Dec 24, 2014
1 parent 4814557 commit b48975a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ function hash(x::Nullable, h::UInt)
return hash(x.value, h + nullablehash_seed)
end
end

# Specialized map over Nullable
function map{T}(f::Callable, x::Nullable{T})
isnull(x) && return Nullable{T}()
Nullable(f(get(x)))
end
8 changes: 8 additions & 0 deletions test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,11 @@ for T in types
@test !isnull(x1.v)
@test get(x1.v, one(T)) === one(T)
end

# test map
@test map(x->x, Nullable{Union()}()) === Nullable{Union()}()

for T in types
@test isequal(map(x->x, Nullable{T}()), Nullable{T}())
@test map(x->x, Nullable(one(T))) === Nullable(one(T))
end

0 comments on commit b48975a

Please sign in to comment.