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

macros don't like "!"-suffixed method names #251

Closed
StefanKarpinski opened this issue Nov 2, 2011 · 3 comments
Closed

macros don't like "!"-suffixed method names #251

StefanKarpinski opened this issue Nov 2, 2011 · 3 comments
Assignees
Labels
kind:bug Indicates an unexpected problem or unintended behavior

Comments

@StefanKarpinski
Copy link
Sponsor Member

I have this patch:

diff --git a/j/combinatorics.j b/j/combinatorics.j
index ce4c4bb..9a57567 100644
--- a/j/combinatorics.j
+++ b/j/combinatorics.j
@@ -56,7 +56,7 @@ function binomial{T<:Int}(n::T, k::T)
     return sgn*convert(T,x)
 end

-## in-place sorting methods ##
+## sorting ##

 function issorted(v::AbstractVector)
     for i=1:(length(v)-1)
@@ -222,37 +222,38 @@ function _jl_mergesort(a::AbstractVector, lo::Int, hi::Int, b::AbstractVector)
 end

 sort!{T <: Real}(a::AbstractVector{T}) = _jl_quicksort(a, 1, length(a))
-
-sort!{T}(a::AbstractVector{T}) =
-    _jl_mergesort(a, 1, length(a), Array(T, length(a)))
+sort!{T}(a::AbstractVector{T}) = _jl_mergesort(a, 1, length(a), Array(T, length(a)))

 sortperm{T}(a::AbstractVector{T}) =
     _jl_mergesort(copy(a), linspace(1,length(a)), 1, length(a),
-              Array(T, length(a)), Array(Size, length(a)))
-
-function sort!(a::AbstractMatrix, dim::Index)
-    m, n = size(a)
-    if dim == 1
-        for i=1:m:numel(a)
-            sort!(sub(a, i:(i+m-1)))
-        end
-    elseif dim == 2
-        for i=1:m
-            sort!(sub(a, i:m:numel(a)))
+                  Array(T, length(a)), Array(Size, length(a)))
+
+macro in_place_matrix_op(in_place, out_of_place)
+    quote
+        function ($in_place)(a::AbstractMatrix, dim::Index)
+            m = size(a,1)
+            if dim == 1
+                for i=1:m:numel(a)
+                    ($in_place)(sub(a, i:(i+m-1)))
+                end
+            elseif dim == 2
+                for i=1:m
+                    ($in_place)(sub(a, i:m:numel(a)))
+                end
+            end
+            return a
         end
+        # TODO: in-place generalized AbstractArray implementation
+        ($in_place)(a::AbstractMatrix) = ($in_place)(a,1)
+
+        ($out_of_place)(a::AbstractVector) = ($in_place)(copy(a))
+        ($out_of_place)(a::AbstractArray, dim::Index) = ($in_place)(copy(a), dim)
     end
-    return a
 end

-sort!(a::AbstractArray) = sort!(a, 1)
-
-## non-in-place sort methods ##
-
-sort(a::AbstractVector) = sort!(copy(a))
-sort(a::AbstractArray, dim::Index) = sort!(copy(a), dim)
-sort(a::AbstractArray) = sort(a, 1)
+@in_place_matrix_op :(sort!) :sort

-# TODO: make this in-place, then call in-place version on a copy
+# TODO: implement generalized in-place, ditch this
 function sort(a::AbstractArray, dim::Index)
     X = similar(a)
     n = size(a,dim)
@@ -271,7 +272,8 @@ function sort(a::AbstractArray, dim::Index)
     return X
 end

-# Knuth shuffle
+## other ordering related functions ##
+
 function shuffle!(a::AbstractVector)
     for i = length(a):-1:2
         j = randi(i)
@@ -279,7 +281,8 @@ function shuffle!(a::AbstractVector)
     end
     return a
 end
-shuffle(a::AbstractVector) = shuffle!(copy(a))
+
+@in_place_matrix_op :(shuffle!) :shuffle

 function randperm(n::Int)
     a = Array(typeof(n), n)

which results in the following error when trying to make:

syntax error: invalid method name 'sort! /Users/stefan/projects/julia/j/combinatorics.j:254 /Users/stefan/projects/julia/j/sysimg.j:87
@ghost ghost assigned JeffBezanson Nov 2, 2011
@JeffBezanson
Copy link
Sponsor Member

It's not the !, it's the colon. You don't need to quote symbols to pass them to macros since macro arguments are not evaluated.

@StefanKarpinski
Copy link
Sponsor Member Author

I tried a bunch of variants of this, with and without colons, and had troubles with all. Let me see if I can get it to work.

@StefanKarpinski
Copy link
Sponsor Member Author

Not sure what I was doing wrong but it now works: e9389ae.

KristofferC added a commit that referenced this issue Apr 19, 2018
cmcaine added a commit to cmcaine/julia that referenced this issue Sep 24, 2020
…e: darts (JuliaLang#251)

* Add GeneratorUtils module and associated scripts

* Add exercise: circular-buffer

* Add exercise: darts

* Add generators/GeneratorUtils/generated_defaults/

* Wrap GeneratorUtils into a package and update generators/ environment to use it

* Modify diff-generated-runtests.sh to use local copy of problem-specifications/

* Fix comment.

* Remove generators/GeneratorUtils/generated_defaults/

The examples are excessive and per cmcaine, they clutter up the PR.

* Apply suggestions from code review

Co-authored-by: Colin Caine <[email protected]>

* Modify script to generate runtests.jl files instead of checking them

* circular-buffer: De-emphasize bonus tasks and remove cruft from example

* Apply suggestions from code review

Improve hints.md.

Co-authored-by: Colin Caine <[email protected]>

* circular-buffer: Add student note on mentoring, add simpler example.

Co-authored-by: Colin Caine <[email protected]>
Keno pushed a commit that referenced this issue Oct 9, 2023
- Fix for change in CodeInfo.slotnames type on julia master (#251)
- Add a way to break on throw (#253)
- Exclude Union{} from is_vararg_type (#254)
- Various performance improvements (#254)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants