-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3fac8f
commit 7fa6717
Showing
8 changed files
with
419 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,64 @@ | ||
import java.util.*; | ||
|
||
import errors.*; | ||
|
||
public class DuckType { | ||
public static void test() { | ||
DuckType duck = new DuckType(new DuckType(-3,-2,-1),0,new DuckType(1,2,3)); | ||
DuckType cat = new DuckType(1); | ||
System.out.println(duck); | ||
System.out.println(duck.get(0)); | ||
System.out.println(cat.asRaw(cat.internalType)); | ||
//cat.add(cat); | ||
//cat.get(1); | ||
} | ||
|
||
ArrayList<DuckType> asList = new ArrayList<>(); | ||
Object asObj; | ||
final Class<?> internalType; | ||
|
||
public <T> DuckType(T... params) { | ||
if (params.length == 0) { | ||
internalType = null; | ||
return; | ||
} | ||
else if (params.length == 1) { | ||
asObj = params[0]; | ||
internalType = asObj.getClass(); | ||
return; | ||
} | ||
|
||
for (T obj : params) | ||
asList.add(new DuckType(obj)); | ||
internalType = ArrayList.class; | ||
} | ||
|
||
public DuckType get(int index) { | ||
if (index > -1 && index < asList.size()) | ||
return asList.get(index); | ||
else if (asList.size() == 0) | ||
throw new AttributeError(String.format("'%s' object is not subscriptable", internalType.getSimpleName())); | ||
else | ||
throw new IndexError("list index out of range"); | ||
} | ||
|
||
/*TODO: doesn't work | ||
public DuckType add(DuckType other) { | ||
if (other.asObj instanceof Number) { | ||
return new DuckType(((Number)(other.asObj)).doubleValue() + ((Number)(this.asObj)).doubleValue()); | ||
} | ||
return this.asObj.add(other.asObj); | ||
}*/ | ||
|
||
public <T> T asRaw(Class<T> type) { | ||
return type.cast(asObj); | ||
} | ||
|
||
public String toString() { | ||
if (asObj != null) | ||
return asObj.toString(); | ||
|
||
return asList.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import java.util.*; | ||
|
||
public class ParseArguments { | ||
final Map<String, String> kwargs = new HashMap<String, String>(); // TODO: make private? | ||
final ArrayList<String> args = new ArrayList<String>(); | ||
|
||
public ParseArguments(Object... params) { | ||
for (Object arg : params) { | ||
if (arg == null) { | ||
addArgs("null"); | ||
continue; | ||
} | ||
|
||
String argStr = arg.toString(); | ||
if (argStr.contains("=") && !argStr.split("=")[0].contains(" ")) { // is named argument | ||
// also checks that key does not contain any spaces | ||
addKwargs(argStr); | ||
} else { // is unnamed argument | ||
if (argStr.toCharArray()[0] == ' ') | ||
argStr = argStr.substring(1); | ||
// if first char space, remove it | ||
addArgs(argStr); | ||
} | ||
} | ||
} | ||
|
||
public ParseArguments addArgs(Object... params) { | ||
for (Object arg : params) { | ||
args.add(arg.toString()); | ||
} | ||
return this; | ||
} | ||
|
||
public ParseArguments addKwargs(String... params) { | ||
for (String item : params) { | ||
String[] kwarg = item.split("="); | ||
String key = kwarg[0]; | ||
String value = kwarg[1]; | ||
kwargs.put(key, value); | ||
} | ||
return this; | ||
} | ||
} |
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,213 @@ | ||
import java.util.*; | ||
|
||
// create DuctType extends ParseArguents? | ||
abstract class Py { // abstract = can't create object | ||
public static void main(String[] args) { //TODO: easier maps and arrays | ||
print(null, null); | ||
|
||
print("#1", new Object[] {1,2,3,4,5}); | ||
Integer[] bla1 = new Integer[] {1,2,3,4,5}; | ||
Integer[] bla2 = new Integer[] {6,7,8,9,0,69}; | ||
for(Object bla : zipAll(bla1, bla2)) | ||
print(bla); | ||
// TODO: could lead to security vulnerability, kwargs being passed in arrays | ||
print("hello", "world", "end=;\n", "sep= | "); | ||
String name = input("Name: "); | ||
name = (name.equals("")) ? "" : ' '+name; | ||
print(formatStr("{} { time }{ name }!", "good", "time=evening", "name=" + name)); | ||
print("123", "end=YEET", "end=;\n", "sep= | "); | ||
print(formatStr("{} + {} = {}", 1, 2, 3)); | ||
String bla = input(">>> "); | ||
print("testing...", ' ' + bla); | ||
print("randint:", randint(0, 10)); | ||
print("min:", min(5,1,-4)); | ||
print("char[] contains e:", contains(new Character[] {'a', 'e', 'i', 'o', 'u'}, 'e')); | ||
print("hello contains ell:", contains("hello", "ell")); | ||
//print(str(1)); | ||
print(list(range(10))); | ||
} | ||
|
||
public static void print(Object... param) { // TODO: protect against null | ||
/* | ||
* param defaults: "text1", "text2", "sep= ", "end=\n" | ||
* sep: string to display between text1 & 2 | ||
* end: string to display at end of line | ||
*/ | ||
ParseArguments params = new ParseArguments(allValues(param).toArray()); | ||
int i = 0; | ||
for (String message : params.args) { | ||
if (i > 0) { // prevent separator from appearing at end | ||
String seperator = params.kwargs.get("sep"); | ||
System.out.print((seperator != null) ? seperator : ' '); | ||
} | ||
i++; | ||
System.out.print(message); | ||
} | ||
String EOL = params.kwargs.get("end"); // Nullable | ||
System.out.print((EOL != null) ? EOL : '\n'); | ||
} | ||
|
||
/*static String str(Object object) { | ||
return object.toString(); | ||
}*/ | ||
|
||
|
||
public static String input(String prompt) { | ||
@SuppressWarnings("resource") | ||
Scanner reader = new Scanner(System.in); // Reading from System.in | ||
System.out.print(prompt); | ||
String responce = reader.nextLine(); | ||
//reader.close(); don't close or else subsequent calls will fail | ||
|
||
return responce; | ||
} | ||
|
||
public static String input() { | ||
return input(""); | ||
} | ||
|
||
public static int randint(int start, int end) { | ||
int diff = end-start+1; | ||
return (int)(Math.random()*diff)+start; | ||
} | ||
|
||
/*public static int randint(int end) { | ||
return randint(0, end); | ||
}*/ | ||
|
||
public static double min(double firstVal, double... vals) { // TODO: can't accept arrays or ArrayLists | ||
double smallest = firstVal; | ||
for (double val : vals) { | ||
if (val < smallest) | ||
smallest = val; | ||
} | ||
return smallest; | ||
} | ||
|
||
public static double max(double firstVal, double... vals) { | ||
double largest = firstVal; | ||
for (double val : vals) { | ||
if (val > largest) | ||
largest = val; | ||
} | ||
return largest; | ||
} | ||
|
||
public static boolean contains(Object[] iterable, Object subunit) { | ||
for (Object item : iterable) { | ||
if (item.equals(subunit)) return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean contains(String iterable, String subunit) { | ||
return iterable.indexOf(subunit) != -1; | ||
} | ||
|
||
public static Object[] zip(Object[]... allVals) { | ||
int width = allVals[0].length; | ||
for (Object[] val : allVals) { // TODO: turn into private function? | ||
if (val.length < width) | ||
width = val.length; | ||
} | ||
|
||
int height = allVals.length; | ||
|
||
Object[][] result = new Object[width][height]; | ||
for (int col=0; col<width; col++) { | ||
for (int row=0; row<height; row++) { | ||
result[col][row] = allVals[row][col]; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public static Object[] zipAll(Object[]... allVals) { | ||
int width = allVals[0].length; | ||
for (Object[] val : allVals) { // TODO: turn into private function? | ||
if (val.length > width) | ||
width = val.length; | ||
} | ||
|
||
int height = allVals.length; | ||
|
||
Object[][] result = new Object[width][height]; | ||
for (int col=0; col<width; col++) { | ||
for (int row=0; row<height; row++) { | ||
try { | ||
result[col][row] = allVals[row][col]; | ||
} catch(Exception E) { | ||
result[col][row] = null; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public static <T> ArrayList<T> list(Iterable<T> iterable) { | ||
ArrayList<T> list = new ArrayList<>(); | ||
for (T item: iterable) { | ||
list.add(item); | ||
} | ||
return list; | ||
} | ||
|
||
public static Range range(int start, int stop) { | ||
return new Range(start, stop); | ||
} | ||
|
||
public static Range range(int start, int stop, int step) { | ||
return new Range(start, stop, step); | ||
} | ||
|
||
public static Range range(int stop) { | ||
return new Range(stop); | ||
} | ||
|
||
public static String formatStr(String stringIn, Object... options) { | ||
/* | ||
* Example: | ||
* {} {time} {name} -> good, time=evening, name=Ethan | ||
* | ||
* Note: will NOT give errors if too many/little options provided | ||
*/ | ||
String stringOut = stringIn; | ||
for (Object replaceWithObj : options) { | ||
String replaceWith = replaceWithObj.toString(); | ||
if (replaceWith.contains("=")) { | ||
String[] dict = replaceWith.split("="); | ||
String key = dict[0]; | ||
String value = ""; | ||
if (dict.length > 1) | ||
value = dict[1]; | ||
stringOut = stringOut.replaceAll("\\{ *" + key + " *\\}", value); | ||
// match value inside curly braces | ||
} else { | ||
stringOut = stringOut.replaceFirst("\\{ *\\}", replaceWith); | ||
// match any curly braces | ||
} | ||
} | ||
return stringOut; | ||
} | ||
|
||
public static String capitalize(String in) { | ||
return ("" + in.toCharArray()[0]).toUpperCase() + in.substring(1); | ||
} | ||
|
||
private static <T> ArrayList<T> allValues(T[] nestedArray) { | ||
if (nestedArray == null) return null; | ||
|
||
ArrayList<T> everything = new ArrayList<T>(); | ||
for (T item : nestedArray) { | ||
if (item != null && item.getClass().isArray()) { // if array contains nested arrays | ||
for(T value : allValues((T[]) item)) { | ||
everything.add(value); | ||
//System.out.println(value); | ||
} | ||
} else { | ||
everything.add(item); | ||
} | ||
} | ||
return everything; | ||
} | ||
} |
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,4 @@ | ||
|
||
public class Random { | ||
|
||
} |
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,50 @@ | ||
import java.util.Iterator; | ||
|
||
public class Range implements Iterable<Integer> { | ||
|
||
private int start = 0; | ||
private int stop; | ||
private int step = 1; | ||
|
||
public Range(int start, int stop) { | ||
this.start = start; | ||
this.stop = stop; | ||
} | ||
|
||
public Range(int start, int stop, int step) { | ||
this.start = start; | ||
this.stop = stop; | ||
this.step = step; | ||
} | ||
|
||
public Range(int stop) { | ||
this.stop = stop; | ||
} | ||
|
||
public Iterator<Integer> iterator() { | ||
Iterator<Integer> it = new Iterator<Integer>() { | ||
|
||
private int currentIndex = start; | ||
|
||
public boolean hasNext() { | ||
return currentIndex < stop; | ||
} | ||
|
||
public Integer next() { | ||
currentIndex += step; | ||
return currentIndex - step; | ||
} | ||
|
||
/*public void remove() { | ||
throw new UnsupportedOperationException(); | ||
}*/ | ||
}; | ||
return it; | ||
} | ||
|
||
public static void main(String args[]) { | ||
for(int i : new Range(1, 5, 2)) { | ||
System.out.println(i); | ||
} | ||
} | ||
} |
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,7 @@ | ||
package errors; | ||
|
||
public class AttributeError extends PyError { | ||
public AttributeError(String message) { | ||
super(message); | ||
} | ||
} |
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,7 @@ | ||
package errors; | ||
|
||
public class IndexError extends PyError { | ||
public IndexError(String message) { | ||
super(message); | ||
} | ||
} |
Oops, something went wrong.