2016-10-12

Scala URL Parser Combinator

URL Parser Combinator

(updated 2016-10-16)
It has bugged me that there is no parser of an URL so I made one.

Parser Combinator

Scala provides an excellent tool of parsing using a mix of code in DSL (domain specific language) and regular expressions. Together with unapply it's easy to match and take out the matching pieces.

URL

From Wikipedia there are the following parts of an URL (some are optional):
scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

Code

Enjoy, I won't go into detail other than it breaks down the URL in the following order:
  1. Split URL by
    1. Mandatory scheme
    2. Optional domain
    3. Mandatory path
    4. Optional query and fragment
  2. Domain is split by
    1. Authorization
    2. Domain
    3. Port
Usually a greedy regexp matcher is used until a delimiter.

2016-10-11

Javascript monad Maybe with Monet



The Monad Maybe

Maybe aka option or optional is very useful for preventing null. Using the Javascript library Monet it's quite easy to use this monad.

Preventing null

You have a variable but don't know if it's null. With Maybe, you wrap it and test if it is "some".

Transform or map

Monads are also useful to transform a value to another. Think of this example:
Each step is instancing a variable and making a "halt". With a monad, you can "go with the flow" and do it all in one step:

Sample

https://jsfiddle.net/yapdd5xv/15/

2016-10-07

Monad implementation in Scala with example

Monad in Scala

Monad

Difficult to explain but useful for nice code.

Implementation in Scala

This is a simple implementation of a monad in Scala which makes Any able to use monad's definition called flatmap (>>=).
/**
 * Monad implementation
 */
final case class Monad[T](val value : T) {

  def >>=[S](f : T => S) : Monad[S] = {
    new Monad(f(value))
  }

  override def toString = String.valueOf(value)
}

/**
 * Implicit wrap Any to a Monad.
 */
implicit def anyToMonad[T](value : T): Monad[T] = new Monad[T](value)

Example

Using this implementation, Any can use flatmap wihch could be used as follows.
// Example
"10" >>= Integer.parseInt >>= (_ + 10) >>= (0 to _) >>= println
Prints
Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

Fiddle

Try it out at Scala Fiddle!

2016-09-01

Scala Stack with Google Flexible Environment

Scala Stack with Google Flexible Environment

(this article is not yet completed)
(updated 2016-11-11)

I've created a few Maven projects based on Google Flexible Environment and Scala backend which produces HTML, CSS and JavaScript without any files (.html, .css, .sass, .less, .js) and without any frontend builder like Gulp or Grunt.

Goal

To produce all frontend from Scala backend utilizing shared code and to be fully tested.

Infographic

Scala servlets creates HTML with CSS and JavaScript.
This is a graphic representation of how it works.
  • All code is written in Scala
  • Code may share
    • Resources
    • Validation
    • Routes
    • Logic
    • Translations and other data
    • Structure of
      • HTML
      • CSS
  • A servlet can read Scala JS source code and compile a JavaScript
    • This includes the shared code
  • A servlet can build HTML with CSS 
  • Other servlets can provide REST API and also use the shared code
  • All code can be unit tested and all code can be tested in the same test
This means that the Scala stack covers both frontend and backend.

Code

There is a repository called Scala Stack at Github. There is one branch for jQuery in development and one branch for Angular which is only started:
*Unfortunately I can't afford Google Compute Engine running 24/7 and was forced to shut them down. If Google Compute Engine would scale from 0 to minimize expense I could start them again.

Examples

I'm currently working on a example using jQuery (in progress) and one using Angular (started).
Note that Scala JS source is placed in webapp, an alternative could be resources



2016-07-20

Migration from Google App Engine to Flexible Environment

Migration

Google App Engine is great since it's reliable and have tons of services free to use. Compared to a platform like Heruko, you get search, database, image and other services for free. But as a javaite, it's unfortunate that it only supports Java 7. The only way to move forward is to migrate to Flexible Environment. But be aware, not all services can migrate!

Start: App Engine

Let's start with a simple App Engine Maven project and then migrate it.

Prerequisites

Use the following guide to install Java 7 SDK and Maven.

Create Artifact

Use Maven to create the simple appengine skeleton artifact.
  1. Run
    $ mvn archetype:generate -Dappengine-version=1.9.38 -Dapplication-id=whatever -Dfilter=com.google.appengine.archetypes:
  2. Select "1"
    1: remote -> com.google.appengine.archetypes:appengine-skeleton-archetype (A skeleton application with Google App Engine)
  3. Select "6"
    6: 2.1.0-1.9.38
  4. Enter group id (same as your Java package name for the project)
    Define value for property 'groupId':
  5. Enter your name for your project
    Define value for property 'artifactId': :
  6. Enter first version
    Define value for property 'version':  1.0-SNAPSHOT:
  7. Enter Java package (same as group id)
    Define value for property 'package':
  8. Enter gcloud version (find latest here)
    Define value for property 'gcloud-version': 2.0.9.111.v20160527
  9. Confirm your selection
     Y:
Maven have now created an App Engine project in a subfolder. Start it with $ mvn appengine:devserver
Notice the layout and the few files needed.
Simple layout with few files

Migration: Flexible Environment

Let's go for Flexible Environment and Java 8!

Prerequisites

  • Install Java 8 SDK and never look back!
    Note: that App Engine uses Jetty which need Java 7 to work with JSP. If you install Java 8 it will be difficult for you to run your old App Engine projects.
  • Install Google Cloud SDK. Make sure Python is on path after installation!
    https://cloud.google.com/sdk/
  • Install the app-engine component to GCloud
    gcloud components install app-engine-java
    https://cloud.google.com/appengine/docs/flexible/java/maven

Migration

Follow the steps in the following guide.
  1. Add the following line in appengine-web.xml
    <vm>true</vm>
  2. Add the following two plugins in pom.xml.
    1. The first one to be able to run with GCloud maven goals
    2. And the second to set Java 8 compilation.
  3. <plugin>
       <groupId>com.google.appengine</groupId>
       <artifactId>gcloud-maven-plugin</artifactId>
       <version>2.0.9.106.v20160420</version>
    </plugin>
  4.       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <version>3.3</version>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
  5. Run with
  6. $ mvn gcloud:run

2016-07-12

Google Flexible Environment with Virtual Scala JS Compiler

Google Flexible Environment with Virtual Scala JS Compiler

After several months I managed to translate Scala JS Fiddle to Google Java Flexible Environment 

Goal

To create a project with Scala backend which generates JavaScript in a virtual compiler.

Live Demo

The live demo uses a Scala Servlet to generate HTML (Scala Tags) with a JavaScript. A second Scala Servlet is responding to "/javascript.js" and is using a virtual compiler to compile a String with simple Scala JS code to a JavaScript

Environment

It's a Maven project using Google Flexible "Compat" Environment which includes some App Engine features (not used) and Java 8 with Servlet API 3.1.

Source

Check out my Github

2016-07-02

My AirBnB

Mariabergets topp

Stockholm, Stockholms län, Sverige
Mitt ställe ligger nära Monteliusvägen, Ivar Los park, The Bishops Arms, Akkurat restaurang & bar, Rival, Mariatorget, Mariaberget, Tavastgatan, Brännkyrkogatan, Slussen, Bysistorget, Münchenbrygge...