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

Add mat"" custom string literal #29

Merged
merged 3 commits into from
Apr 16, 2015
Merged

Add mat"" custom string literal #29

merged 3 commits into from
Apr 16, 2015

Conversation

simonster
Copy link
Member

The @matlab macro currently takes Julia syntax and tries to convert it to MATLAB syntax. This mostly works because the syntax is similar, but it is occasionally confusing and/or broken (see #26). This is an alternative approach that is hopefully both easier to use and harder to break.

The mat string literal allows intermixing of Julia and MATLAB code, e.g.:

julia> mat"""
       x = 1;
       for i = 1:10
           x = x + 1;
       end
       """

julia> @mget x
11.0

It also allows you to use interpolation, avoiding @mput and @mget entirely:

julia> x = 20;

julia> mat"""
       for i = 1:10
          $x = $x + 1;
       end
       """

julia> x
30

or:

julia> v = [1, 2, 3, 4, 5];

julia> mat"$(v[1:3]) = $(v[5])*2"

julia> v
5-element Array{Int64,1}:
 10
 10
 10
  4
  5

Finally, if an expression sets ans, mat returns it, so you can write:

julia> mat"nchoosek(5, 2)"
10.0

There are a couple hacks to get this to work. First, I couldn't figure out how to get string interpolation in a custom literal without calling parse on the corresponding ordinary string literal, so that's what we do. Second, the code to determine whether a variable is assigned, referenced, or both can probably be fooled, although it's hard for me to come up with a case that would actually break at the moment.

If this sounds good to others, we might consider deprecating @matlab in favor of it.

@simonster
Copy link
Member Author

I realized this doesn't work for multiple assignment (e.g. mat"[$a, $b] = [1, 2]"). It may need to be smarter.

edit: That required more code than I would have liked, but it should work now.

@lindahua
Copy link
Contributor

This is a great idea.

However, I would rather keep both and see which one people generally prefer.

@simonster simonster force-pushed the sjk/mat-str branch 2 times, most recently from 4c19514 to 58a06f7 Compare April 16, 2015 01:41
@simonster
Copy link
Member Author

Okay, I'll leave in the documentation for the other methods.

simonster added a commit that referenced this pull request Apr 16, 2015
Add mat"" custom string literal
@simonster simonster merged commit 62fb04a into master Apr 16, 2015
@simonster simonster deleted the sjk/mat-str branch April 16, 2015 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants