Posts

Showing posts from July, 2017

ES7 has only two features

ES7 features: Array.prototype.includes - if you declare         var a = [ 1 , 2 , 3 ] ;      a.includes(1); True      a.includes(1, 0); True      a.includes(1, 2)          The second parameter is fromIndex - which ask it to start seeking after certain no of position. Exponentiation Operator ** - example 4 ** 9 = 262144

Elixir postgrex error - Could not start application postgrex: could not find application file: postgrex.app

If you get postgrex error above, please open a file called "mix.exs" and look for application and remove postgrex from your list as shown below. # Type `mix help compile.app` for more information. def application do [mod: { HelpOn , []}, applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext, :phoenix_ecto, postgrex]] <-- div="" error="" here=""> end

Using MySql with Ecto

Image
You might need to create a folder manually, priv\repo\migrations if it doesn't exist. Otherwise ecto will complain when you run "mix ecto.migrate" Let's create some schema mix  phoenix.gen.model User users name:string email:string bio:string number_of_pets:integer Details of phoenix mix task can be found here . Next run " mix ecto.migrate " The actual documentation uses slightly different command.

Using Elixir pipe example

To demonstrate how pipe operator is used in Elixir, we try out with the following codes. defmodule Test do    def exact(a) do      a    end      def toInt(a) do      Integer.parse(a)    end end This basically takes 200, pass it to Test.exact (which returns anything that it get) and pass it on to Test.toInt which convert it into an integer.  "200" |> Test.exact |> Test.toInt

Elixir demonstrate if, case and cond keyword

The following code demonstrate how we can use if, case and cond keyword. defmodule Test do   def bigger(a) do       if (a > 2) do       :ok     else       :error    end  end def getSize(a) do    case a do     n when n in 1..20 ->           :small     n when n in 21..30 ->         :med     n when n in 31..40->        :big    end   end def getSizeCond(a) do    cond do   a > 30 ->      :big   a > 20 ->      :med   a > 10 ->       :small    end    end end So you can quickly run some case checking using code below :- case Test.bigger(2) do  :ok -> 1  :error -> -1 end

Installing Tensorflow using Anaconda

Image
The process is pretty straight forward, just that you need to keep in mind that you need to install python 3.5 in your environment and you need Anaconda 64bit installer. It is best that you use Conda instead of pip as the installer. Let's kick things off with :- conda create -n tensorflow python=3.5 activate tensorflow pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.2.1-cp35-cp35m-win_amd64.whl If you get messages like tensorflow is not supported in this platform, please check your anaconda installer and make sure it is 64 bits. To test out, fire up your python command prompt and import tensorflow as tf Sneaking in Theano Let's sneak in Theano into our Anaconda project conda install theano pygpu To test out, fire up your python command prompt and  from theano import * Sneaking in Keras To install Keras, you need to use the following command :- conda install -c conda-forge keras

Working example for Elixir EchoServer

The Elixir's sample code for Echo server doesn't exactly work for me. So i created my own EchoServer with some modification here and there. This supports multiple client request. Here's the sample code defmodule Processboy do require Logger @doc """ Starts accepting connections on the given `port`. Processboy.Supervisor.start(nil, nil) """ def accept (port) do {:ok, socket} = :gen_tcp.listen(port, [:binary, packet: :line, active: false , reuseaddr: true ]) Logger .info "Accepting connections on port #{port}" loop_acceptor(socket) end def init () do import Supervisor . Spec children = [ supervisor( Task . Supervisor , [[name: Processboy , restart: :transient]]) ] #opts = [strategy: :one_for_one, name: Processboy] {:ok, pid} = Supervisor .start_link(children, strategy: :one_for_one) end defp loop_acceptor (socket) do {:ok, client} = :gen_tcp.accept(socket) Logg

HttpClient - throwing connection refused when using Https

If you make a simple HTTS request using HttpClient and you get an exception message saying "connection has been refused by remote host", then most likely is you're using .Net 4.5 or below which does not support HTTPS request. Upgrade your project to .Net 4.6 and you're good. Updates :- In case you still facing the issue in your Web Project like ASP.Net MVC, please try  adding the following code before you make a https request : ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; Here's a sample implementation code :

Using Elixir Supervisor's start_link and start_child

