Skip to content

Commit

Permalink
Remove uses of |> chaining from base
Browse files Browse the repository at this point in the history
the usual syntax for function application is faster and more generic
packages can use chaining if they like, but best for base to pick a
style and use it consistently
  • Loading branch information
tkelman committed Oct 19, 2015
1 parent 8396c9f commit 212727c
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 66 deletions.
4 changes: 2 additions & 2 deletions base/dates/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ for op in (:.+, :.-)
end
return Z
end
($op){P<:GeneralPeriod}(x::GeneralPeriod,Y::StridedArray{P}) = ($op)(Y,x) |> ($op_)
($op_){P<:GeneralPeriod}(x::GeneralPeriod,Y::StridedArray{P}) = ($op)(Y,x) |> ($op_)
($op){P<:GeneralPeriod}(x::GeneralPeriod,Y::StridedArray{P}) = $op_(($op)(Y,x))
($op_){P<:GeneralPeriod}(x::GeneralPeriod,Y::StridedArray{P}) = $op_(($op)(Y,x))
($op_){P<:GeneralPeriod}(Y::StridedArray{P},x::GeneralPeriod) = ($op)(Y,x)
($op_){P<:GeneralPeriod, Q<:GeneralPeriod}(X::StridedArray{P}, Y::StridedArray{Q}) =
reshape(CompoundPeriod[($op_)(X[i],Y[i]) for i in eachindex(X, Y)], promote_shape(size(X),size(Y)))
Expand Down
4 changes: 2 additions & 2 deletions base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,11 @@ Low-level macro used to mark expressions returned by a macro that should be docu
more than one expression is marked then the same docstring is applied to each expression.
macro example(f)
quote
esc(quote
$(f)() = 0
@__doc__ $(f)(x) = 1
$(f)(x, y) = 2
end |> esc
end)
end
`@__doc__` has no effect when a macro that uses it is not documented.
Expand Down
6 changes: 3 additions & 3 deletions base/docs/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ end

function fuzzysort(search, candidates)
scores = map(cand -> (fuzzyscore(search, cand), -levenshtein(search, cand)), candidates)
candidates[sortperm(scores)] |> reverse
reverse(candidates[sortperm(scores)])
end

# Levenshtein Distance
Expand Down Expand Up @@ -293,9 +293,9 @@ moduleusings(mod) = ccall(:jl_module_usings, Any, (Any,), mod)
filtervalid(names) = filter(x->!ismatch(r"#", x), map(string, names))

accessible(mod::Module) =
[names(mod, true, true);
filtervalid(unique([names(mod, true, true);
map(names, moduleusings(mod))...;
builtins] |> unique |> filtervalid
builtins]))

completions(name) = fuzzysort(name, accessible(current_module()))
completions(name::Symbol) = completions(string(name))
Expand Down
6 changes: 3 additions & 3 deletions base/markdown/Common/block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function hashheader(stream::IO, md::MD)
return false

if c != '\n' # Empty header
h = readline(stream) |> strip
h = strip(readline(stream))
h = match(r"(.*?)( +#+)?$", h).captures[1]
buffer = IOBuffer()
print(buffer, h)
Expand All @@ -76,11 +76,11 @@ end
function setextheader(stream::IO, md::MD)
withstream(stream) do
eatindent(stream) || return false
header = readline(stream) |> strip
header = strip(readline(stream))
header == "" && return false

eatindent(stream) || return false
underline = readline(stream) |> strip
underline = strip(readline(stream))
length(underline) < 3 && return false
u = underline[1]
u in "-=" || return false
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/GitHub/GitHub.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function fencedcode(stream::IO, block::MD)
line_start = position(stream)
if startswith(stream, string(ch) ^ n)
if !startswith(stream, string(ch))
push!(block, Code(flavor, takebuf_string(buffer) |> chomp))
push!(block, Code(flavor, chomp(takebuf_string(buffer))))
return true
else
seek(stream, line_start)
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/GitHub/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end

function parserow(stream::IO)
withstream(stream) do
line = readline(stream) |> chomp
line = chomp(readline(stream))
row = split(line, "|")
length(row) == 1 && return
row[1] == "" && shift!(row)
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/parse/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function linecontains(io::IO, chars; allow_whitespace = true,
eat = true,
allowempty = false)
start = position(io)
l = readline(io) |> chomp
l = chomp(readline(io))
length(l) == 0 && return allowempty

result = allowempty
Expand Down
4 changes: 2 additions & 2 deletions base/markdown/render/terminal/formatting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ end
# Wrapping

