Wednesday, October 11, 2017

Declaration Files in TypeScript: An Introduction

This is the first post in what I believe would be a series where I capture some of the things I learned about TypeScript while developing ip-num, a library for working with IP numbers.

Picking up TypeScript, the language, was relatively painless. As it is often said, it is just JavaScript with types right? so how hard can it get?

What was not relatively painless was the "other things" around the language. Things like tooling, the workflow, the configurations etc. Things you need to know before you can actually use TypeScript to make anything useful.

Declarations files were one of such "other things" that introduced some learning curve due to the lack of a clear and comprehensive explanation on it. There is also a lot of dated information due to historical reasons out there, and this adds to the confusion.

tsd you said? what about typings? Or what is the deal with that @typing thing in the node_module directory? How does a GitHub repository called DefinitelyTyped tie into the picture? What role is npm even playing? etc. These were some of the not so obvious things that needed clarification.

This post captures some things I have come to understand regarding declaration files that make them less confusing. Hopefully, someone else who is just starting with TypeScript will find it useful.

If you have some vague idea of how declaration files work and you just need a quick, straight to the point listing of the key concepts and how they tie together? Then head to the Summary section.

For a more expository explanation, do read on.

Friday, September 29, 2017

ip-num 1.0.0 is Released

ip-num, version 1.0.0 is now available.

ip-num is a TypeScript library for working with ASNIPv4, and IPv6 numbers. It provides representations of these internet protocol numbers with the ability to perform various IP related operations with them.

ip-num is released under the MIT license and can be found on Github here. Documentation is here.

The version 1.0.0 signifies a stable, well-tested core that contains minimal abstractions for working with IPv4, IPv6, and ASN.

The Road Ahead.

As the truism goes: software is never done. The version 1.0.0 although a milestone, signifies a beginning more than a completion. That means development does not cease with version 1.0.0 out of the door but will continue. The hope is that the effort will lead to the building of a comprehensive, well documented and intuitive library for working with IP numbers in the JavaScript ecosystem.

So, moving forward, the development efforts will largely be influenced by 3 factors:

  1. Dogfooding I would be working on a project that would be using ip-num. The plan is to have this experience of actually building a product with ip-num shape further design of the API.
  2. Richer IPv6 Support. IPv6 is more than just an increase from 32 bit to 128 bit when representing IP numbers. It offers a lot more semantics and features when compared to IPv4. The plan is to provide support for these unique IPv6 features moving forward.
  3. Continual Improvements. Quick wins, processing of TODO’s, evolving smarter abstraction to capture the domain using TypeScripts functionalities etc. 

If you work in a domain or industry that deals with IP numbers and you building with JavaScript, then do check ip-num out. It is just an npm install ip-num step away :)

Saturday, December 24, 2016

Exploring Currying and Partial Application using Scala

tl;dr

  1. Currying is different from Partial (Function) Application.
  2. In Scala, it is possible to define a function that returns another function which can be called with arguments. Although this might look similar, it is not necessarily the same thing as either Currying or Partial (Function) Application.
  3. What is currying? It is a process whereby the multiple arguments required by a function are passed in, one at a time, instead of the typical process of calling the function by supplying all its arguments at once.

    val volume = 
    (length: Double, breadth:Double, height: Double) => 
                                     length * breadth * height
    
    // called with the required argument at once
    volume(1,2,3) 
    
    // creates a curried version of the function
    val curriedVersion = volume.curried
    
    // with the curried version we can pass in the arguments 
    // one after the other
    
    // pass first argument
    val withFirstArg = curriedVersion(1) 
    
    // and now with second argument
    val withFirstArgAndSecondArg = withFirstArg(2)
    
    // and now with third argument which yields the result
    val volumeValue: Double = withFirstArgAndSecondArg(3) 
    
    
  4. Partial Application what is it? It is possible to take a function that expects multiple arguments and pre-fill some of these arguments. Doing this, returns a function that can then be called by supplying the remaining argument(s) that were not pre-filled.

    val volume = 
    (length: Double, breadth:Double, height: Double) => 
                                     length * breadth * height
    
    // called with the required argument at once
    volume(1,2,3)
    
    // first and second argument pre-filled
    val withFirstAndSecondArgsSet: (Double) => Double = volume(1,2,_)
    
    // now third argument is supplied and result evaluated
    val volumeValue: Double = withFirstAndSecondArgsSet(3)
     
    
  5. Currying can then be seen as a specialised outcome of Partial Application. The resulting function of Partial Application can have any number of arguments. With Currying it has to be a chained sequence of functions that take one argument at a time.
  6. Currying can also be seen as a sequence of Partial Application where each step involves partially applying only one of all possible arguments defined by the function.
  7. Partial (Function) Application is different from partial functions.

Tuesday, September 06, 2016

Making Sense Of Symbols In Scala's Collections API

Scala comes under a lot of criticism for the leeway it gives to the use of symbols. A criticism I can understand, since if abused, can easily lead to codebases that might be difficult to read at first glance.

But even with that, I still think the criticism is aggrandized, because when it is all said and done, symbols are still very valid and capable tools for capturing and expressing ideas. Especially when they can do so in very crisp and mathematically fashion without introducing the overhead/need for verbal processing.

"But it is almost impossible to google these symbols"! The complaint goes...

True, but when you think about it, this isn't an inadequacy of Scala, but the inadequacy with most search engines and their inability to deal with symbols in search queries.

To deal with this drawback, I have learnt to use http://symbolhound.com/ whenever I hap upon a symbol in Scala which I want to search. Symbolhound.com can deal with special symbols and does not escape them like Google et al. does.

Wednesday, August 24, 2016

Replaying Events in An Axon Framework Based Application

This post, shows how to replay events from the event store, in an event sourced application built with Axon Framework.

Note: This post applies only to 2.x versions of the Axon Framework, as the implementation of the event processing aspects of the Axon changes significantly with the version 3 release.

If you are not familiar with CQRS and/or the Axon Framework, then consider starting off by reading Exploring CQRS Architecture with Axon Framework, which is a series of post that explores the CQRS architecture/pattern and how the Axon Framework can be used to build applications according to the stipulations of CQRS.

If you’ve read these series of posts, or you are already familiar with CQRS/Axon Framework, then feel free to continue with the rest of this post.

Monday, June 27, 2016

Understanding Implicits in Scala

I knew about the maxim: "Implicits are evil" way before I started learning and using Scala. So by the time I started running into them in codebases, I was already biased. It was thus easier to raise my hands in despair at the "magic" that is implicit and just condemn their existence.

Obviously such a view is not healthy. So I decided to do what any sane human would do: If I am going to continually bitch about something, I should at least know some one or two things about what I am bitching about.

This post captures my understanding of how implicits work in Scala thus far. Not in the sense of how they work at the compiler or implementation level, but how they work in the sense of what they do and how to reason about them when encountered in a codebase.

It is not about when to make the decision to use or not use implicits. Or how to use them "tastefully". My current level of mastery of the Scala language does not permit me to make such pontification. Hopefully in the nearest feature, a post like this would follow.

Should in case you were like me a couple of months back, and just want a clue about what the hell is the magic going on with implicit, then you will find this post useful.

So let us get right to it.

Friday, April 29, 2016

Difference Between Value, Factory, and Service in AngularJs

There are various ways to create Objects in JavaScript. A couple include:

Using Object literal notation: Reference

var carObj = {
  model: "Mercedes-Benz CLS Coupé",
  getModel: function() {
  return this.model;
  }
}

console.log(carObj.getModel()); // prints Mercedes-Benz CLS Coupé