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
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, 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





Wednesday, 15 November 2017

scala pattern matches



collect pattern matches,

scala> val data = Map((1 -> ("prog" -> "Pink floyd")), (2 -> ("metal" -> "Architects")), (3 -> ("prog" -> "Porcupine Tree")))
data: scala.collection.immutable.Map[Int,(String, String)] = Map(1 -> (prog,Pink floyd), 2 -> (metal,Architects), 3 -> (prog,Porcupine Tree))

scala> data.collect {case (k, v) if v._1 == "prog" => v._2}
res9: scala.collection.immutable.Iterable[String] = List(Pink floyd, Porcupine Tree)

scala> data.collect {case (k, (v1, v2)) if v1 == "prog" => v2}

res10: scala.collection.immutable.Iterable[String] = List(Pink floyd, Porcupine Tree)

scala foldLeft pattern matches,

scala> val data = Map((1 -> ("prog" -> "Pink floyd")), (2 -> ("metal" -> "Architects")), (3 -> ("prog" -> "Porcupine Tree")))
data: scala.collection.immutable.Map[Int,(String, String)] = Map(1 -> (prog,Pink floyd), 2 -> (metal,Architects), 3 -> (prog,Porcupine Tree))

scala> data.foldLeft(""){case (text, (id, (genre, artist))) => text + "\n" + artist}
res17: String =
"
Pink floyd
Architects

Porcupine Tree"




Friday, 3 November 2017

collectFirst on Map of Sequence


To find a product first bought from a map of year to List of Items

scala> def firstGuitar(list: Seq[String]) = list.collectFirst { case item if item == "Guitar" => item }
firstGuitar: (list: Seq[String])Option[String]

scala> Map(2016 -> Seq("Cap", "Guitar", "Cap"), 
           2017 -> Seq("Here and now album", "Drum Kits", "Cap", "Guitar")).collectFirst { case (year, itemsBought) 
                        if firstGuitar(itemsBought).nonEmpty => (year -> firstGuitar(itemsBought))}
res2: Option[(Int, Option[String])] = Some((2016,Some(Guitar)))

scala> Map(2016 -> Seq("Cap", "Camera", "Cap"), 
            2017 -> Seq("Here and now album", "Drum Kits", "Cap", "Tshirt")).collectFirst { case (year, itemsBought) 
                       if firstGuitar(itemsBought).nonEmpty => (year -> firstGuitar(itemsBought))}
res3: Option[(Int, Option[String])] = None


Monday, 23 October 2017

clojure epoch time to LocalDateTime to UTC

Part 1 - epoch millis to LocalDateTime in machine with PST Zone 


user=> (import java.util.TimeZone)
java.util.TimeZone
user=> (TimeZone/getDefault)
#object[sun.util.calendar.ZoneInfo 0x36a7961e "sun.util.calendar.ZoneInfo[id=\"America/Los_Angeles\",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]"]

user=> (import java.time.ZoneId)
user=> (import java.time.Instant)

user=> (.atZone (Instant/ofEpochMilli (long 1500318045000)) (ZoneId/systemDefault))
#object[java.time.ZonedDateTime 0x386969fa "2017-07-17T12:00:45-07:00[America/Los_Angeles]"]

user=> (.toLocalDateTime (.atZone (Instant/ofEpochMilli (long 1500318045000)) (ZoneId/systemDefault)))
#object[java.time.LocalDateTime 0x778e9284 "2017-07-17T12:00:45"]

Same epoch millis on a machine with UTC Zone, 

scala> import java.time.Instant
scala> import java.util.TimeZone

scala> TimeZone.getDefault
res9: java.util.TimeZone = sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

scala> Instant.ofEpochMilli(1500318045000l).atZone(ZoneId.systemDefault()).toLocalDateTime
res10: java.time.LocalDateTime = 2017-07-17T19:00:45

// to milis to UTC
scala> Instant.ofEpochMilli(1500318045000l).atZone(ZoneId.of("UTC")).toInstant.toString
res36: String = 2017-07-17T19:00:45Z

Part 2 - Converting date-string to LocalDateTime and ZonedDateTime

user=> (import java.time.format.DateTimeFormatter)
user=> (import java.time.ZoneId)
user=> (import java.util.TimeZone)
user=> (import java.time.LocalDateTime)

(let [formatter  (DateTimeFormatter/ofPattern "yyyy-MM-dd'T'HH:mm")
       localDate   (LocalDateTime/parse "2017-01-28T04:00" formatter)
       myZone     (ZoneId/of "America/Chicago")
       zonedDate (.atZone localDate myZone)
       instant     (.toInstant zonedDate)]
       (print (.toString instant)))

2017-01-28T10:00:00Z

Sunday, 16 July 2017

color theme for javascript and HTML programming

I was using solarised dark for java/scala programming for last couple of years. But since last year sept, I'm more addicted to solarised light with increased font size (15).

I like to try new themes if they work well for my eyes. And recently for javascript/html programming I started using emacs color theme which is very cool. Looks similar to solarized light, but I can see all the keywords unlike in some themes where js keywords like console.log are too pink can't even recognize.


javascript/ typescript



html


color theme IngweLAND is pretty cool too.




Friday, 28 April 2017

HTTP request headers


Check request header with cURL (see URL)
- cURL  is  a tool to transfer data from or to a server, using one of the supported protocols like HTTPS/ SMTPS/ gopher etc. see https://curl.haxx.se/


$ curl -v -XGET 172.24.14.158
* Rebuilt URL to: 172.24.14.158/
*   Trying 172.24.14.158...
* Connected to 172.24.14.158 (172.24.14.158) port 80 (#0)
> GET / HTTP/1.1
> Host: 172.24.14.158
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 
< Content-Type: text/html;charset=ISO-8859-1
< Date: Sun, 30 Apr 2017 06:29:31 GMT
< Set-Cookie: JSESSIONID=C565042EC3A26A25D4ECF6787B54CE76;path=/;HttpOnly
< Content-Length: 14
< Connection: keep-alive
< 
some.her.data
* Connection #0 to host 172.24.14.158 left intact

Check request header with browser