Angel \”Java\” Lopez on Blog

August 4, 2015

New Month’s Resolutions: August 2015

Filed under: .NET, C Sharp, JavaScript, Open Source Projects — ajlopez @ 9:08 am

It was an exciting month. Time to review my July’s resolutions and to write the new ones.

– Continue work on SparkSharp [pending]
– Continue work on BScript [complete] see repo
– Continue work on DynApp [pending]
– Give a talk about TDD [complete]
– Continue work on SharpMongo [complete] see repo
– Continue work on NRiak [pending]
– Continue work on Elixir samples [pending]
– Continue work on Android samples [pending]

Additionally, I was working on:

– Started MathelSharp, mathematical library in C# [complete] see repo
– Started ElixirJS, Elixir language implemented in JavaScript [complete] see repo
Improved SimpleLists [complete] see repo
– Started MathelJS, mathematical library in JavaScript [complete] see repo
– Improved SharpGo [complete] see repo
– Improved Complexo [complete] see repo
– Improved OStore [complete] see repo
– Improved Messi [complete] see repo
– Improved AcquarellaJS [complete] see repo
– Improved SimpleTPL [complete] see repo

My new resolutions are:

– Work on MathelSharp
– Work on SparkSharp
– Work on MathelJS
– Work on Messi
– Work on renaming, refactor AjErl
– Work on OStore
– Work on DynApp

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

June 4, 2015

New Month’s Resolutions: June 2015

Filed under: .NET, C Sharp, Java, JavaScript, Liqueed, NodeJs, Open Source Projects — ajlopez @ 9:48 am

It’s the time for review my May’s resolutions and write the new ones:

– Improve ClojJS [pending]
– Add NPM support to ClojJS [pending]
– Write posts about JavaScript and Artificial Intelligence [pending]
– Give a talk about Meteor [partial]
– Prepare a talk about Clojure or ClojureScript [partial]
– Improve BScript [pending]
– Improve AjErl, distributed features [partial] see repo
– Improve Liqueed Project, kudos features [complete] see repo

I also worked on:

– Start SparkSharp, Apache Spark-like in C# [complete] see repo
– Improve SharpMongo, MongoDB-like in C# [complete] see repo
– Improve OStore, object store in memory, JavaScript/NodeJS [complete] see repo
– Improve PythonSharp, Python interpreter in C# [complete] see repo
– Improve RedPython, compile Python to C using JavaScript/NodeJS [complete] see repo

My new month’s resolutions:

– Give a talk about Meteor
– Write posts about JavaScript and Artificial Intelligence
– Improve ClojJS
– Add NPM support to ClojJS
– Continue work on SparkSharp
– Start Message project in C#, a la Apache Camel
– Improve Liqueed Project, kudos features
– Improve Templie, template engine in Java
– Improve BScript, Basic-like interpreter in C#
– Continue work on OStore
– Continue work on SharpMongo

More fun is coming ;-)

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

May 28, 2015

SparkSharp, Spark in C# (2) Implementing Map and Reduce

Filed under: .NET, C Sharp, Open Source Projects, Spark, SparkSharp — ajlopez @ 10:15 am

Previous Post

The project repo is at:

https://github.com/ajlopez/SparkSharp

I’m using TDD (Test-Driven Development) workflow to write the application, so the code is evolving with the use cases I added as tests. I only wrote the code needed to pass the tests. When I had new use cases, I will add new functionality. As an example, the original project has an Spark Context, with factory methods to create Datasets. I don’t need that class, yet. So in the current conde, the Datasets are created as public objects using new operator..

Born after a refactor, the abstract class for all Datasets is BaseDataset. Partial code:

public abstract class BaseDataset<T> : IEnumerable<T>
{
    public abstract IEnumerable<T> Elements { get; }

    public BaseDataset<S> Map<S>(Func<T, S> map)
    {
        return new EnumDataset<S>(this.ApplyMap(map));
    }

    public S Reduce<S>(Func<S, T, S> reduce)
    {
        S result = default(S);

        foreach (var elem in this)
            result = reduce(result, elem);

        return result;
    }
    
    // ...

    private IEnumerable<S> ApplyMap<S>(Func<T, S> map)
    {
        foreach (var elem in this)
            yield return map(elem);
    }
    
    // ...
}

The enumeration of the dataset elements should be implemented by the concrete subclass. The implementation of Map and Reduce is general, for all datasets. Those methods are defined in the abstract class. Thanks to C#, those methods can receive a lambda or a Func (a function).