function ansi_length(s)
replace(s, r"\e\[[0-9]+m", "") |> length
length(replace(s, r"\e\[[0-9]+m", ""))
end

words(s) = split(s, " ")
Expand All @@ -64,7 +64,7 @@ function wrapped_lines(s::AbstractString; width = 80, i = 0)
end
ws = words(s)
lines = AbstractString[ws[1]]
i += ws[1] |> ansi_length
i += ansi_length(ws[1])
for word in ws[2:end]
word_length = ansi_length(word)
if i + word_length + 1 > width
Expand Down
4 changes: 2 additions & 2 deletions doc/manual/documentation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,11 @@ Examining it's definition should serve as an example of how to use ``@__doc__``
.. code-block:: julia
macro example(f)
quote
esc(quote
$(f)() = 0
@__doc__ $(f)(x) = 1
$(f)(x, y) = 2
end |> esc
end)
end
``@__doc__`` has no effect when a macro that uses it is not documented.
Expand Down
2 changes: 1 addition & 1 deletion doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ System
.. data:: DevNull

Used in a stream redirect to discard all data written to it. Essentially equivalent to /dev/null on Unix or NUL on Windows.
Usage: ``run(`cat test.txt` |> DevNull)``
Usage: ``run(pipeline(`cat test.txt`, DevNull))``

.. function:: success(command)

Expand Down
8 changes: 4 additions & 4 deletions test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ module MacroGenerated
import Base.@__doc__

macro example_1(f)
quote
esc(quote
$(f)() = 0
@__doc__ $(f)(x) = x
$(f)(x, y) = x + y
end |> esc
end)
end

"f"
Expand All @@ -289,11 +289,11 @@ end
@example_1 _f

macro example_2(f)
quote
esc(quote
$(f)() = 0
@__doc__ $(f)(x) = x
@__doc__ $(f)(x, y) = x + y
end |> esc
end)
end

"g"
Expand Down
2 changes: 1 addition & 1 deletion test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ temp_dir() do dir

# set a remote branch
branch = "upstream"
LibGit2.GitRemote(repo, branch, repo_url) |> finalize
finalize(LibGit2.GitRemote(repo, branch, repo_url))