This gives a very simple example of using Elixir's supervisor - which setups a supervisor and ensure your application is running well .If your application stops, supervisor will automatically restart it - using one-for-one strategy. Here we have a module called Test and further down, we have code to setup our Supervisor. The text in bold, are the name we have given to our apps. We tell Supervisor to start our process by calling Test.say(). Our basic dummy module defmodule Test do    def say() do      IO.puts "test"    end end This is where we do all our setup. import Supervisor.Spec children = [   supervisor(Task.Supervisor, [[name: Test , restart: :transient]]), ] {:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one) Runs our module. Task.Supervisor.start_child( Test , fn -> Test.say() end) That's it. Bottom line is, you need to setup your supervisor and then you ask Supervisor to start running a module. 

Understanding Elixir loop

As many of you already know Elixir does not have loop. There are 2 main things to understand about this. a) You need the same method signature to check the loop count b) Your method need to be handling the real actions / tasks and not loop checker. Let's see this in action. You have 2 runs methods and this has to match, otherwise elixir will complaint. Next, the run method which implement loop check, just do what it suppose to do, check the counter. Depending on true / false, you method "run" (without loop checking) is call. That's why you need to have implementation code like :timer.sleep in there. defmodule FakeProcess do def run (n) when n <= 1 do IO .puts "serving as a final loop run" :timer.sleep( 1000 ) end def run (n) do IO .puts n :timer.sleep( 1000 ) run(n - 1 ) end end

File reading fun with Elixir and Erlang

To read a file using Elixir File module, all you gotta do is this (if you fire up your iex, and run this - assuming you have your file in the proper location) In Elixir  {:ok, wf} =  File.read "c:\\tmp\\test2.log" What if you wanted to write. In that case, we need to open our file using the command below and assigned file handler to a variable called "wf".  {:ok, wf } = File.open "c:\\tmp\\test3.log" To write to your already open file handler.  IO.binwrite wf , "uhh llalala" Using Erlang   :file.read_file("c:\\tmp\\test2.log") This also gives us a clue to call Erlang's built in libraries.

Elixir case do pattern matching example

case do allow us to quickly specify some condition and then we're good to go. The only catch here is the sequence condition (1st condition) a <=3 and (2nd condition) a > 3 must be in sequence, otherwise elixir will complaint. It is not a bug, just a logic error that you might want to avoid COND DO defmodule Test do  def say(a) do     cond do       a <= 3 -> "lesser than3 "       a > 3  -> "bigger than 3"    end   end end CASE DO Notice how we specify condition for bigger than 2 conditions. defmodule Test do   def say(a) do     case a do      1 -> "1"      2 -> "2"      n when n > 2 -> "Big"     end   end end Creating exceptions using defexception.  Yes it is why it is called defexceptions. Considered it a special keyword to do this specifically. Example code and usage :- defmodule AppError do    defexception message: "app error"    end end // invo

TSQL Common table expression getting incorrect syntax near )

This message is really not helpful at all. Say if you create a common table expression that does not immediate follow by a query, you get this nice error, for example :- WITH MyDataCTE (Id, SecurityID) AS (    SELECT [Identity] as Id, SecurityID FROM [Ids_staging].dbo.[StageJPMorganValuationReport] WHERE batchId = 38969 ) SELECT * FROM MyDataCTE  /// Without this line here, you get this error.

Weird moments in Elixir

Calling Erlang external libraries. Say you have the huge bunch of erlang libaries goodies from  here , how do you call it?  Well, fire up your iex and then run the following command : :inet.gethostname() And you get the output. Map data stucture : creating a map using %{} map = %{:a => 1, 2 => :b} Pipe operator |>

Getting started with elixir package manager can be a bitch

Many stuff are not well documented. And just crappy to read through so much to do a simple package install. Hopefully this helps. mix (installed together with your elixir) and hex is your package manager. All you need to do is create a new project using the following command (you're trying to do something like npm install) a) mix new myProject - after that try running mix compile. You can see your project being compiled. After you have done that, open a file called "mix.exs". This is your package.json file in node. Locate your "defp deps" as shown here and add in a dependencies for cowboy or whatever you like from here :- This basically saying that you are trying to use external libraries. Next run "mix deps.get"  - to download your package to a folder called dep. This is done automatically. Great and you will see dep folder being filled up. Fire up your iex (using the following command - iex -S mix) and run the following command to s

Getting started with AWS CodeCommit

Image
AWS CodeCommit is Github in a nutshell. To create your repository, goto AWS Console and click on create repository. Next, you need to go to IAM and generate your CodeCommit credentials as show in the diagram below :- Go ahead, download credentials and clone your repository as you would normally do with Git.  I guess the interesting thing to do is the "Trigger". What' happens if you commit some code into this branch. You can create trigger to AWS SNS or AWS Lambda.