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

Implementing logical methods #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions src/main/java/com/jagrosh/jagtag/libraries/Functional.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
/**
* Functional Library
* Methods for conditionals and mathematics
*
*
* @author John Grosh (jagrosh)
*/
public class Functional {

public static Collection<Method> getMethods() {
return Arrays.asList(
// equivalent to a comment; gets removed at runtime
new Method("note", (env,in) -> ""),

// chooses randomly between options
new Method("choose", (env,in) -> {return in.length==0 ? "" : in[(int)(Math.random()*in.length)];}, true),

// picks a random number in the provided range
new Method("range", (env,in) -> {
try{
Expand All @@ -52,7 +52,7 @@ public static Collection<Method> getMethods() {
return in[0]+"|"+in[1];
}
}, "|"),

// performs a conditional
new Method("if", (env,in) -> {
if(in[0].equalsIgnoreCase("true"))
Expand All @@ -63,12 +63,36 @@ public static Collection<Method> getMethods() {
return in[1];
return in[2];
}, "|then:", "|else:"),


// performs a conditional and outputs true/false
new Method("bool", (env, in) -> {
if(in[0].equalsIgnoreCase("true"))
return "true";
if(in[0].equalsIgnoreCase("false"))
return "false";
return Boolean.toString(evaluateStatement(in[0]));
}),

// performs a switch conditional
new Method("switch", (env,in) -> {
String[] cases = in[1].split("\\|");

if(cases.length % 2 != 0)
return in[in.length - 1];

for (int i = 0; i < cases.length; i += 2) {
if(in[0].equals(cases[i]))
return cases[i + 1];
}

return in[in.length - 1];
}, "|cases:", "|default:"),

// performs basic mathematical function
new Method("math", (env,in) -> {
return evaluateMath(in[0]);
}),

// returns the absolute value of the provided number
new Method("abs", (env,in) -> {
try {
Expand All @@ -79,7 +103,7 @@ public static Collection<Method> getMethods() {
} catch(NumberFormatException e){}
return in[0];
}),

// rounds a number down
new Method("floor", (env,in) -> {
try {
Expand All @@ -88,7 +112,7 @@ public static Collection<Method> getMethods() {
return in[0];
}
}),

// rounds a number up
new Method("ceil", (env,in) -> {
try {
Expand All @@ -97,7 +121,7 @@ public static Collection<Method> getMethods() {
return in[0];
}
}),

// rounds a number up if the decimal part is .5 or greater, down otherwise
new Method("round", (env,in) -> {
try {
Expand All @@ -106,7 +130,7 @@ public static Collection<Method> getMethods() {
return in[0];
}
}),

// takes the sine of radians
new Method("sin", (env,in) -> {
try {
Expand All @@ -115,7 +139,7 @@ public static Collection<Method> getMethods() {
return in[0];
}
}),

// takes the cosine of radians
new Method("cos", (env,in) -> {
try {
Expand All @@ -124,7 +148,7 @@ public static Collection<Method> getMethods() {
return in[0];
}
}),

// takes the tangent of radians
new Method("tan", (env,in) -> {
try {
Expand All @@ -133,7 +157,7 @@ public static Collection<Method> getMethods() {
return in[0];
}
}),

// converts a number to a different base
new Method("base", (env, in) -> {
try {
Expand All @@ -144,7 +168,7 @@ public static Collection<Method> getMethods() {
},true)
);
}

private static String evaluateMath(String statement)
{
int index = statement.lastIndexOf("|+|");
Expand Down Expand Up @@ -201,7 +225,7 @@ private static String evaluateMath(String statement)
}
return statement;
}

private static boolean evaluateStatement(String statement)
{
int index = statement.indexOf("|=|");
Expand All @@ -217,7 +241,7 @@ private static boolean evaluateStatement(String statement)
return false;
String s1 = statement.substring(0, index).trim();
String s2 = statement.substring(index+3).trim();

try{
double i1 = Double.parseDouble(s1);
double i2 = Double.parseDouble(s2);
Expand All @@ -233,7 +257,7 @@ private static boolean evaluateStatement(String statement)
return (i1<i2);
}
}catch(NumberFormatException e){}

switch(statement.substring(index, index+3))
{
case "|=|":
Expand Down