Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from NetLogo/string-list-functions
Added to-list and from-list
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.nlogo.extensions.string; | ||
|
||
import org.nlogo.api.Argument; | ||
import org.nlogo.api.Context; | ||
import org.nlogo.api.DefaultReporter; | ||
import org.nlogo.api.ExtensionException; | ||
import org.nlogo.api.LogoException; | ||
import org.nlogo.api.Syntax; | ||
import org.nlogo.api.LogoListBuilder; | ||
import org.nlogo.api.LogoList; | ||
|
||
public class FromList extends DefaultReporter{ | ||
|
||
@Override | ||
public Syntax getSyntax(){ | ||
return Syntax.reporterSyntax(new int[] { Syntax.ListType() }, | ||
Syntax.StringType()); | ||
} | ||
|
||
@Override | ||
public Object report(Argument[] arg0, Context arg1) | ||
throws ExtensionException, LogoException { | ||
LogoList input = arg0[0].getList(); | ||
StringBuilder sb = new StringBuilder(); | ||
for (Object o : input){ | ||
sb.append(String.valueOf(o)); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.nlogo.extensions.string; | ||
|
||
import org.nlogo.api.Argument; | ||
import org.nlogo.api.Context; | ||
import org.nlogo.api.DefaultReporter; | ||
import org.nlogo.api.ExtensionException; | ||
import org.nlogo.api.LogoException; | ||
import org.nlogo.api.Syntax; | ||
import org.nlogo.api.LogoListBuilder; | ||
import org.nlogo.api.LogoList; | ||
|
||
public class ToList extends DefaultReporter{ | ||
|
||
@Override | ||
public Syntax getSyntax(){ | ||
return Syntax.reporterSyntax( | ||
// we take in a string | ||
new int[] { Syntax.StringType() }, | ||
// and return a list | ||
Syntax.ListType()); | ||
} | ||
|
||
@Override | ||
public Object report(Argument[] arg0, Context arg1) | ||
throws ExtensionException, LogoException { | ||
String input = arg0[0].getString(); | ||
LogoListBuilder myLLB = new LogoListBuilder(); | ||
for (char c : input.toCharArray()){ | ||
myLLB.add(String.valueOf(c)); | ||
} | ||
return myLLB.toLogoList(); | ||
} | ||
|
||
} | ||
|