Skip to content

Commit

Permalink
Merge pull request #121 from aya-lang/cleanup-2024dec4
Browse files Browse the repository at this point in the history
Standard Library Cleanup & A Few Fixes
  • Loading branch information
nick-paul authored Dec 5, 2024
2 parents 1bde0bf + 7149ceb commit d3453e7
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 51 deletions.
2 changes: 1 addition & 1 deletion examples/genetic.aya
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct dna {data fitness}
.# over the length of the array
def dna::calc_fitness {target::dna self,
.# Leave the fitness on the stack
target.data self.data .= .E \W\ / $ self.:fitness;
target.data self.data {=} .& .E \W\ / $ self.:fitness;
}

.# Apply a random mutation to the dna
Expand Down
6 changes: 3 additions & 3 deletions examples/plot/iris.aya
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ df.["Species"] :~ :species;
plot.plot! :plt;
"Iris Dataset" plt.:title;
"sepal length" plt.x.:label;
"sepal width" plt.y.:label;
"sepal width" plt.y.:label;

species :# {s_name : sp_df,
"Species" {s_name =} df.filter_on_col :sp_df;
sp_df.["Sepal.Length"] sp_df.["Sepal.Width"] {, s_name:label } plt.scatter
df.[df.["Species"] s_name .=] :sp_df;
sp_df.["Sepal.Length"] sp_df.["Sepal.Width"] {, s_name:label } plt.scatter
};

plt.view
38 changes: 22 additions & 16 deletions src/aya/instruction/op/DotOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -726,35 +726,41 @@ public void execute(BlockEvaluator blockEvaluator) {
}
}

// = 61 new OP_Dot_Equals(),
// = - 61
class OP_Dot_Equals extends Operator {

public OP_Dot_Equals() {
init(".=");
arg("LL|AL|LA", "element-wise equivalence");
arg("AA", "equality (vectorized)");
vect();
}

private static NumberListOp NUML_OP = new NumberListOp() {
public NumberList ln(NumberList a, Number b) { return a.eq(b);}
public NumberList nl(Number a, NumberList b) { return b.eq(a);}
public NumberList ll(NumberList a, NumberList b) { return a.eq(b);}
public NumberList l(NumberList a) { throw new UnimplementedError(); }
};

@Override
public void execute(final BlockEvaluator blockEvaluator) {
final Obj a = blockEvaluator.pop();
final Obj b = blockEvaluator.pop();

if (a.isa(DICT) && b.isa(DICT)) {
blockEvaluator.push(a.equiv(b) ? Num.ONE : Num.ZERO);
} else if (a.isa(LIST) && b.isa(LIST)) {
blockEvaluator.push(asList(a).equalsElementwise(asList(b)));
} else if ( a.isa(LIST) ) {
blockEvaluator.push(asList(a).equalsElementwise(b));
} else if ( b.isa(LIST) ) {
blockEvaluator.push(asList(b).equalsElementwise(a));
} else {
throw new TypeError(this, a, b);
}
final Obj a = blockEvaluator.pop();
blockEvaluator.push(exec2arg(blockEvaluator.getContext(), a, b));
}

@Override
public Obj exec2arg(ExecutionContext context, final Obj a, final Obj b) {
Obj res;
// First apply standard vectorization rules
if ((res = VectorizedFunctions.vectorize2arg(context, this, a, b, NUML_OP)) != null) return res;
// Vectorization rules applied, return standard equals
return Num.fromBool(a.equiv(b));
}
}