In the ApplyMap method I’m using the C# yield operator to return an element suspending the executiong of the foreach. That command will resume when the consumer needs the next element of the enumerable collection. In this way, the generation of the elements is lazy, each element is produced only under demand. A note: C# has lambdas and delegate functions, and they are examples of good and useful features added to a programming language. In contrast, Java world has Scala, that in my opinion, it a bit “too much”. I prefer the evolution of C# instead of Scala.

There are no tests using the abstract class (it was born as a refactor), but they are tests on concrete ones. A test of Map method using EnumDataset (a Dataset that is a wrapper around an IEnumerable collection):

[TestMethod]
public void MapIncrement()
{
    EnumDataset<int> ds = new EnumDataset<int>(new int[] { 1, 2, 3 });
    BaseDataset<int> mapds = ds.Map(i => i + 1);
    var enumerator = mapds.GetEnumerator();

    for (int k = 1; enumerator.MoveNext(); k++)
        Assert.AreEqual(k + 1, enumerator.Current);

    Assert.AreEqual(3, mapds.Count());
}

And a Reduce test:

[TestMethod]
public void ReduceSum()
{
    EnumDataset<int> ds = new EnumDataset<int>(new int[] { 1, 2, 3 });
    var result = ds.Reduce<int>((x, y) => x + y);

    Assert.IsNotNull(result);
    Assert.AreEqual(6, result);
}

Next topics: more BaseDataset methods, concrete classes, datasets with keys, etc…

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

May 23, 2015

SparkSharp, Spark in C# (1) First Ideas

Filed under: .NET, C Sharp, Open Source Projects, Spark, SparkSharp — ajlopez @ 11:20 pm

Next Post

In these days, I visited Apache Spark project:

https://spark.apache.org/

And started to think about implementing some of its ideas in C#.

The original project has Datasets, that can be consumed, item by item, and processed by methods like map and reduce. A dataset can consume a text file, local files or distributed ones. The jobs to run over datasets, applying transformations, can be launched in many distributed nodes (I should review the consolidation of results).

I started a new C# project:

https://github.com/ajlopez/SparkSharp

To me, it is important to start with small steps, using TDD (Test-Driven Development) workflow. So, in my first commits, I wrote datasets that implement IEnumerable. They have methods like Map, Reduce, Split, Take, Skip. Those methods were implemented writing the tests that express the expected API and behavior.

A dataset can be a simple wrapper of any IEnumerable, or it can read a text file, reading lines.

All these datasets are local, reside in the same machine. My idea is to implement a dataset wrapper, to expose the dataset content to other machines, and write a client wrapper that runs in each machine. The client wrapper looks like a regular dataset, but when the client program needs the next item of the dataset, that item come from the remote original machine.

The remote dataset gives the next item to any client. Each item is delivered only to ONE client. So, the items can be consumed and processed by n remote clients, without having an item processed twice.

To implement such pair server/client, I should implement serialization/deserialization of an arbitrary type T. I will use my previous work in AjErl and Aktores to have such feature. Using TDD, I could assert the expected behavior of the serializaction/deserialization process. If in the future, I have a better idea for such process, like using an external robust open source serialization library, all the TDD tests will help me to make the switch.

But, baby steps. Next steps: improve current local datasets, maybe add a new variant of dataset, and write keyed datasets, created using MapToKey method (to implement)

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

May 19, 2015

AjErl Implementing Erlang in C# (2) Expressions

Filed under: .NET, C Sharp, Open Source Projects, Erlang, AjErl — ajlopez @ 9:58 am

Previous Post

Some time ago I write this project, an Erlang implementation as interpreter using C #. Let review today the implementation of expressions. An expression is evaluated in a context. For example, the variable X, is an expression that is evaluated in a context that tells how that variable is bound to a value or it is free.

All expressions are written using the workflow TDD (Test-Driven Development). After some expressions, this interface emerge in a refactor, and now all expressions implement it:

public interface IExpression
{
    object Evaluate(Context context, bool withvars = false);

    bool HasVariable();
}

The key method is Evaluate. It can allow or not that some variables are still unbound after the evaluate. There is a boolean method HasVariable that indicates the existence of variables in the expression. The simplest expression is a constant expression:

public class ConstantExpression : IExpression
{
    private object value;

    public ConstantExpression(object value)
    {
        this.value = value;
    }

    public object Value { get { return this.value; } }

    public object Evaluate(Context context, bool withvars = false)
    {
        return this.value;
    }

    public bool HasVariable()
    {
        return false;
    }
}

The Evaluate method does not use the provided context, it is only returning the original constant value, defined in the constructor. The expressions are grouped in a tree of expressions that is recursively evaluated.

An expression that evaluates a variable:

public class VariableExpression : IExpression
{
    private Variable variable;

    public VariableExpression(Variable variable)
    {
        this.variable = variable;
    }