config = joinpath(cache_repo, ".git", "config")
lines = split(open(readall, config, "r"), "\n")
Expand Down
8 changes: 4 additions & 4 deletions test/linalg/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ for i = 1:10
@test ones(0,0)*ones(0,4) == zeros(0,4)
@test ones(3,0)*ones(0,0) == zeros(3,0)
@test ones(0,0)*ones(0,0) == zeros(0,0)
@test Array(Float64, 5, 0) |> t -> t't == zeros(0,0)
@test Array(Float64, 5, 0) |> t -> t*t' == zeros(5,5)
@test Array(Complex128, 5, 0) |> t -> t't == zeros(0,0)
@test Array(Complex128, 5, 0) |> t -> t*t' == zeros(5,5)
@test (t -> t't)(Array(Float64, 5, 0)) == zeros(0,0)
@test (t -> t*t')(Array(Float64, 5, 0)) == zeros(5,5)
@test (t -> t't)(Array(Complex128, 5, 0)) == zeros(0,0)
@test (t -> t*t')(Array(Complex128, 5, 0)) == zeros(5,5)
end

# 2x2
Expand Down
34 changes: 26 additions & 8 deletions test/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ srand(123)

debug && println("Test basic type functionality")
@test_throws DimensionMismatch LowerTriangular(randn(5, 4))
@test LowerTriangular(randn(3, 3)) |> t -> [size(t, i) for i = 1:3] == [size(full(t), i) for i = 1:3]
@test (t -> [size(t, i) for i = 1:3] == [size(full(t), i) for i = 1:3])(LowerTriangular(randn(3, 3)))

# The following test block tries to call all methods in base/linalg/triangular.jl in order for a combination of input element types. Keep the ordering when adding code.
for elty1 in (Float32, Float64, Complex64, Complex128, BigFloat, Int)
Expand All @@ -22,7 +22,15 @@ for elty1 in (Float32, Float64, Complex64, Complex128, BigFloat, Int)
(UnitLowerTriangular, :L))

# Construct test matrix
A1 = t1(elty1 == Int ? rand(1:7, n, n) : convert(Matrix{elty1}, (elty1 <: Complex ? complex(randn(n, n), randn(n, n)) : randn(n, n)) |> t -> chol(t't, Val{uplo1})))
if elty1 == Int
A1 = t1(rand(1:7, n, n))
elseif elty1 <: Complex
A1 = t1(convert(Matrix{elty1},
(t -> chol(t't, Val{uplo1}))(complex(randn(n, n), randn(n, n)))))
else
A1 = t1(convert(Matrix{elty1},
(t -> chol(t't, Val{uplo1}))(randn(n, n))))
end

debug && println("elty1: $elty1, A1: $t1")

Expand Down Expand Up @@ -206,7 +214,7 @@ for elty1 in (Float32, Float64, Complex64, Complex128, BigFloat, Int)
@test_approx_eq_eps det(A1) det(lufact(full(A1))) sqrt(eps(real(float(one(elty1)))))*n*n

# Matrix square root
@test sqrtm(A1) |> t->t*t A1
@test (t->t*t)(sqrtm(A1)) A1

# naivesub errors
@test_throws DimensionMismatch naivesub!(A1,ones(elty1,n+1))
Expand Down Expand Up @@ -243,7 +251,15 @@ for elty1 in (Float32, Float64, Complex64, Complex128, BigFloat, Int)

debug && println("elty1: $elty1, A1: $t1, elty2: $elty2")

A2 = t2(elty2 == Int ? rand(1:7, n, n) : convert(Matrix{elty2}, (elty2 <: Complex ? complex(randn(n, n), randn(n, n)) : randn(n, n)) |> t-> chol(t't, Val{uplo2})))
if elty2 == Int
A2 = t2(rand(1:7, n, n))
elseif elty2 <: Complex
A2 = t2(convert(Matrix{elty2},
(t -> chol(t't, Val{uplo2}))(complex(randn(n, n), randn(n, n)))))
else
A2 = t2(convert(Matrix{elty2},
(t -> chol(t't, Val{uplo2}))(randn(n, n))))
end

# Convert
if elty1 <: Real && !(elty2 <: Integer)
Expand Down Expand Up @@ -330,9 +346,9 @@ end
# Matrix square root
Atn = UpperTriangular([-1 1 2; 0 -2 2; 0 0 -3])
Atp = UpperTriangular([1 1 2; 0 2 2; 0 0 3])
@test sqrtm(Atn) |> t->t*t Atn
@test (t->t*t)(sqrtm(Atn)) Atn
@test typeof(sqrtm(Atn)[1,1]) <: Complex
@test sqrtm(Atp) |> t->t*t Atp
@test (t->t*t)(sqrtm(Atp)) Atp
@test typeof(sqrtm(Atp)[1,1]) <: Real

Areal = randn(n, n)/2
Expand All @@ -352,7 +368,8 @@ for eltya in (Float32, Float64, Complex64, Complex128, BigFloat, Int)
debug && println("\ntype of A: ", eltya, " type of b: ", eltyb, "\n")

debug && println("Solve upper triangular system")
Atri = UpperTriangular(lufact(A)[:U]) |> t -> eltya <: Complex && eltyb <: Real ? real(t) : t # Here the triangular matrix can't be too badly conditioned
Atri = (t -> eltya <: Complex && eltyb <: Real ? real(t) : t)( # Here the triangular matrix can't be too badly conditioned
UpperTriangular(lufact(A)[:U]))
b = convert(Matrix{eltyb}, eltya <: Complex ? full(Atri)*ones(n, 2) : full(Atri)*ones(n, 2))
x = full(Atri) \ b

Expand Down Expand Up @@ -380,7 +397,8 @@ for eltya in (Float32, Float64, Complex64, Complex128, BigFloat, Int)
end

debug && println("Solve lower triangular system")
Atri = UpperTriangular(lufact(A)[:U]) |> t -> eltya <: Complex && eltyb <: Real ? real(t) : t # Here the triangular matrix can't be too badly conditioned
Atri = (t -> eltya <: Complex && eltyb <: Real ? real(t) : t)( # Here the triangular matrix can't be too badly conditioned
UpperTriangular(lufact(A)[:U]))
b = convert(Matrix{eltyb}, eltya <: Complex ? full(Atri)*ones(n, 2) : full(Atri)*ones(n, 2))
x = full(Atri)\b

Expand Down
60 changes: 30 additions & 30 deletions test/markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,61 +56,61 @@ foo

# Basic plain (markdown) output

@test md"foo" |> plain == "foo\n"
@test md"foo *bar* baz" |> plain == "foo *bar* baz\n"
@test md"# title" |> plain == "# title\n"
@test md"## section" |> plain == "## section\n"
@test md"## section `foo`" |> plain == "## section `foo`\n"
@test md"""Hello
@test plain(md"foo") == "foo\n"
@test plain(md"foo *bar* baz") == "foo *bar* baz\n"
@test plain(md"# title") == "# title\n"
@test plain(md"## section") == "## section\n"
@test plain(md"## section `foo`") == "## section `foo`\n"
@test plain(md"""Hello
---
World""" |> plain == "Hello\n\n–––\n\nWorld\n"
@test md"[*a*](b)" |> plain == "[*a*](b)\n"
@test md"""
World""") == "Hello\n\n–––\n\nWorld\n"
@test plain(md"[*a*](b)") == "[*a*](b)\n"
@test plain(md"""
> foo
>
> * bar
>
> ```
> baz
> ```""" |> plain == """> foo\n>\n> * bar\n>\n> ```\n> baz\n> ```\n\n"""
> ```""") == """> foo\n>\n> * bar\n>\n> ```\n> baz\n> ```\n\n"""

# HTML output

@test md"foo *bar* baz" |> html == "<p>foo <em>bar</em> baz</p>\n"
@test md"something ***" |> html == "<p>something ***</p>\n"
@test md"# h1## " |> html == "<h1>h1##</h1>\n"
@test md"## h2 ### " |> html == "<h2>h2</h2>\n"
@test md"###### h6" |> html == "<h6>h6</h6>\n"
@test md"####### h7" |> html == "<p>####### h7</p>\n"
@test md" >" |> html == "<blockquote>\n</blockquote>\n"
@test md"1. Hello" |> html == "<ol>\n<li>Hello</li>\n</ol>\n"
@test md"* World" |> html == "<ul>\n<li>World</li>\n</ul>\n"
@test md"# title *blah*" |> html == "<h1>title <em>blah</em></h1>\n"
@test md"## title *blah*" |> html == "<h2>title <em>blah</em></h2>\n"
@test md"""Hello
@test html(md"foo *bar* baz") == "<p>foo <em>bar</em> baz</p>\n"
@test html(md"something ***") == "<p>something ***</p>\n"
@test html(md"# h1## ") == "<h1>h1##</h1>\n"
@test html(md"## h2 ### ") == "<h2>h2</h2>\n"
@test html(md"###### h6") == "<h6>h6</h6>\n"
@test html(md"####### h7") == "<p>####### h7</p>\n"
@test html(md" >") == "<blockquote>\n</blockquote>\n"
@test html(md"1. Hello") == "<ol>\n<li>Hello</li>\n</ol>\n"
@test html(md"* World") == "<ul>\n<li>World</li>\n</ul>\n"
@test html(md"# title *blah*") == "<h1>title <em>blah</em></h1>\n"
@test html(md"## title *blah*") == "<h2>title <em>blah</em></h2>\n"
@test html(md"""Hello
---
World""" |> html == "<p>Hello</p>\n<hr />\n<p>World</p>\n"
@test md"`escape</code>`" |> html == "<p><code>escape&lt;/code&gt;</code></p>\n"
World""") == "<p>Hello</p>\n<hr />\n<p>World</p>\n"
@test html(md"`escape</code>`") == "<p><code>escape&lt;/code&gt;</code></p>\n"

@test md"""
@test html(md"""
code1
code2
""" |> html == "<pre><code>code1\n\ncode2</code></pre>\n" # single code block
""") == "<pre><code>code1\n\ncode2</code></pre>\n" # single code block

# @test md"""
# @test html(md"""
# - Foo
# ---
# - Bar""" |> html == "<ul>\n<li>Foo</li>\n</ul>\n<hr />\n<ul>\n<li>Bar</li>\n</ul>\n"
@test md"""
# - Bar""") == "<ul>\n<li>Foo</li>\n</ul>\n<hr />\n<ul>\n<li>Bar</li>\n</ul>\n"
@test html(md"""
h1
===
h2
---
not
== =""" |> html == "<h1>h1</h1>\n<h2>h2</h2>\n<p>not &#61;&#61; &#61;</p>\n"
== =""") == "<h1>h1</h1>\n<h2>h2</h2>\n<p>not &#61;&#61; &#61;</p>\n"

# Latex output
book = md"""
Expand Down
2 changes: 1 addition & 1 deletion test/sparsedir/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ for elty in (Float64, Complex{Float64})
@test_approx_eq logdet(ldltfact(A1pd)) logdet(full(A1pd))
@test isposdef(A1pd)
@test !isposdef(A1)
@test !isposdef(A1 + A1' |> t -> t - 2eigmax(full(t))*I)
@test !isposdef((t -> t - 2eigmax(full(t))*I)(A1 + A1'))

if elty <: Real
@test CHOLMOD.issym(Sparse(A1pd, 0))
Expand Down

0 comments on commit 212727c

Please sign in to comment.