Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: regularize Dict syntax #8521

Merged
merged 9 commits into from
Oct 6, 2014
16 changes: 13 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ Language changes

* `Nothing` (the type of `nothing`) is renamed to `Void` ([#8423]).

* `Dict` literal syntax `[a=>b,c=>d]` is replaced with `Dict(a=>b,c=>d)`.
`{a=>b}` is replaced with `Dict{Any,Any}(a=>b)`.
`(K=>V)[...]` is replaced with `Dict{K,V}(...)`.
The new syntax has many advantages: all of its components are first-class,
it generalizes to other types of containers, it is easier to guess how to
specify key and value types, and the syntaxes for empty and pre-populated
dicts are synchronized. As part of this change, `=>` is parsed as a normal
operator, and `Base` defines it to construct `Pair` objects ([#6739]).

Library improvements
--------------------

Expand Down Expand Up @@ -74,7 +83,7 @@ New language features
generated. Constructors that look like `MyType(a, b) = new(a, b)` do not
need to be added manually ([#4026], [#7071]).

* Expanded array type hierarchy to include an abstract ``DenseArray`` for
* Expanded array type hierarchy to include an abstract `DenseArray` for
in-memory arrays with standard strided storage ([#987], [#2345],
[#6212]).

Expand Down Expand Up @@ -199,8 +208,8 @@ Library improvements

* `writedlm` and `writecsv` now accept any iterable collection of
iterable rows, in addition to `AbstractArray` arguments, and the
``writedlm`` delimiter can be any printable object (e.g. a
``String``) instead of just a ``Char``.
`writedlm` delimiter can be any printable object (e.g. a
`String`) instead of just a `Char`.

* `isempty` now works for any iterable collection ([#5827]).

Expand Down Expand Up @@ -991,3 +1000,4 @@ Too numerous to mention.
[#7311]: https://github.com/JuliaLang/julia/issues/7311
[#8423]: https://github.com/JuliaLang/julia/issues/8423
[#8152]: https://github.com/JuliaLang/julia/pull/8152
[#6739]: https://github.com/JuliaLang/julia/issues/6739
22 changes: 11 additions & 11 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using ..Terminals
import ..Terminals: raw!, width, height, cmove, getX,
getY, clear_line, beep

import Base: ensureroom, peek, show
import Base: ensureroom, peek, show, AnyDict

abstract TextInterface

Expand Down Expand Up @@ -835,7 +835,7 @@ end

const escape_defaults = merge!(
{char(i) => nothing for i=[1:26, 28:31]}, # Ignore control characters by default
{ # And ignore other escape sequences by default
AnyDict( # And ignore other escape sequences by default
"\e*" => nothing,
"\e[*" => nothing,
# Also ignore extended escape sequences
Expand All @@ -856,7 +856,7 @@ const escape_defaults = merge!(
"\eOD" => "\e[D",
"\eOH" => "\e[H",
"\eOF" => "\e[F",
})
))

function write_response_buffer(s::PromptState, data)
offset = s.input_buffer.ptr
Expand Down Expand Up @@ -997,7 +997,7 @@ end

function setup_search_keymap(hp)
p = HistoryPrompt(hp)
pkeymap = {
pkeymap = AnyDict(
"^R" => (s,data,c)->(history_set_backward(data, true); history_next_result(s, data)),
"^S" => (s,data,c)->(history_set_backward(data, false); history_next_result(s, data)),
'\r' => (s,o...)->accept_result(s, p),
Expand Down Expand Up @@ -1066,12 +1066,12 @@ function setup_search_keymap(hp)
edit_insert(data.query_buffer, input); update_display_buffer(s, data)
end,
"*" => (s,data,c)->(edit_insert(data.query_buffer, c); update_display_buffer(s, data))
}
)
p.keymap_func = keymap([pkeymap, escape_defaults])
skeymap = {
skeymap = AnyDict(
"^R" => (s,o...)->(enter_search(s, p, true)),
"^S" => (s,o...)->(enter_search(s, p, false)),
}
)
(p, skeymap)
end

Expand Down Expand Up @@ -1118,7 +1118,7 @@ function commit_line(s)
end

const default_keymap =
{
AnyDict(
# Tab
'\t' => (s,o...)->begin
buf = buffer(s)
Expand Down Expand Up @@ -1226,9 +1226,9 @@ const default_keymap =
edit_insert(s, input)
end,
"^T" => (s,o...)->edit_transpose(s),
}
)

const history_keymap = {
const history_keymap = AnyDict(
"^P" => (s,o...)->(history_prev(s, mode(s).hist)),
"^N" => (s,o...)->(history_next(s, mode(s).hist)),
# Up Arrow
Expand All @@ -1239,7 +1239,7 @@ const history_keymap = {
"\e[5~" => (s,o...)->(history_prev(s, mode(s).hist)),
# Page Down
"\e[6~" => (s,o...)->(history_next(s, mode(s).hist))
}
)

function deactivate(p::Union(Prompt,HistoryPrompt), s::Union(SearchState,PromptState), termbuf)
clear_input_area(termbuf, s)
Expand Down
17 changes: 9 additions & 8 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import Base:
AsyncStream,
Display,
display,
writemime
writemime,
AnyDict

import ..LineEdit:
CompletionProvider,
Expand Down Expand Up @@ -675,9 +676,9 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep

# Setup history
# We will have a unified history for all REPL modes
hp = REPLHistoryProvider((Symbol=>Any)[:julia => julia_prompt,
:shell => shell_mode,
:help => help_mode])
hp = REPLHistoryProvider(Dict{Symbol,Any}(:julia => julia_prompt,
:shell => shell_mode,
:help => help_mode))
if !repl.no_history_file
try
f = open(find_hist_file(), true, true, true, false, false)
Expand All @@ -704,7 +705,7 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
extra_repl_keymap = [extra_repl_keymap]
end

const repl_keymap = {
const repl_keymap = AnyDict(
';' => function (s,o...)
if isempty(s) || position(LineEdit.buffer(s)) == 0
buf = copy(LineEdit.buffer(s))
Expand Down Expand Up @@ -775,14 +776,14 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
end
end
end,
}
)

a = Dict{Any,Any}[hkeymap, repl_keymap, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults]
prepend!(a, extra_repl_keymap)

julia_prompt.keymap_func = LineEdit.keymap(a)

const mode_keymap = {
const mode_keymap = AnyDict(
'\b' => function (s,o...)
if isempty(s) || position(LineEdit.buffer(s)) == 0
buf = copy(LineEdit.buffer(s))
Expand All @@ -801,7 +802,7 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
transition(s, :reset)
LineEdit.refresh_line(s)
end
}
)

b = Dict{Any,Any}[hkeymap, mode_keymap, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults]
prepend!(b, extra_repl_keymap)
Expand Down
8 changes: 4 additions & 4 deletions base/cartesian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,10 @@ end

## Resolve expressions at parsing time ##

const exprresolve_arith_dict = (Symbol=>Function)[:+ => +,
:- => -, :* => *, :/ => /, :^ => ^, :div => div]
const exprresolve_cond_dict = (Symbol=>Function)[:(==) => ==,
:(<) => <, :(>) => >, :(<=) => <=, :(>=) => >=]
const exprresolve_arith_dict = Dict{Symbol,Function}(:+ => +,
:- => -, :* => *, :/ => /, :^ => ^, :div => div)
const exprresolve_cond_dict = Dict{Symbol,Function}(:(==) => ==,
:(<) => <, :(>) => >, :(<=) => <=, :(>=) => >=)

function exprresolve_arith(ex::Expr)
if ex.head == :call && haskey(exprresolve_arith_dict, ex.args[1]) && all([isa(ex.args[i], Number) for i = 2:length(ex.args)])
Expand Down
4 changes: 2 additions & 2 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const ARGS = UTF8String[]

const text_colors = {
const text_colors = AnyDict(
:black => "\033[1m\033[30m",
:red => "\033[1m\033[31m",
:green => "\033[1m\033[32m",
Expand All @@ -14,7 +14,7 @@ const text_colors = {
:white => "\033[1m\033[37m",
:normal => "\033[0m",
:bold => "\033[1m",
}
)

have_color = false
@unix_only default_color_answer = text_colors[:bold]
Expand Down
47 changes: 17 additions & 30 deletions base/collections.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

module Collections

import Base: setindex!, done, get, hash, haskey, isempty, length, next, getindex, start
import ..Order: Forward, Ordering, lt

export
Pair,
PriorityQueue,
dequeue!,
enqueue!,
Expand Down Expand Up @@ -107,17 +105,6 @@ end
# PriorityQueue
# -------------

immutable Pair{A,B}
a::A
b::B
end

function hash(p::Pair, h::Uint)
h = hash(p.a, h)
hash(p.b, h)
end
hash(p::Pair) = hash(p, zero(Uint))

# A PriorityQueue that acts like a Dict, mapping values to their priorities,
# with the addition of a dequeue! function to remove the lowest priority
# element.
Expand Down Expand Up @@ -179,23 +166,23 @@ PriorityQueue{K,V}(a::AbstractArray{(K,V)}, o::Ordering=Forward) = PriorityQueue
length(pq::PriorityQueue) = length(pq.xs)
isempty(pq::PriorityQueue) = isempty(pq.xs)
haskey(pq::PriorityQueue, key) = haskey(pq.index, key)
peek(pq::PriorityQueue) = (kv = pq.xs[1]; (kv.a, kv.b))
peek(pq::PriorityQueue) = (kv = pq.xs[1]; (kv.first, kv.second))


function percolate_down!(pq::PriorityQueue, i::Integer)
x = pq.xs[i]
@inbounds while (l = heapleft(i)) <= length(pq)
r = heapright(i)
j = r > length(pq) || lt(pq.o, pq.xs[l].b, pq.xs[r].b) ? l : r
if lt(pq.o, pq.xs[j].b, x.b)
pq.index[pq.xs[j].a] = i
j = r > length(pq) || lt(pq.o, pq.xs[l].second, pq.xs[r].second) ? l : r
if lt(pq.o, pq.xs[j].second, x.second)
pq.index[pq.xs[j].first] = i
pq.xs[i] = pq.xs[j]
i = j
else
break
end
end
pq.index[x.a] = i
pq.index[x.first] = i
pq.xs[i] = x
end

Expand All @@ -204,15 +191,15 @@ function percolate_up!(pq::PriorityQueue, i::Integer)
x = pq.xs[i]
@inbounds while i > 1
j = heapparent(i)
if lt(pq.o, x.b, pq.xs[j].b)
pq.index[pq.xs[j].a] = i
if lt(pq.o, x.second, pq.xs[j].second)
pq.index[pq.xs[j].first] = i
pq.xs[i] = pq.xs[j]
i = j
else
break
end
end
pq.index[x.a] = i
pq.index[x.first] = i
pq.xs[i] = x
end

Expand All @@ -221,30 +208,30 @@ function force_up!(pq::PriorityQueue, i::Integer)
x = pq.xs[i]
@inbounds while i > 1
j = heapparent(i)
pq.index[pq.xs[j].a] = i
pq.index[pq.xs[j].first] = i
pq.xs[i] = pq.xs[j]
i = j
end
pq.index[x.a] = i
pq.index[x.first] = i
pq.xs[i] = x
end

function getindex{K,V}(pq::PriorityQueue{K,V}, key)
pq.xs[pq.index[key]].b
pq.xs[pq.index[key]].second
end


function get{K,V}(pq::PriorityQueue{K,V}, key, deflt)
i = get(pq.index, key, 0)
i == 0 ? deflt : pq.xs[i].b
i == 0 ? deflt : pq.xs[i].second
end


# Change the priority of an existing element, or equeue it if it isn't present.
function setindex!{K,V}(pq::PriorityQueue{K, V}, value, key)
if haskey(pq, key)
i = pq.index[key]
oldvalue = pq.xs[i].b
oldvalue = pq.xs[i].second
pq.xs[i] = Pair{K,V}(key, value)
if lt(pq.o, oldvalue, value)
percolate_down!(pq, i)
Expand Down Expand Up @@ -275,11 +262,11 @@ function dequeue!(pq::PriorityQueue)
y = pop!(pq.xs)
if !isempty(pq)
pq.xs[1] = y
pq.index[y.a] = 1
pq.index[y.first] = 1
percolate_down!(pq, 1)
end
delete!(pq.index, x.a)
x.a
delete!(pq.index, x.first)
x.first
end

function dequeue!(pq::PriorityQueue, key)
Expand All @@ -297,7 +284,7 @@ done(pq::PriorityQueue, i) = done(pq.index, i)

function next(pq::PriorityQueue, i)
(k, idx), i = next(pq.index, i)
return ((k, pq.xs[idx].b), i)
return ((k, pq.xs[idx].second), i)
end


Expand Down
6 changes: 3 additions & 3 deletions base/combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ function nextpartition(n, as)
xs
end

let _npartitions = (Int=>Int)[]
let _npartitions = Dict{Int,Int}()
global npartitions
function npartitions(n::Int)
if n < 0
Expand Down Expand Up @@ -424,7 +424,7 @@ function nextfixedpartition(n, m, bs)
return as
end

let _nipartitions = ((Int,Int)=>Int)[]
let _nipartitions = Dict{(Int,Int),Int}()
global npartitions
function npartitions(n::Int,m::Int)
if n < m || m == 0
Expand Down Expand Up @@ -489,7 +489,7 @@ function nextsetpartition(s::AbstractVector, a, b, n, m)

end

let _nsetpartitions = (Int=>Int)[]
let _nsetpartitions = Dict{Int,Int}()
global nsetpartitions
function nsetpartitions(n::Int)
if n < 0
Expand Down
2 changes: 1 addition & 1 deletion base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ end

const valid_opts = [:header, :has_header, :ignore_invalid_chars, :use_mmap, :quotes, :comments, :dims, :comment_char, :skipstart, :skipblanks]
const valid_opt_types = [Bool, Bool, Bool, Bool, Bool, Bool, NTuple{2,Integer}, Char, Integer, Bool]
const deprecated_opts = [ :has_header => :header ]
const deprecated_opts = Dict(:has_header => :header)
function val_opts(opts)
d = Dict{Symbol,Union(Bool,NTuple{2,Integer},Char,Integer)}()
for (opt_name, opt_val) in opts
Expand Down
Loading