    public Variable Variable { get { return this.variable; } }

    public object Evaluate(Context context, bool withvars = false)
    {
        if (!context.HasValue(this.variable.Name))
            if (!withvars)
                throw new Exception(string.Format("variable '{0}' is unbound", this.variable.Name));
            else
                return this.variable;

        return context.GetValue(this.variable.Name);
    }

    public bool HasVariable()
    {
        return true;
    }
}

This time, the context is used. If the variable is not bound to a value, it can return the unbound value, or raise an exception, depending on a boolean withValues. Some times, Erlang expects and needs that an expression be complete evaluated, having no unbound variables.

This code was implemented following the workflow of TDD. Some tests as examples (but it is only the final result, the important thing is the workflow, the dynamic of the programming activity). For a constant expression:

[TestMethod]
public void CreateSimpleConstantExpression()
{
    ConstantExpression expr = new ConstantExpression(10);

    Assert.AreEqual(10, expr.Value);
}

[TestMethod]
public void EvaluateSimpleConstantExpression()
{
    ConstantExpression expr = new ConstantExpression(10);

    Assert.AreEqual(10, expr.Evaluate(null));
}

For a variable expression:

[TestMethod]
public void CreateSimpleVariableExpression()
{
    Variable variable = new Variable("X");
    VariableExpression expression = new VariableExpression(variable);

    Assert.AreEqual(variable, expression.Variable);
}

[TestMethod]
public void EvaluateVariableExpression()
{
    Variable variable = new Variable("X");
    Context context = new Context();
    context.SetValue("X", 1);
    VariableExpression expression = new VariableExpression(variable);

    Assert.AreEqual(1, expression.Evaluate(context));
}

[TestMethod]
public void EvaluateUndefinedVariableExpression()
{
    Variable variable = new Variable("X");
    Context context = new Context();
    VariableExpression expression = new VariableExpression(variable);

    Assert.AreEqual(variable, expression.Evaluate(context, true));
}

[TestMethod]
public void RaiseIfEvaluateUndefinedVariableExpression()
{
    Variable variable = new Variable("X");
    Context context = new Context();
    VariableExpression expression = new VariableExpression(variable);

    try
    {
        expression.Evaluate(context, false);
        Assert.Fail();
    }
    catch (Exception ex)
    {
        Assert.AreEqual("variable 'X' is unbound", ex.Message);
    }
}

Upcoming topics: explore other expression implementations, data structure implementations for variables, maps, and more…

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

May 5, 2015

New Month’s Resolutions: May 2015

Filed under: .NET, C Sharp, JavaScript, NodeJs, Open Source Projects — ajlopez @ 9:41 am

It’s the time for review my April’s resolutions and write the new ones:

– Write posts about JavaScript and Artificial Intelligence  [pending]
– Improve ClojJS [complete] see repo
– Add npm modules support to ClojJS [pending]
– Improve Chip8 emulator [pending]
– Improve BScript [complete] see repo

Additionally, I worked on:

– Improve AcquarellaJS, first operators in match dynamic object [complete] see repo
– Improve AjErl, refactor type utilities [complete] see repo
– Improve OStore, first operators in match [complete] see repo
– Improve SimpleLists, intersect operation [complete] see repo
– Improve MeteorSamples, using Windows version 1.1 [complete] see repo
– Participate in Google Code Jam, pre-round [complete] see repo
– Liqueed Project coding [complete] see repo
– Record Liqueed Project team programming session using TDD [complete] see post/video
– Write posts about Liqueed Project [complete] read English posts read Spanish posts

The new month’s resolutions are:

– Improve ClojJS
– Add NPM support to ClojJS
– Write posts about JavaScript and Artificial Intelligence
– Give a talk about Meteor
– Prepare a talk about Clojure or ClojureScript
– Improve BScript
– Improve AjErl, distributed features
– Improve Liqueed Project, kudos features

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

January 2, 2015

New Month’s Resolutions: January 2015

A new year begins. Time to review past month’s resolutions:

– Add distributed features to AjErl, Erlang-like interpreter in C# [partial] see repo
– Improve ClojSharp [complete] see repo
– Improve SimpleLisp [complete] see repo
– Code generation with AjGenesis for Node [pending]
– Start Basic Script Language for C# [complete] see repo

Additionally, I was working on:

– Improve ScalaSharp [complete] see repo
– First operations in Chip8 emulator [complete] see repo
– Write Clojure samples (Ring, Compojure) [complete] see repo
– Work on Liqueed project [complete] see repo
– Improve AjScript [complete] see repo

This new month’s resolutions:

– Continue add distributed features to AjErl, Erlang-like interpreter in C#
– Improve ClojSharp
– Improve ScalaSharp
– Improve Chip8 emulator
– Code generation with AjGenesis for Node
– Improve Basic Script Language for C#
– Write JavaScript and Artificial Intelligence posts

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

December 14, 2014

Aktores: Implementing Actor Model In C# (2) A Simple Sample

Filed under: .NET, Akka, Aktores, C Sharp, Open Source Projects — ajlopez @ 8:18 am

Previous Post

Let’s review a simple sample that creates two actors, and sends messages to them. The code is at:

https://github.com/ajlopez/Aktores/tree/master/Samples/PingPong

As usual, this is “work in progress”, and could be change in the future. I’m following TDD (Test-Driven Development) workflow, so some API calls could be change when I add new use cases (as distributed samples).

In this solution, we have an actor:

using Aktores.Core;

public class PingActor : Actor
{
    private int messagecount;

    public int MessageCount { get { return this.messagecount; } }

    public override void Receive(object message)
    {
        messagecount++;
        this.Sender.Tell("ping", this.Self);
    }
}

An actor inherits from abtract class Actor. The method to implement is Receive. This method receives a message (sent by other actor, or by other object in the system). Internally, Aktores sends only one message to our instance, we don’t worry about concurrency. The above actor, when receives a message, increments a counters and sends another message to the original sender. This sender is referenced by this.Sender, DURING the process of THIS message. This property can change in the process of another message.

We have another similar actor:

using Aktores.Core;

public class PongActor : Actor
{
    private int messagecount;

    public int MessageCount { get { return this.messagecount; } }

    public override void Receive(object message)
    {
        messagecount++;
        this.Sender.Tell("pong", this.Self);
    }
}

The main program creates an actor systems, creates two actor instances, one of type Ping and the other of type Pong, and sends to them many message to process:

public static void Main(string[] args)
{
    ActorSystem system = new ActorSystem(2);
    var pingactor = new PingActor();
    var pongactor = new PongActor();
    
    var ping = system.ActorOf(pingactor);
    var pong = system.ActorOf(pongactor);

    for (int k = 0; k < 10; k++)
    {
        ping.Tell("pong", pong);
        pong.Tell("ping", ping);
    }

    // ....
}

There is additional code to show the message counters. The parameter 2 in the constructor of ActorSystem indicates the number of internal threads to use to process message. I should review if such info should be specified here. Two threads are enough becase we have only two actor instances, but we could have more actors.

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

December 4, 2014

New Month’s Resolutions: December 2014

It’s time to review past month’s resolutions:

– Give a talk about JavaScript and Artificial Intelligence [complete] see repo see talk read Spanish post
– Neural network in JavaScript example [complete] see repo
– Genetic Algorithms in JavaScript example [complete] see repo
– More Code Generation using AjGenesis for NodeJs [pending]
– Basic Script Language for C# [pending]
– Improve SimpleScript for JavaScript [complete] see repo

Also, I was working on:

– Improve SimpleLisp, Lisp to JavaScript compiler [complete] see repo
– Improve RuScript, Ruby interpreter in JavaScript [complete] see repo
– Improve RustScript, Rust-like interpreter in JavaScript [complete] see repo
– Create SimpleDT, simple decision tree in JavaScript [complete] see repo
– Improve ClojSharp, Clojure-like interpreter in C# [complete] see repo
– Backgammon evaluation page [complete] see repo

This new month’s resolutions:

– Add distributed features to AjElr, Erlang-like interpreter in C#
– Improve ClojSharp
– Improve SimpleLisp
– Code generation with AjGenesis for Node
– Start Basic Script Language for C#

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

November 4, 2014

New Month’s Resolutions: November 2014

Filed under: .NET, AjTalk, Aktores, C Sharp, JavaScript, NodeJs, Open Source Projects — ajlopez @ 5:25 pm

Time to review past month’s resolutions:

– Give a talk Aktores intro [complete] see repo see presentation
– Distributed AjErl [pending]
– More Express samples in AjTalkJs [complete] see repo
– Explore neural networks in JavaScript [complete] see repo
– Explore genetic algorithms in JavaScript [partial] see repo
– Explore other artificial intelligence topics in JavaScript [partial]
– More Distributed Aktores [pending]
– Start Distributed Smalltalk in AjTalkJs [complete] see repo

My new month’s resolutions:

– Give a talk about JavaScript and Artificial Intelligence
– Neural network in JavaScript example
– Genetic Algorithms in JavaScript example
– More Code Generation using AjGenesis for NodeJs
– Basic Script Language for C#
– Improve SimpleScript for JavaScript

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

Older Posts »

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 69 other followers