Thursday, 22 November 2018

haskell 101 - hello world with cabal

I am in love with fp these days. In my opinion the cool thing about fp is about being deterministic. Just like in mathematics where a function f(x) = x * x always returns a same output for a given input.
I have been in situtations where I had to debug in exising applications where reference is passed around all over the place and mutated in multiple places. It is very dificult to trace mutations. I'm still learning fp but this week decided to do some haskell as if I'm building a professional application so that I can also learn the tooling in haskell.
So, the first step is to install cabal package manager which is equivalent to sbt, gradle or maven in JVM world.
$ cabal --version
cabal-install version 2.0.0.0
compiled using version 2.0.0.2 of the Cabal library 
With cabal installed in a machine next step is to create a create a haskell project with base structures which is what cabal init does.
mkdir infp
cd infp
cabal init
Otherwise there is always an option to create those file by yourself.
The structure of a project looks like
$ tree .
.
├── ChangeLog.md
├── LICENSE
├── Setup.hs
├── infp-world.cabal
└── src
    └── Main.hs

1 directory, 5 files
*.cabal equivalent to pom.xml of JVM world for dependency management and application artifact creation etc which will look like
name:                infp-world
version:             0.1.0.0
license:             BSD3
license-file:        LICENSE
author:              prayagupd
maintainer:          upd@upd.com
build-type:          Simple
extra-source-files:  ChangeLog.md
cabal-version:       >=1.10

executable infp-world
  main-is:             Main.hs
  hs-source-dirs:      src
  build-depends:       base >=4.10 && <4.11
  default-language:    Haskell98
build-depends section is where I have to add external dependencies which are available in http://hackage.haskell.org/packages/
For example if I need mongodb driver http://hackage.haskell.org/package/mongoDB could be the one which I would add as
build-depends:       base >=4.10 && <4.11,
                     mongodb == 2.4.0.0
For this application we simply want to print the system time so lets use the time package,
build-depends:       base >=4.10 && <4.11,
                     time == 1.9.2
Followed by cabal install to download the dependencies which will be installed to ~/.cabal/packages/ pretty much same as ~/.m2/repository in JVM world.
Now, now the next step is to add some functionality to get system time in main function.
To do that let's create following src/Main.hs. Note to edit the file, you can use vim or emacs with autocomplete (I use spacemacs - https://github.com/syl20bnr/spacemacs/tree/master/layers/%2Blang/haskell).
module Main where

import Data.Time
import Control.Applicative

main :: IO ()
main = do
  time <- getZonedTime
  putStrLn ("current time: " ++ show time)
lets build the project now which will create an executable in dist.
$ cabal build
Preprocessing executable 'infp-world' for infp-world-0.1.0.0..
Building executable 'infp-world' for infp-world-0.1.0.0..
It's good to see no compilation error, so it is good to run now with cabal run,
$ cabal run
Preprocessing executable 'infp-world' for infp-world-0.1.0.0..
Building executable 'infp-world' for infp-world-0.1.0.0..
Running infp-world...
current time: 2018-11-22 22:54:06.355117 PST
There we go, it prints current time: 2018-11-22 22:54:06.355117 PST which is the what we wanted.
So, there I end the hello world in haskell using cabal package manager. I am doing REST API on haskell as well so at some point plan to write a post on that.

Saturday, 20 October 2018

join clips in imovie in macos


In the latest version of imovie, clips can be joined clicking the "share as File..." at the right top corner.



In order to mute the volume of particular video, click on it at the bottom bar and lower the percentage to 0 at the setting on top right corner.


Saturday, 13 October 2018

tcp connection 101



client     ----------------------------------------> server
                TCP handshake (conn timeout) 
                      eg. 5 secs
                
           ----------------------------------------->
                reading data (read timeout)
                       eg. 60 secs

example - https://github.com/AsyncHttpClient/async-http-client/blob/master/client/src/main/resources/org/asynchttpclient/config/ahc-default.properties#L4

https://stackoverflow.com/a/3069450/432903

Saturday, 5 May 2018

basic performance measurement of JVM process/application

JDK comes with couple of tools for perf measurement
1) jconsole
2) jvisualvm
3) jmc

JMC stands for java mission control.

1) To get started type in command jmc in terminal.

$ jmc
2018-05-05 20:32:04.418 jmc[1556:12405] IMKInputSession presentFunctionRowItemTextInputViewWithEndpoint:completionHandler: : [self textInputContext]=0x0 *NO* NSRemoteViewController to client, NSError=Error Domain=NSCocoaErrorDomain Code=4099 "The connection from pid 0 was invalidated from this process." UserInfo={NSDebugDescription=The connection from pid 0 was invalidated from this process.}, com.apple.inputmethod.EmojiFunctionRowItem


2) First thing I see in UI is the CPU usage, 




3) The 5th tab shows the memory usage,



4) And the 6th tab shows the Threads




Saturday, 7 April 2018

scala populate data class from tuple values



scala> case class Order(id: String, items: Seq[String]) 
// defined case class Order

scala> ("DW-001" -> Seq("item1", "item2")) 
val res0: (String, Seq[String]) = (DW-001,List(item1, item2))

scala> (Order.apply _) tupled res0 
val res1: Order = Order(DW-001,List(item1, item2))


Obviously the tuple needs to have data as in a data class members. Out of sequence tuple won't be applied to data class.

scala> (Seq("item1", "item2"), "some-id") 
val res2: (Seq[String], String) = (List(item1, item2),some-id)
scala> (Order.apply _) tupled res2 
1 |(Order.apply _) tupled res2
  |                       ^^^^
  |                       found:    (Seq[String], String)(res2)
  |                       required: (String, Seq[String])
  |    

Tuesday, 21 November 2017

using java lambdas as groovy closure



using java lambdas from groovy

I have a scala method that takes java.util.function.Supplier as parameter.
class TestKit(system: ActorSystem) {
   def awaitCond(p: Supplier[Boolean]): Unit = tp.awaitCond(p.get)
}
syntactic sugar for Supplier functor would be () -> ... in java, but is not compiled in groovy.
def actorSystem = ActorSystem.create("actor-system")
def verifier = new TestKit(actorSystem)
verifier.awaitCond { -> return verifier.msgAvailable()}

Thursday, 16 November 2017

debugging scala web artifact deployed on docker + tomcat, from intellij



STEP 1 - enable "Java Platform Debugger Architecture" port in docker
FROM tomcat:8.0.46-jre8                                                                                
                                                                                                       
ENV JAVA_OPTS="-Denvironment=local"                                                                    
#ENV CATALINA_OPTS="$CATALINA_OPTS -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"
                                                                                                       
ENV JPDA_ADDRESS 8000                                                                                  
COPY nlu-processor-service/target/*.war /usr/local/tomcat/webapps/nlu-processor-1.0.war
                                                                                                       
EXPOSE 8080                                                                                                                                            
CMD ["catalina.sh", "jpda", "run"]

If it's simply tomcat(without docker), create bin/setenv.sh and add jpda port address.

JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"


STEP 2 - add remote debugging to intellij

Set the JPDA port as set in setenv.sh or Dockerfile