// ? - 63
class OP_Dot_Conditional extends Operator {

Expand Down
19 changes: 11 additions & 8 deletions src/aya/variable/VariableData.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class Scope {
private final Dict OBJ_NUM = new Dict();
private final Dict OBJ_CHAR = new Dict();
private final Dict OBJ_BLOCK = new Dict();
private final Dict BUILTINS = new Dict();

// Builtins are static across all threads
private static final Dict BUILTINS = new Dict();

public final Dict OBJ_NIL = new Dict();

Expand All @@ -72,7 +74,6 @@ public VariableData duplicateWithGlobals() {
v.OBJ_CHAR.update(this.OBJ_CHAR);
v.OBJ_BLOCK.update(this.OBJ_BLOCK);
v.OBJ_NIL.update(this.OBJ_NIL);
v.BUILTINS.update(this.BUILTINS);

return v;
}
Expand All @@ -93,12 +94,14 @@ public void initGlobals() {
OBJ_NUM.set(SymbolConstants.KEYVAR_META, OBJ_NUM);
OBJ_STR.set(SymbolConstants.KEYVAR_META, OBJ_STR);

BUILTINS.set(SymbolConstants.CHAR, OBJ_CHAR);
BUILTINS.set(SymbolConstants.SYM, OBJ_SYM);
BUILTINS.set(SymbolConstants.BLOCK, OBJ_BLOCK);
BUILTINS.set(SymbolConstants.LIST, OBJ_LIST);
BUILTINS.set(SymbolConstants.NUM, OBJ_NUM);
BUILTINS.set(SymbolConstants.STR, OBJ_STR);
if (BUILTINS.size() == 0) {
BUILTINS.set(SymbolConstants.CHAR, OBJ_CHAR);
BUILTINS.set(SymbolConstants.SYM, OBJ_SYM);
BUILTINS.set(SymbolConstants.BLOCK, OBJ_BLOCK);
BUILTINS.set(SymbolConstants.LIST, OBJ_LIST);
BUILTINS.set(SymbolConstants.NUM, OBJ_NUM);
BUILTINS.set(SymbolConstants.STR, OBJ_STR);
}

_var_sets.add(new Scope(globals));
}
Expand Down
2 changes: 1 addition & 1 deletion std/canvas.aya
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,5 @@ def canvas::text {self,

def canvas::get_pixels {self : data image^,
self.id :{graphics.get_pixels} :data;
data.data data.width data.height image!
data.r data.g data.b data.a data.width data.height data.meta image!
}
19 changes: 10 additions & 9 deletions std/csv.aya
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,26 @@ def csv::read { arg : csv^ ,
}

def csv::_read_kw {kwargs::dict : csv^
clabel(1)
rlabel(nil)
filename(nil)
csv_str(nil)
clabel(1) .# bool
rlabel(::nil) .# bool
filename(::nil) .# str
csv_str(::nil) .# str
dlm(",")
data
colnames(nil)
rownames(nil),
colnames(::nil) .# ::list
rownames(::nil) .# ::list
,
kwargs ~

(csv_str nil = filename nil = &) {"csv.read: Must provide either filename or csv_str" .D} ?
(csv_str ::nil = filename ::nil = &) {"csv.read: Must provide either filename or csv_str" .D} ?

.# Open the file and read into data
filename nil =! {filenameP G :csv_str;} ?
filename ::nil =! {filenameP G :csv_str;} ?

csv_str dlm csv.parse :data;

.# Attempt to auto-detect rlabel if it is not specified
rlabel nil = {
rlabel ::nil = {
data.[0].[0] "" = {
1 :rlabel;
} {
Expand Down
44 changes: 32 additions & 12 deletions std/dataframe.aya
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class dataframe
export ::dataframe

def dataframe::__init__ {kwargs::dict self :
data(nil)
colnames(nil)
index(nil),
data(::nil)
colnames(::nil)
index(::nil),

kwargs ~

Expand All @@ -15,20 +15,20 @@ def dataframe::__init__ {kwargs::dict self :
[] self.:_data;

.# Create using data
data nil =! {:numrows numcols,
data ::nil =! {:numrows numcols,
data E :numrows;
data .TE :numcols;
data self.:_data;

.# Column names
colnames nil = {
colnames ::nil = {
numcols self.M._gen_colnames
} {
colnames
} .? self.:_colnames;

.# Row names
index nil = {
index ::nil = {
numrows .R
} {
index
Expand Down Expand Up @@ -103,20 +103,40 @@ def dataframe::_get_col {colname self : colindex,
self._data :# { colindex I }
}

def dataframe::_numgetindex {index::num self : dataframe^,
{,
[self._data.[index]] :data;
[self._index.[index]] :index;
self._colnames :colnames;
} dataframe!
def dataframe::_numgetindex {index::num self : dataframe^ idx,
.# Lookup the index in _index
self._index index N :idx;

idx 0 :> {
{,
[self._data.[idx]] :data;
[self._index.[idx]] :index;
self._colnames :colnames;
} dataframe!
} {
"Index $num does not exist in df._index" .D
} .?
}

def dataframe::__getindex__ { index self : dataframe^,
.# If input is a number, look up that row, return a dataframe
.# If input is a string, look up that column, return a list
.# If input is a list, use it as a row mask, return a dataframe
{
(index :T ::num =) {
index self._numgetindex
} (index :T ::str =) {
index self._get_col
} (index :T ::list =) {
index E self._data E = {
{,
self._data index .i :data;
self._index index .i :index;
self._colnames :colnames;
} dataframe!
} {
"List length must match row length" .D
} .?
} {
"Unsupported index: $index" .D
}
Expand Down
4 changes: 4 additions & 0 deletions std/image.aya
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def image::pixels {self,
[self.r self.g self.b self.a] .T
}

def image::pixels_rgb {self,
[self.r self.g self.b] .T
}

def image::channels {self,
[self.r self.g self.b self.a]
}
2 changes: 1 addition & 1 deletion test/dot_ops.aya
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{[1 2 3 [1 2]] 1 .= [ 1 0 0 [ 1 0 ] ] } test.test
{"abc" "Abc" .= [0 1 1]} test.test
{"abc" "Abc" {=} .& [0 1 1]} test.test

{ [1 2 3 4] [2 3] .- [ 1 2 ] } test.test
{ [1 2 3 4] 2 .- [ 1 2 4 ] } test.test
Expand Down

0 comments on commit d3453e7

Please sign in to comment.