Visar inlägg med etikett optional. Visa alla inlägg
Visar inlägg med etikett optional. Visa alla inlägg

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/